• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* lib.h - header file for lib directory
2  *
3  * Copyright 2006 Rob Landley <rob@landley.net>
4  */
5 
6 // llist.c
7 
8 // All these list types can be handled by the same code because first element
9 // is always next pointer, so next = (mytype *)&struct. (The payloads are
10 // named differently to catch using the wrong type early.)
11 
12 struct string_list {
13   struct string_list *next;
14   char str[];
15 };
16 
17 struct arg_list {
18   struct arg_list *next;
19   char *arg;
20 };
21 
22 struct double_list {
23   struct double_list *next, *prev;
24   char *data;
25 };
26 
27 struct dev_ino {
28   dev_t dev;
29   ino_t ino;
30 };
31 
32 void llist_free_arg(void *node);
33 void llist_free_double(void *node);
34 void llist_traverse(void *list, void (*using)(void *node));
35 void *llist_pop(void *list);  // actually void **list
36 void *dlist_pop(void *list);  // actually struct double_list **list
37 void *dlist_lpop(void *list); // also struct double_list **list
38 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
39 struct double_list *dlist_add(struct double_list **list, char *data);
40 void *dlist_terminate(void *list);
41 
42 // args.c
43 #define FLAGS_NODASH (1LL<<63)
44 void get_optflags(void);
45 
46 // dirtree.c
47 
48 // Values returnable from callback function (bitfield, or them together)
49 // Default with no callback is 0
50 
51 // Add this node to the tree
52 #define DIRTREE_SAVE         1
53 // Recurse into children
54 #define DIRTREE_RECURSE      2
55 // Call again after handling all children of this directory
56 // (Ignored for non-directories, sets linklen = -1 before second call.)
57 #define DIRTREE_COMEAGAIN    4
58 // Follow symlinks to directories
59 #define DIRTREE_SYMFOLLOW    8
60 // Don't warn about failure to stat
61 #define DIRTREE_SHUTUP      16
62 // Breadth first traversal, conserves filehandles at the expense of memory
63 #define DIRTREE_BREADTH     32 // TODO not implemented yet
64 // skip non-numeric entries
65 #define DIRTREE_PROC        64
66 // Return files we can't stat
67 #define DIRTREE_STATLESS   128
68 // Don't look at any more files in this directory.
69 #define DIRTREE_ABORT      256
70 
71 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
72 
73 struct dirtree {
74   struct dirtree *next, *parent, *child;
75   long extra; // place for user to store their stuff (can be pointer)
76   char *symlink;
77   int dirfd;
78   struct stat st;
79   char again, name[];
80 };
81 
82 int isdotdot(char *name);
83 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags);
84 char *dirtree_path(struct dirtree *node, int *plen);
85 int dirtree_notdotdot(struct dirtree *catch);
86 int dirtree_parentfd(struct dirtree *node);
87 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
88   int dirfd, int symfollow);
89 struct dirtree *dirtree_flagread(char *path, int flags,
90   int (*callback)(struct dirtree *node));
91 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
92 
93 // Tell xopen and friends to print warnings but return -1 as necessary
94 // The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
95 // plenty of headroom.
96 #define WARN_ONLY        (1<<31) // don't exit, just warn
97 #define LOOPFILES_ANYWAY (1<<30) // call function with fd -1
98 
99 // xabspath flags
100 #define ABS_PATH 1 // all but last path component must exist
101 #define ABS_FILE 2 // last path component must exist
102 #define ABS_KEEP 4 // don't resolve symlinks in path to last component
103 #define ABS_LAST 8 // don't resolve symlink in last path component
104 
105 // xwrap.c
106 void xstrncpy(char *dest, char *src, size_t size);
107 void xstrncat(char *dest, char *src, size_t size);
108 _Noreturn void _xexit(void);
109 _Noreturn void xexit(void);
110 void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off);
111 void *xmalloc(size_t size);
112 void *xzalloc(size_t size);
113 void *xrealloc(void *ptr, size_t size);
114 char *xstrndup(char *s, size_t n);
115 char *xstrdup(char *s);
116 void *xmemdup(void *s, long len);
117 char *xmprintf(char *format, ...) printf_format;
118 void xflush(int flush);
119 void xprintf(char *format, ...) printf_format;
120 void xputsl(char *s, int len);
121 void xputsn(char *s);
122 void xputs(char *s);
123 void xputc(char c);
124 void xvdaemon(void);
125 void xexec(char **argv);
126 pid_t xpopen_setup(char **argv, int *pipes, void (*callback)(char **argv));
127 pid_t xpopen_both(char **argv, int *pipes);
128 int xwaitpid(pid_t pid);
129 int xpclose_both(pid_t pid, int *pipes);
130 pid_t xpopen(char **argv, int *pipe, int isstdout);
131 pid_t xpclose(pid_t pid, int pipe);
132 int xrun(char **argv);
133 char *xrunread(char *argv[], char *to_stdin);
134 int xpspawn(char **argv, int*pipes);
135 void xaccess(char *path, int flags);
136 void xunlink(char *path);
137 void xrename(char *from, char *to);
138 int xtempfile(char *name, char **tempname);
139 int xcreate(char *path, int flags, int mode);
140 int xopen(char *path, int flags);
141 int xcreate_stdio(char *path, int flags, int mode);
142 int xopen_stdio(char *path, int flags);
143 int openro(char *path, int flags);
144 int xopenro(char *path);
145 void xpipe(int *pp);
146 void xclose(int fd);
147 int xdup(int fd);
148 int xnotstdio(int fd);
149 FILE *xfdopen(int fd, char *mode);
150 FILE *xfopen(char *path, char *mode);
151 size_t xread(int fd, void *buf, size_t len);
152 void xreadall(int fd, void *buf, size_t len);
153 void xwrite(int fd, void *buf, size_t len);
154 off_t xlseek(int fd, off_t offset, int whence);
155 char *xreadfile(char *name, char *buf, off_t len);
156 int xioctl(int fd, int request, void *data);
157 char *xgetcwd(void);
158 void xstat(char *path, struct stat *st);
159 char *xabspath(char *path, int exact);
160 void xchdir(char *path);
161 void xchroot(char *path);
162 struct passwd *xgetpwuid(uid_t uid);
163 struct group *xgetgrgid(gid_t gid);
164 struct passwd *xgetpwnam(char *name);
165 struct group *xgetgrnam(char *name);
166 unsigned xgetuid(char *name);
167 unsigned xgetgid(char *name);
168 void xsetuser(struct passwd *pwd);
169 char *xreadlinkat(int dir, char *name);
170 char *xreadlink(char *name);
171 double xstrtod(char *s);
172 long xparsetime(char *arg, long units, long *fraction);
173 void xparsetimespec(char *arg, struct timespec *ts);
174 long long xparsemillitime(char *arg);
175 void xpidfile(char *name);
176 void xregcomp(regex_t *preg, char *rexec, int cflags);
177 char *xtzset(char *new);
178 void xsignal_flags(int signal, void *handler, int flags);
179 void xsignal(int signal, void *handler);
180 time_t xvali_date(struct tm *tm, char *str);
181 void xparsedate(char *str, time_t *t, unsigned *nano, int endian);
182 char *xgetline(FILE *fp);
183 time_t xmktime(struct tm *tm, int utc);
184 
185 // lib.c
186 void verror_msg(char *msg, int err, va_list va);
187 void error_msg(char *msg, ...) printf_format;
188 void perror_msg(char *msg, ...) printf_format;
189 _Noreturn void error_exit(char *msg, ...) printf_format;
190 _Noreturn void perror_exit(char *msg, ...) printf_format;
191 _Noreturn void help_exit(char *msg, ...) printf_format;
192 void error_msg_raw(char *msg);
193 void perror_msg_raw(char *msg);
194 _Noreturn void error_exit_raw(char *msg);
195 _Noreturn void perror_exit_raw(char *msg);
196 ssize_t readall(int fd, void *buf, size_t len);
197 ssize_t writeall(int fd, void *buf, size_t len);
198 off_t lskip(int fd, off_t offset);
199 #define MKPATHAT_MKLAST  1
200 #define MKPATHAT_MAKE    2
201 #define MKPATHAT_VERBOSE 4
202 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
203 int mkpath(char *dir);
204 struct string_list **splitpath(char *path, struct string_list **list);
205 char *readfd(int fd, char *ibuf, off_t *plen);
206 char *readfileat(int dirfd, char *name, char *buf, off_t *len);
207 char *readfile(char *name, char *buf, off_t len);
208 void msleep(long milliseconds);
209 void nanomove(struct timespec *ts, long long offset);
210 long long nanodiff(struct timespec *old, struct timespec *new);
211 int highest_bit(unsigned long l);
212 int64_t peek_le(void *ptr, unsigned size);
213 int64_t peek_be(void *ptr, unsigned size);
214 int64_t peek(void *ptr, unsigned size);
215 void poke_le(void *ptr, long long val, unsigned size);
216 void poke_be(void *ptr, long long val, unsigned size);
217 void poke(void *ptr, long long val, unsigned size);
218 struct string_list *find_in_path(char *path, char *filename);
219 long long estrtol(char *str, char **end, int base);
220 long long xstrtol(char *str, char **end, int base);
221 long long atolx(char *c);
222 long long atolx_range(char *numstr, long long low, long long high);
223 int stridx(char *haystack, char needle);
224 int wctoutf8(char *s, unsigned wc);
225 int utf8towc(unsigned *wc, char *str, unsigned len);
226 char *strlower(char *s);
227 char *strafter(char *haystack, char *needle);
228 char *chomp(char *s);
229 int unescape(char c);
230 int unescape2(char **c, int echo);
231 char *strend(char *str, char *suffix);
232 int strstart(char **a, char *b);
233 int strcasestart(char **a, char *b);
234 int same_file(struct stat *st1, struct stat *st2);
235 int same_dev_ino(struct stat *st, struct dev_ino *di);
236 off_t fdlength(int fd);
237 void loopfiles_rw(char **argv, int flags, int permissions,
238   void (*function)(int fd, char *name));
239 void loopfiles(char **argv, void (*function)(int fd, char *name));
240 void loopfiles_lines(char **argv, void (*function)(char **pline, long len));
241 long long sendfile_len(int in, int out, long long len, long long *consumed);
242 long long xsendfile_len(int in, int out, long long len);
243 void xsendfile_pad(int in, int out, long long len);
244 long long xsendfile(int in, int out);
245 int wfchmodat(int rc, char *name, mode_t mode);
246 int copy_tempfile(int fdin, char *name, char **tempname);
247 void delete_tempfile(int fdin, int fdout, char **tempname);
248 void replace_tempfile(int fdin, int fdout, char **tempname);
249 void crc_init(unsigned *crc_table, int little_endian);
250 void base64_init(char *p);
251 int yesno(int def);
252 int fyesno(FILE *fp, int def);
253 int qstrcmp(const void *a, const void *b);
254 void create_uuid(char *uuid);
255 char *show_uuid(char *uuid);
256 char *next_printf(char *s, char **start);
257 struct passwd *bufgetpwnamuid(char *name, uid_t uid);
258 struct passwd *bufgetpwuid(uid_t uid);
259 struct group *bufgetgrnamgid(char *name, gid_t gid);
260 struct group *bufgetgrgid(gid_t gid);
261 int readlinkat0(int dirfd, char *path, char *buf, int len);
262 int readlink0(char *path, char *buf, int len);
263 int regexec0(regex_t *preg, char *string, long len, int nmatch,
264   regmatch_t pmatch[], int eflags);
265 char *getusername(uid_t uid);
266 char *getgroupname(gid_t gid);
267 void do_lines(int fd, char delim, void (*call)(char **pline, long len));
268 long long millitime(void);
269 char *format_iso_time(char *buf, size_t len, struct timespec *ts);
270 void loggit(int priority, char *format, ...);
271 unsigned tar_cksum(void *data);
272 int is_tar_header(void *pkt);
273 char *elf_arch_name(int type);
274 void octal_deslash(char *s);
275 int smemcmp(char *one, char *two, unsigned long len);
276 
277 #define HR_SPACE  1 // Space between number and units
278 #define HR_B      2 // Use "B" for single byte units
279 #define HR_1000   4 // Use decimal instead of binary units
280 #define HR_NODOT  8 // No tenths for single digit units
281 int human_readable_long(char *buf, unsigned long long num, int dgt, int unit,
282   int style);
283 int human_readable(char *buf, unsigned long long num, int style);
284 
285 // env.c
286 
287 long environ_bytes(void);
288 char *xsetenv(char *name, char *val);
289 void xunsetenv(char *name);
290 char *xpop_env(char *name); // because xpopenv() looks like xpopen_v()
291 void xclearenv(void);
292 void reset_env(struct passwd *p, int clear);
293 
294 // utf8.c
295 
296 int crunch_escape(FILE *out, int cols, int wc);
297 int crunch_rev_escape(FILE *out, int cols, int wc);
298 int crunch_str(char **str, int width, FILE *out, char *escmore,
299   int (*escout)(FILE *out, int cols, int wc));
300 int draw_str(char *start, int width);
301 int utf8len(char *str);
302 int utf8skip(char *str, int width);
303 int draw_trim_esc(char *str, int padto, int width, char *escmore,
304   int (*escout)(FILE *out, int cols,int wc));
305 int draw_trim(char *str, int padto, int width);
306 
307 // tty.c
308 int tty_fd(void);
309 int terminal_size(unsigned *xx, unsigned *yy);
310 int terminal_probesize(unsigned *xx, unsigned *yy);
311 #define KEY_UP 0
312 #define KEY_DOWN 1
313 #define KEY_RIGHT 2
314 #define KEY_LEFT 3
315 #define KEY_PGUP 4
316 #define KEY_PGDN 5
317 #define KEY_HOME 6
318 #define KEY_END 7
319 #define KEY_INSERT 8
320 #define KEY_DELETE 9
321 #define KEY_FN 10 // F1 = KEY_FN+1, F2 = KEY_FN+2, ...
322 #define KEY_SHIFT (1<<16)
323 #define KEY_CTRL (1<<17)
324 #define KEY_ALT (1<<18)
325 int scan_key(char *scratch, int timeout_ms);
326 int scan_key_getsize(char *scratch, int timeout_ms, unsigned *xx, unsigned *yy);
327 void xsetspeed(struct termios *tio, int speed);
328 int set_terminal(int fd, int raw, int speed, struct termios *old);
329 void xset_terminal(int fd, int raw, int speed, struct termios *old);
330 void tty_reset(void);
331 void tty_sigreset(int i);
332 void start_redraw(unsigned *width, unsigned *height);
333 
334 // net.c
335 
336 union socksaddr {
337   struct sockaddr s;
338   struct sockaddr_in in;
339   struct sockaddr_in6 in6;
340 };
341 
342 int xsocket(int domain, int type, int protocol);
343 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
344 struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
345   int protocol, int flags);
346 void xbind(int fd, const struct sockaddr *sa, socklen_t len);
347 void xconnect(int fd, const struct sockaddr *sa, socklen_t len);
348 int xconnectany(struct addrinfo *ai);
349 int xbindany(struct addrinfo *ai);
350 int xpoll(struct pollfd *fds, int nfds, int timeout);
351 int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
352 char *ntop(struct sockaddr *sa);
353 void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest);
354 int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout);
355 char *escape_url(char *str, char *and);
356 char *unescape_url(char *str, int do_cut);
357 
358 // password.c
359 int get_salt(char *salt, char * algo);
360 
361 // commas.c
362 void comma_args(struct arg_list *al, void *data, char *err,
363   char *(*callback)(void *data, char *str, int len));
364 void comma_collate(char **old, char *new);
365 char *comma_iterate(char **list, int *len);
366 int comma_scan(char *optlist, char *opt, int clean);
367 int comma_scanall(char *optlist, char *scanlist);
368 int comma_remove(char *optlist, char *opt);
369 
370 // deflate.c
371 
372 long long gzip_fd(int infd, int outfd);
373 long long gunzip_fd(int infd, int outfd);
374 long long gunzip_fd_preload(int infd, int outfd, char *buf, unsigned len);
375 
376 
377 // getmountlist.c
378 struct mtab_list {
379   struct mtab_list *next, *prev;
380   struct stat stat;
381   struct statvfs statvfs;
382   char *dir;
383   char *device;
384   char *opts;
385   char type[0];
386 };
387 
388 int mountlist_istype(struct mtab_list  *ml, char *typelist);
389 struct mtab_list *xgetmountlist(char *path);
390 
391 // signal
392 
393 void generic_signal(int signal);
394 void exit_signal(int signal);
395 void sigatexit(void *handler);
396 void list_signals(void);
397 
398 mode_t string_to_mode(char *mode_str, mode_t base);
399 void mode_to_string(mode_t mode, char *buf);
400 char *getbasename(char *name);
401 char *fileunderdir(char *file, char *dir);
402 void *mepcpy(void *from, void *to, unsigned long len);
403 char *relative_path(char *from, char *to, int abs);
404 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name),
405     int scripts);
406 
407 pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid);
408 #define XVFORK() xvforkwrap(vfork())
409 
410 // Wrapper to make xfuncs() return (via siglongjmp) instead of exiting.
411 // Assigns true/false "did it exit" value to first argument.
412 #define WOULD_EXIT(y, x) do { sigjmp_buf _noexit; \
413   int _noexit_res; \
414   toys.rebound = &_noexit; \
415   _noexit_res = sigsetjmp(_noexit, 1); \
416   if (!_noexit_res) do {x;} while(0); \
417   toys.rebound = 0; \
418   y = _noexit_res; \
419 } while(0)
420 
421 // Wrapper that discards true/false "did it exit" value.
422 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
423 
424 #define minof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa<bb ? aa : bb;})
425 #define maxof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa>bb ? aa : bb;})
426 
427 // Functions in need of further review/cleanup
428 #include "lib/pending.h"
429