Mercurial > hg > toybox
comparison lib/lib.h @ 565:44abf4d901f3
Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 14 Apr 2012 22:30:41 -0500 |
parents | 2548e6e590b2 |
children | 4877cff01b25 |
comparison
equal
deleted
inserted
replaced
564:9530899eee51 | 565:44abf4d901f3 |
---|---|
15 // libc generally has this, but the headers are screwed up | 15 // libc generally has this, but the headers are screwed up |
16 ssize_t getline(char **lineptr, size_t *n, FILE *stream); | 16 ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
17 | 17 |
18 // llist.c | 18 // llist.c |
19 | 19 |
20 // All these list types can be handled by the same code because first element | |
21 // is always next pointer, so next = (mytype *)&struct. | |
22 | |
20 struct string_list { | 23 struct string_list { |
21 struct string_list *next; | 24 struct string_list *next; |
22 char str[0]; | 25 char str[0]; |
23 }; | 26 }; |
24 | 27 |
26 struct arg_list *next; | 29 struct arg_list *next; |
27 char *arg; | 30 char *arg; |
28 }; | 31 }; |
29 | 32 |
30 struct double_list { | 33 struct double_list { |
31 struct double_list *next; | 34 struct double_list *next, *prev; |
32 struct double_list *prev; | |
33 char *data; | 35 char *data; |
34 }; | 36 }; |
35 | 37 |
36 void llist_free(void *list, void (*freeit)(void *data)); | 38 void llist_free(void *list, void (*freeit)(void *data)); |
37 void *llist_pop(void *list); // actually void **list, but the compiler's dumb | 39 void *llist_pop(void *list); // actually void **list, but the compiler's dumb |
40 | 42 |
41 // args.c | 43 // args.c |
42 void get_optflags(void); | 44 void get_optflags(void); |
43 | 45 |
44 // dirtree.c | 46 // dirtree.c |
47 | |
48 // Values returnable from callback function (bitfield, or them together) | |
49 // Default with no callback is 0 | |
50 | |
51 // Do not add this node to the tree | |
52 #define DIRTREE_NOSAVE 1 | |
53 // Do not recurse into children | |
54 #define DIRTREE_NORECURSE 2 | |
55 // Call again after handling all children (Directories only. Sets linklen = -1) | |
56 #define DIRTREE_COMEAGAIN 4 | |
57 // Follow symlinks to directories | |
58 #define DIRTREE_SYMFOLLOW 8 | |
59 // Abort recursive dirtree. (Forces NOSAVE and NORECURSE on this entry.) | |
60 #define DIRTREE_ABORT (256|DIRTREE_NOSAVE|DIRTREE_NORECURSE) | |
61 | |
62 #define DIRTREE_ABORTVAL ((struct dirtree *)1) | |
63 | |
45 struct dirtree { | 64 struct dirtree { |
46 struct dirtree *next, *child, *parent; | 65 struct dirtree *next, *parent, *child; |
66 long extra; // place for user to store their stuff (can be pointer) | |
67 long data; // dirfd for directory, linklen for symlink | |
47 struct stat st; | 68 struct stat st; |
48 int depth; | 69 char *symlink; |
49 char name[]; | 70 char name[]; |
50 }; | 71 }; |
51 | 72 |
52 struct dirtree *dirtree_add_node(char *path); | 73 struct dirtree *dirtree_add_node(int dirfd, char *name); |
53 struct dirtree *dirtree_read(char *path, struct dirtree *parent, | 74 char *dirtree_path(struct dirtree *node, int *plen); |
54 int (*callback)(char *path, struct dirtree *node)); | 75 int dirtree_isdotdot(struct dirtree *catch); |
76 struct dirtree *handle_callback(struct dirtree *new, | |
77 int (*callback)(struct dirtree *node)); | |
78 void dirtree_recurse(struct dirtree *node, | |
79 int (*callback)(struct dirtree *node)); | |
80 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); | |
55 | 81 |
56 // lib.c | 82 // lib.c |
57 void xstrcpy(char *dest, char *src, size_t size); | 83 void xstrcpy(char *dest, char *src, size_t size); |
58 void verror_msg(char *msg, int err, va_list va); | 84 void verror_msg(char *msg, int err, va_list va); |
59 void error_msg(char *msg, ...); | 85 void error_msg(char *msg, ...); |
74 void xaccess(char *path, int flags); | 100 void xaccess(char *path, int flags); |
75 void xunlink(char *path); | 101 void xunlink(char *path); |
76 int xcreate(char *path, int flags, int mode); | 102 int xcreate(char *path, int flags, int mode); |
77 int xopen(char *path, int flags); | 103 int xopen(char *path, int flags); |
78 void xclose(int fd); | 104 void xclose(int fd); |
105 int xdup(int fd); | |
79 FILE *xfopen(char *path, char *mode); | 106 FILE *xfopen(char *path, char *mode); |
80 ssize_t readall(int fd, void *buf, size_t len); | 107 ssize_t readall(int fd, void *buf, size_t len); |
81 ssize_t writeall(int fd, void *buf, size_t len); | 108 ssize_t writeall(int fd, void *buf, size_t len); |
82 size_t xread(int fd, void *buf, size_t len); | 109 size_t xread(int fd, void *buf, size_t len); |
83 void xreadall(int fd, void *buf, size_t len); | 110 void xreadall(int fd, void *buf, size_t len); |
95 void utoa_to_buf(unsigned n, char *buf, unsigned buflen); | 122 void utoa_to_buf(unsigned n, char *buf, unsigned buflen); |
96 void itoa_to_buf(int n, char *buf, unsigned buflen); | 123 void itoa_to_buf(int n, char *buf, unsigned buflen); |
97 char *utoa(unsigned n); | 124 char *utoa(unsigned n); |
98 char *itoa(int n); | 125 char *itoa(int n); |
99 long atolx(char *c); | 126 long atolx(char *c); |
127 int numlen(long l); | |
100 off_t fdlength(int fd); | 128 off_t fdlength(int fd); |
101 char *xreadlink(char *name); | 129 char *xreadlink(char *name); |
102 void loopfiles_rw(char **argv, int flags, int permissions, int failok, | 130 void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
103 void (*function)(int fd, char *name)); | 131 void (*function)(int fd, char *name)); |
104 void loopfiles(char **argv, void (*function)(int fd, char *name)); | 132 void loopfiles(char **argv, void (*function)(int fd, char *name)); |