1: /* $Header$ */
2:
3: /*
4: * Author: Peter J. Nicklin
5: */
6: #include "null.h"
7: #include "ptree.h"
8: /*
9: * ptree() searchs for a project directory name in a binary tree. If the
10: * search is unsuccessful, a new node is added to the tree.
11: */
12: PTREE *
13: ptree(p, alias, pd1, pd2)
14: PTREE *p; /* current node pointer */
15: char *alias; /* project directory alias */
16: char *pd1; /* project directory pathname */
17: char *pd2; /* project directory pathname */
18: {
19: char *strsav(); /* save a string somewhere */
20: int comp; /* compare key values */
21: int strcmp(); /* string comparison */
22: PTREE *palloc(); /* allocate a tree node */
23:
24: if (p == NULL)
25: { /* a new alias has arrived */
26: if ((p = palloc()) == NULL || (p->alias = strsav(alias)) == NULL)
27: goto nomemory;
28: if (pd1 != NULL)
29: {
30: if ((p->pd1 = strsav(pd1)) == NULL)
31: goto nomemory;
32: p->pd2 = NULL;
33: }
34: else {
35: if ((p->pd2 = strsav(pd2)) == NULL)
36: goto nomemory;
37: p->pd1 = NULL;
38: }
39: p->left = p->right = NULL;
40: }
41: else if ((comp = strcmp(alias, p->alias)) < 0)
42: p->left = ptree(p->left, alias, pd1, pd2);
43: else if (comp > 0)
44: p->right = ptree(p->right, alias, pd1, pd2);
45: else if (comp == 0)
46: if ((p->pd2 = strsav(pd2)) == NULL)
47: {
48: nomemory: warn("out of memory");
49: exit(2);
50: }
51: return(p);
52: }
53:
54:
55:
56: /*
57: * ptreerm() removes a project directory tree.
58: */
59: void
60: ptreerm(p)
61: PTREE *p; /* current node pointer */
62: {
63: if (p != NULL)
64: {
65: if (p->left != NULL)
66: ptreerm(p->left);
67: if (p->right != NULL)
68: ptreerm(p->right);
69: free(p->alias);
70: if (p->pd1 != NULL)
71: free(p->pd1);
72: if (p->pd2 != NULL)
73: free(p->pd2);
74: free((char *) p);
75: }
76: }
77:
78:
79:
80: /*
81: * palloc allocates memory for a project tree node.
82: */
83: static PTREE *
84: palloc()
85: {
86: char *malloc(); /* memory allocator */
87:
88: return((PTREE *) malloc(sizeof(PTREE)));
89: }
Defined functions
ptree
defined in line
12; used 9 times