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;
38 struct num_cache *next;
43 void llist_free_arg(void *node);
44 void llist_free_double(void *node);
45 void llist_traverse(void *list, void (*using)(void *node));
46 void *llist_pop(void *list); // actually void **list
47 void *dlist_pop(void *list); // actually struct double_list **list
48 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
49 struct double_list *dlist_add(struct double_list **list, char *data);
50 void *dlist_terminate(void *list);
51 struct num_cache *get_num_cache(struct num_cache *cache, long long num);
52 struct num_cache *add_num_cache(struct num_cache **cache, long long num,
56 void get_optflags(void);
60 // Values returnable from callback function (bitfield, or them together)
61 // Default with no callback is 0
63 // Add this node to the tree
64 #define DIRTREE_SAVE 1
65 // Recurse into children
66 #define DIRTREE_RECURSE 2
67 // Call again after handling all children of this directory
68 // (Ignored for non-directories, sets linklen = -1 before second call.)
69 #define DIRTREE_COMEAGAIN 4
70 // Follow symlinks to directories
71 #define DIRTREE_SYMFOLLOW 8
72 // Don't warn about failure to stat
73 #define DIRTREE_SHUTUP 16
74 // Breadth first traversal, conserves filehandles at the expense of memory
75 #define DIRTREE_BREADTH 32
76 // Don't look at any more files in this directory.
77 #define DIRTREE_ABORT 256
79 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
82 struct dirtree *next, *parent, *child;
83 long extra; // place for user to store their stuff (can be pointer)
91 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags);
92 char *dirtree_path(struct dirtree *node, int *plen);
93 int dirtree_notdotdot(struct dirtree *catch);
94 int dirtree_parentfd(struct dirtree *node);
95 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
96 int dirfd, int symfollow);
97 struct dirtree *dirtree_flagread(char *path, int flags,
98 int (*callback)(struct dirtree *node));
99 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
103 void show_help(FILE *out);
106 void xstrncpy(char *dest, char *src, size_t size);
107 void xstrncat(char *dest, char *src, size_t size);
108 void _xexit(void) noreturn;
109 void xexit(void) noreturn;
110 void *xmalloc(size_t size);
111 void *xzalloc(size_t size);
112 void *xrealloc(void *ptr, size_t size);
113 char *xstrndup(char *s, size_t n);
114 char *xstrdup(char *s);
115 void *xmemdup(void *s, long len);
116 char *xmprintf(char *format, ...) printf_format;
117 void xprintf(char *format, ...) printf_format;
121 void xexec(char **argv);
122 pid_t xpopen_both(char **argv, int *pipes);
123 int xwaitpid(pid_t pid);
124 int xpclose_both(pid_t pid, int *pipes);
125 pid_t xpopen(char **argv, int *pipe, int stdout);
126 pid_t xpclose(pid_t pid, int pipe);
127 int xrun(char **argv);
128 int xpspawn(char **argv, int*pipes);
129 void xaccess(char *path, int flags);
130 void xunlink(char *path);
131 int xcreate(char *path, int flags, int mode);
132 int xopen(char *path, int flags);
136 FILE *xfdopen(int fd, char *mode);
137 FILE *xfopen(char *path, char *mode);
138 size_t xread(int fd, void *buf, size_t len);
139 void xreadall(int fd, void *buf, size_t len);
140 void xwrite(int fd, void *buf, size_t len);
141 off_t xlseek(int fd, off_t offset, int whence);
142 char *xreadfile(char *name, char *buf, off_t len);
143 int xioctl(int fd, int request, void *data);
145 void xstat(char *path, struct stat *st);
146 char *xabspath(char *path, int exact);
147 void xchdir(char *path);
148 void xchroot(char *path);
149 struct passwd *xgetpwuid(uid_t uid);
150 struct group *xgetgrgid(gid_t gid);
151 struct passwd *xgetpwnam(char *name);
152 struct group *xgetgrnam(char *name);
153 struct passwd *xgetpwnamid(char *user);
154 struct group *xgetgrnamid(char *group);
155 void xsetuser(struct passwd *pwd);
156 char *xreadlink(char *name);
157 long xparsetime(char *arg, long units, long *fraction);
158 void xpidfile(char *name);
159 void xregcomp(regex_t *preg, char *rexec, int cflags);
160 char *xtzset(char *new);
161 void xsignal(int signal, void *handler);
164 void verror_msg(char *msg, int err, va_list va);
165 void error_msg(char *msg, ...) printf_format;
166 void perror_msg(char *msg, ...) printf_format;
167 void error_exit(char *msg, ...) printf_format noreturn;
168 void perror_exit(char *msg, ...) printf_format noreturn;
169 void help_exit(char *msg, ...) printf_format noreturn;
170 void error_msg_raw(char *msg);
171 void perror_msg_raw(char *msg);
172 void error_exit_raw(char *msg);
173 void perror_exit_raw(char *msg);
174 ssize_t readall(int fd, void *buf, size_t len);
175 ssize_t writeall(int fd, void *buf, size_t len);
176 off_t lskip(int fd, off_t offset);
177 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
178 struct string_list **splitpath(char *path, struct string_list **list);
179 char *readfileat(int dirfd, char *name, char *buf, off_t *len);
180 char *readfile(char *name, char *buf, off_t len);
181 void msleep(long miliseconds);
182 int64_t peek_le(void *ptr, unsigned size);
183 int64_t peek_be(void *ptr, unsigned size);
184 int64_t peek(void *ptr, unsigned size);
185 void poke(void *ptr, uint64_t val, int size);
186 struct string_list *find_in_path(char *path, char *filename);
187 long estrtol(char *str, char **end, int base);
188 long xstrtol(char *str, char **end, int base);
190 long atolx_range(char *numstr, long low, long high);
191 int stridx(char *haystack, char needle);
192 char *strlower(char *s);
193 char *strafter(char *haystack, char *needle);
194 char *chomp(char *s);
195 int unescape(char c);
196 int strstart(char **a, char *b);
197 off_t fdlength(int fd);
198 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
199 void (*function)(int fd, char *name));
200 void loopfiles(char **argv, void (*function)(int fd, char *name));
201 void xsendfile(int in, int out);
202 int wfchmodat(int rc, char *name, mode_t mode);
203 int copy_tempfile(int fdin, char *name, char **tempname);
204 void delete_tempfile(int fdin, int fdout, char **tempname);
205 void replace_tempfile(int fdin, int fdout, char **tempname);
206 void crc_init(unsigned int *crc_table, int little_endian);
207 void base64_init(char *p);
209 int qstrcmp(const void *a, const void *b);
210 void create_uuid(char *uuid);
211 char *show_uuid(char *uuid);
212 char *next_printf(char *s, char **start);
213 char *strnstr(char *line, char *str);
214 int dev_minor(int dev);
215 int dev_major(int dev);
216 int dev_makedev(int major, int minor);
217 struct passwd *bufgetpwuid(uid_t uid);
218 struct group *bufgetgrgid(gid_t gid);
219 int readlinkat0(int dirfd, char *path, char *buf, int len);
220 int readlink0(char *path, char *buf, int len);
222 #define HR_SPACE 1 // Space between number and units
223 #define HR_B 2 // Use "B" for single byte units
224 #define HR_1000 4 // Use decimal instead of binary units
225 int human_readable(char *buf, unsigned long long num, int style);
231 struct ptr_len idx[];
234 void linestack_addstack(struct linestack **lls, struct linestack *throw,
236 void linestack_insert(struct linestack **lls, long pos, char *line, long len);
237 void linestack_append(struct linestack **lls, char *line);
238 struct linestack *linestack_load(char *name);
239 int crunch_escape(FILE *out, int cols, int wc);
240 int crunch_rev_escape(FILE *out, int cols, int wc);
241 int crunch_str(char **str, int width, FILE *out, char *escmore,
242 int (*escout)(FILE *out, int cols, int wc));
243 int draw_str(char *start, int width);
244 int utf8len(char *str);
245 int utf8skip(char *str, int width);
246 int draw_trim_esc(char *str, int padto, int width, char *escmore,
247 int (*escout)(FILE *out, int cols,int wc));
248 int draw_trim(char *str, int padto, int width);
250 // interestingtimes.c
252 int terminal_size(unsigned *xx, unsigned *yy);
253 int terminal_probesize(unsigned *xx, unsigned *yy);
254 int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy);
255 int set_terminal(int fd, int raw, struct termios *old);
256 void xset_terminal(int fd, int raw, struct termios *old);
257 int scan_key(char *scratch, int miliwait);
258 void tty_esc(char *s);
259 void tty_jump(int x, int y);
260 void tty_reset(void);
261 void tty_sigreset(int i);
264 int xsocket(int domain, int type, int protocol);
265 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
266 int xconnect(char *host, char *port, int family, int socktype, int protocol,
268 int xpoll(struct pollfd *fds, int nfds, int timeout);
271 int get_salt(char *salt, char * algo);
275 struct mtab_list *next, *prev;
277 struct statvfs statvfs;
284 void comma_args(struct arg_list *al, void *data, char *err,
285 char *(*callback)(void *data, char *str, int len));
286 void comma_collate(char **old, char *new);
287 char *comma_iterate(char **list, int *len);
288 int comma_scan(char *optlist, char *opt, int clean);
289 int comma_scanall(char *optlist, char *scanlist);
290 int mountlist_istype(struct mtab_list *ml, char *typelist);
291 struct mtab_list *xgetmountlist(char *path);
295 void generic_signal(int signal);
296 void sigatexit(void *handler);
297 int sig_to_num(char *pidstr);
298 char *num_to_sig(int sig);
300 mode_t string_to_mode(char *mode_str, mode_t base);
301 void mode_to_string(mode_t mode, char *buf);
302 char *getbasename(char *name);
303 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
305 pid_t xvforkwrap(pid_t pid);
306 #define XVFORK() xvforkwrap(vfork())
308 // Wrapper to make xfuncs() return (via longjmp) instead of exiting.
309 // Assigns true/false "did it exit" value to first argument.
310 #define WOULD_EXIT(y, x) do { jmp_buf _noexit; \
312 toys.rebound = &_noexit; \
313 _noexit_res = setjmp(_noexit); \
314 if (!_noexit_res) do {x;} while(0); \
319 // Wrapper that discards true/false "did it exit" value.
320 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
322 // Functions in need of further review/cleanup
323 #include "lib/pending.h"