1 /* lib.h - header file for lib directory
3 * Copyright 2006 Rob Landley <rob@landley.net>
18 // All these list types can be handled by the same code because first element
19 // is always next pointer, so next = (mytype *)&struct. (The payloads are
20 // named differently to catch using the wrong type early.)
23 struct string_list *next;
28 struct arg_list *next;
33 struct double_list *next, *prev;
37 void llist_free_arg(void *node);
38 void llist_free_double(void *node);
39 void llist_traverse(void *list, void (*using)(void *node));
40 void *llist_pop(void *list); // actually void **list
41 void *dlist_pop(void *list); // actually struct double_list **list
42 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
43 struct double_list *dlist_add(struct double_list **list, char *data);
44 void *dlist_terminate(void *list);
47 void get_optflags(void);
51 // Values returnable from callback function (bitfield, or them together)
52 // Default with no callback is 0
54 // Add this node to the tree
55 #define DIRTREE_SAVE 1
56 // Recurse into children
57 #define DIRTREE_RECURSE 2
58 // Call again after handling all children of this directory
59 // (Ignored for non-directories, sets linklen = -1 before second call.)
60 #define DIRTREE_COMEAGAIN 4
61 // Follow symlinks to directories
62 #define DIRTREE_SYMFOLLOW 8
63 // Don't warn about failure to stat
64 #define DIRTREE_SHUTUP 16
65 // Breadth first traversal, conserves filehandles at the expense of memory
66 #define DIRTREE_BREADTH 32
67 // Don't look at any more files in this directory.
68 #define DIRTREE_ABORT 256
70 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
73 struct dirtree *next, *parent, *child;
74 long extra; // place for user to store their stuff (can be pointer)
82 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags);
83 char *dirtree_path(struct dirtree *node, int *plen);
84 int dirtree_notdotdot(struct dirtree *catch);
85 int dirtree_parentfd(struct dirtree *node);
86 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
87 int dirfd, int symfollow);
88 struct dirtree *dirtree_flagread(char *path, int flags,
89 int (*callback)(struct dirtree *node));
90 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
94 void show_help(FILE *out);
97 void xstrncpy(char *dest, char *src, size_t size);
98 void xstrncat(char *dest, char *src, size_t size);
99 void _xexit(void) noreturn;
100 void xexit(void) noreturn;
101 void *xmalloc(size_t size);
102 void *xzalloc(size_t size);
103 void *xrealloc(void *ptr, size_t size);
104 char *xstrndup(char *s, size_t n);
105 char *xstrdup(char *s);
106 void *xmemdup(void *s, long len);
107 char *xmprintf(char *format, ...) printf_format;
108 void xprintf(char *format, ...) printf_format;
112 void xexec(char **argv);
113 pid_t xpopen_both(char **argv, int *pipes);
114 int xwaitpid(pid_t pid);
115 int xpclose_both(pid_t pid, int *pipes);
116 pid_t xpopen(char **argv, int *pipe, int stdout);
117 pid_t xpclose(pid_t pid, int pipe);
118 int xrun(char **argv);
119 int xpspawn(char **argv, int*pipes);
120 void xaccess(char *path, int flags);
121 void xunlink(char *path);
122 int xcreate(char *path, int flags, int mode);
123 int xopen(char *path, int flags);
127 FILE *xfdopen(int fd, char *mode);
128 FILE *xfopen(char *path, char *mode);
129 size_t xread(int fd, void *buf, size_t len);
130 void xreadall(int fd, void *buf, size_t len);
131 void xwrite(int fd, void *buf, size_t len);
132 off_t xlseek(int fd, off_t offset, int whence);
133 char *xreadfile(char *name, char *buf, off_t len);
134 int xioctl(int fd, int request, void *data);
136 void xstat(char *path, struct stat *st);
137 char *xabspath(char *path, int exact);
138 void xchdir(char *path);
139 void xchroot(char *path);
140 struct passwd *xgetpwuid(uid_t uid);
141 struct group *xgetgrgid(gid_t gid);
142 struct passwd *xgetpwnam(char *name);
143 struct group *xgetgrnam(char *name);
144 struct passwd *xgetpwnamid(char *user);
145 struct group *xgetgrnamid(char *group);
146 void xsetuser(struct passwd *pwd);
147 char *xreadlink(char *name);
148 long xparsetime(char *arg, long units, long *fraction);
149 void xpidfile(char *name);
150 void xregcomp(regex_t *preg, char *rexec, int cflags);
151 char *xtzset(char *new);
152 void xsignal(int signal, void *handler);
155 void verror_msg(char *msg, int err, va_list va);
156 void error_msg(char *msg, ...) printf_format;
157 void perror_msg(char *msg, ...) printf_format;
158 void error_exit(char *msg, ...) printf_format noreturn;
159 void perror_exit(char *msg, ...) printf_format noreturn;
160 void help_exit(char *msg, ...) printf_format noreturn;
161 void error_msg_raw(char *msg);
162 void perror_msg_raw(char *msg);
163 void error_exit_raw(char *msg);
164 void perror_exit_raw(char *msg);
165 ssize_t readall(int fd, void *buf, size_t len);
166 ssize_t writeall(int fd, void *buf, size_t len);
167 off_t lskip(int fd, off_t offset);
168 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
169 struct string_list **splitpath(char *path, struct string_list **list);
170 char *readfileat(int dirfd, char *name, char *buf, off_t *len);
171 char *readfile(char *name, char *buf, off_t len);
172 void msleep(long miliseconds);
173 int64_t peek_le(void *ptr, unsigned size);
174 int64_t peek_be(void *ptr, unsigned size);
175 int64_t peek(void *ptr, unsigned size);
176 void poke(void *ptr, uint64_t val, int size);
177 struct string_list *find_in_path(char *path, char *filename);
178 long estrtol(char *str, char **end, int base);
179 long xstrtol(char *str, char **end, int base);
181 long atolx_range(char *numstr, long low, long high);
182 int stridx(char *haystack, char needle);
183 char *strlower(char *s);
184 char *strafter(char *haystack, char *needle);
185 char *chomp(char *s);
186 int unescape(char c);
187 int strstart(char **a, char *b);
188 off_t fdlength(int fd);
189 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
190 void (*function)(int fd, char *name));
191 void loopfiles(char **argv, void (*function)(int fd, char *name));
192 void xsendfile(int in, int out);
193 int wfchmodat(int rc, char *name, mode_t mode);
194 int copy_tempfile(int fdin, char *name, char **tempname);
195 void delete_tempfile(int fdin, int fdout, char **tempname);
196 void replace_tempfile(int fdin, int fdout, char **tempname);
197 void crc_init(unsigned int *crc_table, int little_endian);
198 void base64_init(char *p);
200 int qstrcmp(const void *a, const void *b);
201 void create_uuid(char *uuid);
202 char *show_uuid(char *uuid);
203 char *next_printf(char *s, char **start);
204 char *strnstr(char *line, char *str);
205 int dev_minor(int dev);
206 int dev_major(int dev);
207 int dev_makedev(int major, int minor);
208 struct passwd *bufgetpwuid(uid_t uid);
210 #define HR_SPACE 1 // Space between number and units
211 #define HR_B 2 // Use "B" for single byte units
212 #define HR_1000 4 // Use decimal instead of binary units
213 int human_readable(char *buf, unsigned long long num, int style);
219 struct ptr_len idx[];
222 void linestack_addstack(struct linestack **lls, struct linestack *throw,
224 void linestack_insert(struct linestack **lls, long pos, char *line, long len);
225 void linestack_append(struct linestack **lls, char *line);
226 struct linestack *linestack_load(char *name);
227 int crunch_escape(FILE *out, int cols, int wc);
228 int crunch_rev_escape(FILE *out, int cols, int wc);
229 int crunch_str(char **str, int width, FILE *out, char *escmore,
230 int (*escout)(FILE *out, int cols, int wc));
231 int draw_str(char *start, int width);
232 int utf8len(char *str);
233 int utf8skip(char *str, int width);
234 int draw_trim_esc(char *str, int padto, int width, char *escmore,
235 int (*escout)(FILE *out, int cols,int wc));
236 int draw_trim(char *str, int padto, int width);
238 // interestingtimes.c
240 int terminal_size(unsigned *xx, unsigned *yy);
241 int terminal_probesize(unsigned *xx, unsigned *yy);
242 int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy);
243 int set_terminal(int fd, int raw, struct termios *old);
244 void xset_terminal(int fd, int raw, struct termios *old);
245 int scan_key(char *scratch, int miliwait);
246 void tty_esc(char *s);
247 void tty_jump(int x, int y);
248 void tty_reset(void);
249 void tty_sigreset(int i);
252 int xsocket(int domain, int type, int protocol);
253 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
254 int xconnect(char *host, char *port, int family, int socktype, int protocol,
256 int xpoll(struct pollfd *fds, int nfds, int timeout);
259 int get_salt(char *salt, char * algo);
263 struct mtab_list *next, *prev;
265 struct statvfs statvfs;
272 void comma_args(struct arg_list *al, void *data, char *err,
273 char *(*callback)(void *data, char *str, int len));
274 void comma_collate(char **old, char *new);
275 char *comma_iterate(char **list, int *len);
276 int comma_scan(char *optlist, char *opt, int clean);
277 int comma_scanall(char *optlist, char *scanlist);
278 int mountlist_istype(struct mtab_list *ml, char *typelist);
279 struct mtab_list *xgetmountlist(char *path);
283 void generic_signal(int signal);
284 void sigatexit(void *handler);
285 int sig_to_num(char *pidstr);
286 char *num_to_sig(int sig);
288 mode_t string_to_mode(char *mode_str, mode_t base);
289 void mode_to_string(mode_t mode, char *buf);
290 char *getbasename(char *name);
291 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
293 pid_t xvforkwrap(pid_t pid);
294 #define XVFORK() xvforkwrap(vfork())
296 // Wrapper to make xfuncs() return (via longjmp) instead of exiting.
297 // Assigns true/false "did it exit" value to first argument.
298 #define WOULD_EXIT(y, x) do { jmp_buf _noexit; \
300 toys.rebound = &_noexit; \
301 _noexit_res = setjmp(_noexit); \
302 if (!_noexit_res) do {x;} while(0); \
307 // Wrapper that discards true/false "did it exit" value.
308 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
310 // Functions in need of further review/cleanup
311 #include "lib/pending.h"