25 int errno; /* External symbol */
27 char *node_to_abspath(struct node *node)
35 while (tmp && node_name(tmp)) {
36 len += strlen(node_name(tmp)) + 1; /* trail '/' */
37 tmp = node_parent(tmp);
40 path = malloc(len + 2); /* '/' and '\0' */
51 while (tmp && node_name(tmp)) {
52 ptr -= (strlen(node_name(tmp)) + 1);
54 strncpy(ptr + 1, node_name(tmp), strlen(node_name(tmp)));
55 tmp = node_parent(tmp);
62 static inline int next_state(int from, char ch)
69 if ( from == 1 ) return 2;
70 if ( from == 2 ) return 3;
76 static inline void abspath(char* pBuffer, char* pRet)
82 int src_len = strlen(pBuffer);
86 while (src_idx <= src_len) {
88 state = next_state(from, pBuffer[src_idx]);
93 pRet[idx] = pBuffer[src_idx];
99 if ( idx > 0 ) idx --;
101 pRet[idx] = pBuffer[src_idx];
106 // Only can go to the 1 or 4
109 if ( idx < 0 ) idx = 0;
111 while ( idx > 0 && pRet[idx] != '/' ) idx --;
112 if ( idx > 0 && pRet[idx] == '/' ) idx --;
113 while ( idx > 0 && pRet[idx] != '/' ) idx --;
116 pRet[idx] = pBuffer[src_idx];
126 struct node *node_find(const struct node *node, char *path)
132 buffer = malloc(strlen(path) + 3); /* add 2 more bytes */
136 abspath(path, buffer);
140 ptr += (*ptr == '/');
141 for (len = 0; ptr[len] && ptr[len] != '/'; len++);
145 if (!strncmp("..", ptr, len)) {
147 node = node->parent ? node->parent : node;
151 if (!strncmp(".", ptr, len)) {
163 if (!strncmp(node->name, ptr, len) && node->name[len] == '\0') {
168 node = node->sibling.next;
170 } while (*ptr && node);
173 return (struct node *)node;
176 struct node *node_create(struct node *parent, const char *name, enum node_type type)
180 node = malloc(sizeof(*node));
182 printf("Error: %s\n", strerror(errno));
186 node->parent = parent;
189 node->name = strdup(name);
191 printf("Error: %s\n", strerror(errno));
200 node->sibling.next = NULL;
201 node->sibling.prev = NULL;
210 while (tmp->sibling.next)
211 tmp = tmp->sibling.next;
213 tmp->sibling.next = node;
214 node->sibling.prev = tmp;
216 parent->child = node;
222 void node_destroy(struct node *node)
228 struct node * const node_next_sibling(const struct node *node)
230 return node->sibling.next;
233 struct node * const node_prev_sibling(const struct node *node)
235 return node->sibling.prev;
238 void node_set_mode(struct node *node, int mode)
243 void node_set_data(struct node *node, void *data)
248 void node_set_type(struct node *node, enum node_type type)
253 struct node * const node_child(const struct node *node)
258 struct node * const node_parent(const struct node *node)
263 const int const node_mode(const struct node *node)
268 void * const node_data(const struct node *node)
273 const enum node_type const node_type(const struct node *node)
278 const char * const node_name(const struct node *node)