blob: 0b93bde9b6b43db151f1b786f2d5524d49f6eb94 [file] [log] [blame]
landley09ea7ac2006-10-30 01:38:00 -05001/* lib.h - header file for lib directory
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 */
landley4f344e32006-10-05 16:18:03 -04005
Rob Landleye32e8022015-12-23 18:16:23 -06006struct ptr_len {
7 void *ptr;
8 long len;
9};
10
11struct str_len {
12 char *str;
13 long len;
14};
15
Rob Landley0a04b3e2006-11-03 00:05:52 -050016// llist.c
Rob Landley0a04b3e2006-11-03 00:05:52 -050017
Rob Landleyeb7ea222012-04-14 22:30:41 -050018// All these list types can be handled by the same code because first element
Rob Landleye5f3a0b2013-03-17 17:57:28 -050019// is always next pointer, so next = (mytype *)&struct. (The payloads are
20// named differently to catch using the wrong type early.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050021
Rob Landley0a04b3e2006-11-03 00:05:52 -050022struct string_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060023 struct string_list *next;
24 char str[0];
Rob Landley0a04b3e2006-11-03 00:05:52 -050025};
26
Rob Landley8324b892006-11-19 02:49:22 -050027struct arg_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060028 struct arg_list *next;
29 char *arg;
Rob Landley8324b892006-11-19 02:49:22 -050030};
31
Rob Landleybc078652007-12-15 21:47:25 -060032struct double_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060033 struct double_list *next, *prev;
34 char *data;
Rob Landleybc078652007-12-15 21:47:25 -060035};
36
Rob Landley57dafe32016-06-19 07:07:44 -050037struct num_cache {
38 struct num_cache *next;
39 long long num;
40 char data[];
41};
42
Rob Landleye604d532014-05-21 06:57:43 -050043void llist_free_arg(void *node);
44void llist_free_double(void *node);
45void llist_traverse(void *list, void (*using)(void *node));
Rob Landley5f57bcc2013-09-09 04:26:03 -050046void *llist_pop(void *list); // actually void **list
47void *dlist_pop(void *list); // actually struct double_list **list
Rob Landley2c482472012-03-12 00:25:40 -050048void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
Rob Landleybdf037f2008-10-23 16:44:30 -050049struct double_list *dlist_add(struct double_list **list, char *data);
Rob Landleydc640252014-05-29 05:22:02 -050050void *dlist_terminate(void *list);
Rob Landley57dafe32016-06-19 07:07:44 -050051struct num_cache *get_num_cache(struct num_cache *cache, long long num);
52struct num_cache *add_num_cache(struct num_cache **cache, long long num,
53 void *data, int len);
Rob Landley6ef04ef2008-01-20 17:34:53 -060054
Rob Landley103b7e02007-10-04 02:04:10 -050055// args.c
56void get_optflags(void);
57
58// dirtree.c
Rob Landleyeb7ea222012-04-14 22:30:41 -050059
60// Values returnable from callback function (bitfield, or them together)
61// Default with no callback is 0
62
Rob Landley8c4ae8a2012-05-20 15:00:19 -050063// 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.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050069#define DIRTREE_COMEAGAIN 4
70// Follow symlinks to directories
71#define DIRTREE_SYMFOLLOW 8
Rob Landleyaab91642015-05-09 18:11:22 -050072// Don't warn about failure to stat
73#define DIRTREE_SHUTUP 16
Rob Landley8d950742016-03-07 16:02:47 -060074// Breadth first traversal, conserves filehandles at the expense of memory
75#define DIRTREE_BREADTH 32
Rob Landleya9759522016-12-08 21:29:00 -060076// skip non-numeric entries
77#define DIRTREE_PROC 64
Rob Landley8c4ae8a2012-05-20 15:00:19 -050078// Don't look at any more files in this directory.
79#define DIRTREE_ABORT 256
Rob Landleyeb7ea222012-04-14 22:30:41 -050080
81#define DIRTREE_ABORTVAL ((struct dirtree *)1)
82
Rob Landleyd25f7e42007-02-03 14:11:26 -050083struct dirtree {
Rob Landley7aa651a2012-11-13 17:14:08 -060084 struct dirtree *next, *parent, *child;
85 long extra; // place for user to store their stuff (can be pointer)
86 struct stat st;
87 char *symlink;
Rob Landleyd336af42015-12-06 20:36:59 -060088 int dirfd;
Rob Landleyfec3fd12014-07-26 13:30:40 -050089 char again;
Rob Landley7aa651a2012-11-13 17:14:08 -060090 char name[];
Rob Landleyd25f7e42007-02-03 14:11:26 -050091};
92
Rob Landley382057f2016-11-21 16:47:23 -060093int isdotdot(char *name);
Rob Landleyaab91642015-05-09 18:11:22 -050094struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags);
Rob Landleyeb7ea222012-04-14 22:30:41 -050095char *dirtree_path(struct dirtree *node, int *plen);
Rob Landley8c4ae8a2012-05-20 15:00:19 -050096int dirtree_notdotdot(struct dirtree *catch);
Rob Landley6ec21782012-06-16 15:16:08 -050097int dirtree_parentfd(struct dirtree *node);
Rob Landleyfec3fd12014-07-26 13:30:40 -050098int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
Rob Landleybb77dde2016-04-21 17:46:25 -050099 int dirfd, int symfollow);
Rob Landley8d950742016-03-07 16:02:47 -0600100struct dirtree *dirtree_flagread(char *path, int flags,
101 int (*callback)(struct dirtree *node));
Rob Landleyeb7ea222012-04-14 22:30:41 -0500102struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
Rob Landley8324b892006-11-19 02:49:22 -0500103
Rob Landley36ffc5a2013-04-14 21:43:22 -0500104// help.c
105
Rob Landleye5354ca2015-09-11 16:35:14 -0500106void show_help(FILE *out);
Rob Landley36ffc5a2013-04-14 21:43:22 -0500107
Rob Landley299d4382016-09-04 17:26:34 -0500108// Tell xopen and friends to print warnings but return -1 as necessary
109// The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
110// plenty of headroom.
111#define WARN_ONLY (1<<31)
112
Rob Landley72756672013-07-17 17:22:46 -0500113// xwrap.c
Rob Landleybe93c912013-04-20 23:33:48 -0500114void xstrncpy(char *dest, char *src, size_t size);
Rob Landley2fb85a32014-12-04 21:41:12 -0600115void xstrncat(char *dest, char *src, size_t size);
Rob Landleyeb24df92016-03-13 20:23:41 -0500116void _xexit(void) noreturn;
Rob Landley55830302013-06-16 19:59:51 -0500117void xexit(void) noreturn;
Elliott Hughes12f07442017-05-23 17:35:49 -0700118void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off);
landley4f344e32006-10-05 16:18:03 -0400119void *xmalloc(size_t size);
landleycd9dfc32006-10-18 18:38:16 -0400120void *xzalloc(size_t size);
Rob Landley0c93f6c2007-04-29 19:55:21 -0400121void *xrealloc(void *ptr, size_t size);
Rob Landley1e01cd12010-01-05 10:48:32 -0600122char *xstrndup(char *s, size_t n);
123char *xstrdup(char *s);
Rob Landley8f7137e2016-01-28 13:36:12 -0600124void *xmemdup(void *s, long len);
Elliott Hughes1be99e62015-03-01 16:16:50 -0600125char *xmprintf(char *format, ...) printf_format;
126void xprintf(char *format, ...) printf_format;
Rob Landley5084fea2007-06-18 00:14:03 -0400127void xputs(char *s);
Rob Landley24d1d452007-01-20 18:04:20 -0500128void xputc(char c);
129void xflush(void);
landley09ea7ac2006-10-30 01:38:00 -0500130void xexec(char **argv);
Rob Landley360d57f2014-09-14 12:29:44 -0500131pid_t xpopen_both(char **argv, int *pipes);
Rob Landley3b51a072015-09-27 09:03:41 -0500132int xwaitpid(pid_t pid);
Rob Landley360d57f2014-09-14 12:29:44 -0500133int xpclose_both(pid_t pid, int *pipes);
Rob Landleyea9dd8a2017-02-04 14:55:36 -0600134pid_t xpopen(char **argv, int *pipe, int isstdout);
Rob Landley360d57f2014-09-14 12:29:44 -0500135pid_t xpclose(pid_t pid, int pipe);
136int xrun(char **argv);
137int xpspawn(char **argv, int*pipes);
Rob Landleyd3e9d642007-01-08 03:25:47 -0500138void xaccess(char *path, int flags);
Rob Landleye745d8e2007-12-20 06:30:19 -0600139void xunlink(char *path);
Rob Landley1322beb2007-01-07 22:51:12 -0500140int xcreate(char *path, int flags, int mode);
141int xopen(char *path, int flags);
Rob Landley027a73a2016-08-04 10:16:59 -0500142int xcreate_stdio(char *path, int flags, int mode);
143int xopen_stdio(char *path, int flags);
Rob Landley299d4382016-09-04 17:26:34 -0500144int openro(char *path, int flags);
Rob Landley027a73a2016-08-04 10:16:59 -0500145int xopenro(char *path);
Rob Landleya7a869c2016-02-09 17:06:12 -0600146void xpipe(int *pp);
Rob Landleybc078652007-12-15 21:47:25 -0600147void xclose(int fd);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500148int xdup(int fd);
Rob Landley7f7907f2016-09-05 00:52:44 -0500149int notstdio(int fd);
Rob Landley1aa75112013-08-07 12:19:51 -0500150FILE *xfdopen(int fd, char *mode);
landley4f344e32006-10-05 16:18:03 -0400151FILE *xfopen(char *path, char *mode);
Rob Landley90163772007-01-18 21:54:08 -0500152size_t xread(int fd, void *buf, size_t len);
153void xreadall(int fd, void *buf, size_t len);
154void xwrite(int fd, void *buf, size_t len);
Rob Landley52476712009-01-18 16:19:25 -0600155off_t xlseek(int fd, off_t offset, int whence);
Rob Landleydc373172013-12-27 18:45:01 -0600156char *xreadfile(char *name, char *buf, off_t len);
Rob Landleyf0153442013-04-26 02:41:05 -0500157int xioctl(int fd, int request, void *data);
landley00f87f12006-10-25 18:38:37 -0400158char *xgetcwd(void);
Rob Landleyd25f7e42007-02-03 14:11:26 -0500159void xstat(char *path, struct stat *st);
Rob Landleyfe91e682012-11-22 21:18:09 -0600160char *xabspath(char *path, int exact);
Rob Landley988abb32008-05-12 00:52:27 -0500161void xchdir(char *path);
Rob Landleyafba5b82013-12-23 06:49:38 -0600162void xchroot(char *path);
Rob Landley9e44a582013-11-28 20:18:04 -0600163struct passwd *xgetpwuid(uid_t uid);
164struct group *xgetgrgid(gid_t gid);
Rob Landley5ec4ab32013-11-28 21:06:15 -0600165struct passwd *xgetpwnam(char *name);
Rob Landley60c35c42014-08-03 15:50:10 -0500166struct group *xgetgrnam(char *name);
Rob Landley3d64b0c2016-08-18 21:33:27 -0500167unsigned xgetuid(char *name);
168unsigned xgetgid(char *name);
Rob Landleyafba5b82013-12-23 06:49:38 -0600169void xsetuser(struct passwd *pwd);
Rob Landleyd3904932013-07-16 00:04:56 -0500170char *xreadlink(char *name);
Rob Landley72756672013-07-17 17:22:46 -0500171long xparsetime(char *arg, long units, long *fraction);
Felix Jandadccfb2a2013-08-26 21:55:33 +0200172void xpidfile(char *name);
Rob Landley5b405822014-03-29 18:11:00 -0500173void xregcomp(regex_t *preg, char *rexec, int cflags);
Rob Landleyc277f342015-02-09 16:34:24 -0600174char *xtzset(char *new);
Rob Landleye6abb612015-03-09 14:52:32 -0500175void xsignal(int signal, void *handler);
Rob Landleyd3904932013-07-16 00:04:56 -0500176
177// lib.c
178void verror_msg(char *msg, int err, va_list va);
Elliott Hughes1be99e62015-03-01 16:16:50 -0600179void error_msg(char *msg, ...) printf_format;
180void perror_msg(char *msg, ...) printf_format;
181void error_exit(char *msg, ...) printf_format noreturn;
182void perror_exit(char *msg, ...) printf_format noreturn;
Rob Landleyd3a435e2016-01-05 22:26:58 -0600183void help_exit(char *msg, ...) printf_format noreturn;
184void error_msg_raw(char *msg);
185void perror_msg_raw(char *msg);
186void error_exit_raw(char *msg);
187void perror_exit_raw(char *msg);
Rob Landleyd3904932013-07-16 00:04:56 -0500188ssize_t readall(int fd, void *buf, size_t len);
189ssize_t writeall(int fd, void *buf, size_t len);
190off_t lskip(int fd, off_t offset);
Rob Landleyca1b60e2014-03-11 20:44:55 -0500191int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
Rob Landleyd3904932013-07-16 00:04:56 -0500192struct string_list **splitpath(char *path, struct string_list **list);
Rob Landley12a487b2015-11-26 21:16:12 -0600193char *readfileat(int dirfd, char *name, char *buf, off_t *len);
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500194char *readfile(char *name, char *buf, off_t len);
Rob Landleyd3904932013-07-16 00:04:56 -0500195void msleep(long miliseconds);
Rob Landleyf86f2f42017-05-21 13:11:42 -0500196int highest_bit(unsigned long l);
Rob Landley546b2932014-07-21 19:56:53 -0500197int64_t peek_le(void *ptr, unsigned size);
198int64_t peek_be(void *ptr, unsigned size);
199int64_t peek(void *ptr, unsigned size);
Rob Landleyd3904932013-07-16 00:04:56 -0500200void poke(void *ptr, uint64_t val, int size);
Rob Landley0a04b3e2006-11-03 00:05:52 -0500201struct string_list *find_in_path(char *path, char *filename);
Rob Landley30454a42016-07-12 14:51:51 -0500202long long estrtol(char *str, char **end, int base);
203long long xstrtol(char *str, char **end, int base);
204long long atolx(char *c);
205long long atolx_range(char *numstr, long long low, long long high);
Rob Landley2037b832012-07-15 16:56:20 -0500206int stridx(char *haystack, char needle);
Rob Landley7cc95a72015-08-01 11:48:59 -0500207char *strlower(char *s);
Rob Landley4d3ad672015-12-29 11:52:12 -0600208char *strafter(char *haystack, char *needle);
Elliott Hughes11d60792015-10-31 12:15:25 -0700209char *chomp(char *s);
Rob Landley5fcc7152014-10-18 17:14:12 -0500210int unescape(char c);
Rob Landleyd74b5622017-05-08 23:01:06 -0500211char *strend(char *str, char *suffix);
Rob Landley8115fc12014-06-09 07:12:49 -0500212int strstart(char **a, char *b);
Rob Landleye2580db2007-01-23 13:20:38 -0500213off_t fdlength(int fd);
Rob Landleyeed9ed42016-09-05 00:55:24 -0500214void loopfiles_rw(char **argv, int flags, int permissions,
Rob Landley7aa651a2012-11-13 17:14:08 -0600215 void (*function)(int fd, char *name));
Rob Landley7634b552007-11-29 17:49:50 -0600216void loopfiles(char **argv, void (*function)(int fd, char *name));
Rob Landleya1a559e2017-01-04 01:32:44 -0600217long long xsendfile(int in, int out);
Rob Landley67a069d2012-06-03 00:32:12 -0500218int wfchmodat(int rc, char *name, mode_t mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600219int copy_tempfile(int fdin, char *name, char **tempname);
220void delete_tempfile(int fdin, int fdout, char **tempname);
221void replace_tempfile(int fdin, int fdout, char **tempname);
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600222void crc_init(unsigned int *crc_table, int little_endian);
Rob Landley5bec5ba2014-12-13 11:59:37 -0600223void base64_init(char *p);
Rob Landley960100a2015-09-06 20:10:04 -0500224int yesno(int def);
Rob Landley5b493dc2015-04-19 21:50:51 -0500225int qstrcmp(const void *a, const void *b);
Rob Landley54939162016-01-16 16:59:47 -0600226void create_uuid(char *uuid);
Rob Landleyba868642016-01-17 17:16:03 -0600227char *show_uuid(char *uuid);
Rob Landleycf0f0372016-01-22 21:35:48 -0600228char *next_printf(char *s, char **start);
Rob Landleyf435f042016-02-10 21:05:22 -0600229char *strnstr(char *line, char *str);
Rob Landley7ca5dc42016-03-02 11:52:38 -0600230int dev_minor(int dev);
231int dev_major(int dev);
232int dev_makedev(int major, int minor);
Rob Landley6d50d4c2016-05-17 12:10:07 -0500233struct passwd *bufgetpwuid(uid_t uid);
Rob Landleyb602f1c2016-05-20 14:28:13 -0500234struct group *bufgetgrgid(gid_t gid);
Rob Landley46409d52016-06-15 15:47:01 -0500235int readlinkat0(int dirfd, char *path, char *buf, int len);
236int readlink0(char *path, char *buf, int len);
Rob Landleyf20b10e2016-07-26 13:35:56 -0500237int regexec0(regex_t *preg, char *string, long len, int nmatch,
238 regmatch_t pmatch[], int eflags);
Rob Landleybc1ccac2016-08-13 15:19:29 -0500239char *getusername(uid_t uid);
240char *getgroupname(gid_t gid);
Rob Landley7528a962016-09-03 15:41:55 -0500241void do_lines(int fd, void (*call)(char **pline, long len));
Rob Landley72cd2e02015-05-08 20:20:29 -0500242
Rob Landley960100a2015-09-06 20:10:04 -0500243#define HR_SPACE 1 // Space between number and units
244#define HR_B 2 // Use "B" for single byte units
245#define HR_1000 4 // Use decimal instead of binary units
Rob Landleyd06ea372015-09-03 20:36:44 -0500246int human_readable(char *buf, unsigned long long num, int style);
247
Rob Landleye32e8022015-12-23 18:16:23 -0600248// linestack.c
249
250struct linestack {
251 long len, max;
252 struct ptr_len idx[];
253};
254
255void linestack_addstack(struct linestack **lls, struct linestack *throw,
256 long pos);
257void linestack_insert(struct linestack **lls, long pos, char *line, long len);
258void linestack_append(struct linestack **lls, char *line);
259struct linestack *linestack_load(char *name);
Rob Landley2b999e62016-03-15 02:23:50 -0500260int crunch_escape(FILE *out, int cols, int wc);
261int crunch_rev_escape(FILE *out, int cols, int wc);
262int crunch_str(char **str, int width, FILE *out, char *escmore,
Elliott Hughes9fcaca82016-03-26 10:54:23 -0700263 int (*escout)(FILE *out, int cols, int wc));
Rob Landley544c1ec2016-01-17 14:08:10 -0600264int draw_str(char *start, int width);
Rob Landleyba868642016-01-17 17:16:03 -0600265int utf8len(char *str);
266int utf8skip(char *str, int width);
Rob Landley2b999e62016-03-15 02:23:50 -0500267int draw_trim_esc(char *str, int padto, int width, char *escmore,
268 int (*escout)(FILE *out, int cols,int wc));
Rob Landleyba868642016-01-17 17:16:03 -0600269int draw_trim(char *str, int padto, int width);
Rob Landleye32e8022015-12-23 18:16:23 -0600270
Rob Landley72cd2e02015-05-08 20:20:29 -0500271// interestingtimes.c
Rob Landley5b2644c2015-05-14 13:43:01 -0500272int xgettty(void);
Rob Landley72cd2e02015-05-08 20:20:29 -0500273int terminal_size(unsigned *xx, unsigned *yy);
Rob Landleyefb309d2016-01-07 14:34:47 -0600274int terminal_probesize(unsigned *xx, unsigned *yy);
275int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy);
Rob Landley72cd2e02015-05-08 20:20:29 -0500276int set_terminal(int fd, int raw, struct termios *old);
Rob Landley1b983742016-01-10 15:59:09 -0600277void xset_terminal(int fd, int raw, struct termios *old);
Rob Landleyefb309d2016-01-07 14:34:47 -0600278int scan_key(char *scratch, int miliwait);
Rob Landleyb20c80b2015-06-26 16:26:15 -0500279void tty_esc(char *s);
280void tty_jump(int x, int y);
281void tty_reset(void);
282void tty_sigreset(int i);
283
Rob Landleyf0153442013-04-26 02:41:05 -0500284// net.c
285int xsocket(int domain, int type, int protocol);
Rob Landley5bec5ba2014-12-13 11:59:37 -0600286void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
Rob Landley35dafc72015-08-05 20:32:49 -0500287int xconnect(char *host, char *port, int family, int socktype, int protocol,
Rob Landleyea75e752015-08-03 14:34:01 -0500288 int flags);
Rob Landley54939162016-01-16 16:59:47 -0600289int xpoll(struct pollfd *fds, int nfds, int timeout);
Rob Landley4e867b82016-10-11 08:19:41 -0500290int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
Rob Landleyf0153442013-04-26 02:41:05 -0500291
Rob Landley6d15f0d2014-06-25 22:54:59 -0500292// password.c
293int get_salt(char *salt, char * algo);
294
landleycd9dfc32006-10-18 18:38:16 -0400295// getmountlist.c
landley4f344e32006-10-05 16:18:03 -0400296struct mtab_list {
Rob Landleydc640252014-05-29 05:22:02 -0500297 struct mtab_list *next, *prev;
Rob Landley7aa651a2012-11-13 17:14:08 -0600298 struct stat stat;
299 struct statvfs statvfs;
300 char *dir;
301 char *device;
Rob Landley55e9f352014-05-27 07:56:51 -0500302 char *opts;
Rob Landley7aa651a2012-11-13 17:14:08 -0600303 char type[0];
landley4f344e32006-10-05 16:18:03 -0400304};
305
Rob Landleye32e8022015-12-23 18:16:23 -0600306void comma_args(struct arg_list *al, void *data, char *err,
307 char *(*callback)(void *data, char *str, int len));
Rob Landleye996bdd2014-08-24 23:46:23 -0500308void comma_collate(char **old, char *new);
309char *comma_iterate(char **list, int *len);
310int comma_scan(char *optlist, char *opt, int clean);
311int comma_scanall(char *optlist, char *scanlist);
312int mountlist_istype(struct mtab_list *ml, char *typelist);
Rob Landley42adb7a2013-08-30 17:34:24 -0500313struct mtab_list *xgetmountlist(char *path);
landley4f344e32006-10-05 16:18:03 -0400314
Rob Landley2dd50ad2012-02-26 13:48:00 -0600315// signal
316
Rob Landley1bc52242014-05-21 07:24:16 -0500317void generic_signal(int signal);
Elliott Hughesd5088a02016-07-07 21:48:33 -0700318void exit_signal(int signal);
Rob Landleyc52db602012-07-30 01:01:33 -0500319void sigatexit(void *handler);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600320int sig_to_num(char *pidstr);
321char *num_to_sig(int sig);
Daniel Walter05744b32012-03-19 19:57:56 -0500322
323mode_t string_to_mode(char *mode_str, mode_t base);
Rob Landley5a26a862013-06-02 00:24:24 -0500324void mode_to_string(mode_t mode, char *buf);
Rob Landley1c028ca2016-04-08 18:25:59 -0500325char *getbasename(char *name);
Rob Landleydb1009d2013-12-19 09:32:30 -0600326void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
Rob Landley2c917f52012-07-17 08:54:47 -0500327
Rob Landley938901d2017-02-04 00:34:31 -0600328pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid);
Rob Landley7d6af772015-09-29 05:09:46 -0500329#define XVFORK() xvforkwrap(vfork())
Rob Landley847bcb62015-09-23 22:18:22 -0500330
Rob Landleye32e8022015-12-23 18:16:23 -0600331// Wrapper to make xfuncs() return (via longjmp) instead of exiting.
332// Assigns true/false "did it exit" value to first argument.
Rob Landley12a487b2015-11-26 21:16:12 -0600333#define WOULD_EXIT(y, x) do { jmp_buf _noexit; \
Rob Landley2f69ee22015-11-08 16:47:52 -0600334 int _noexit_res; \
335 toys.rebound = &_noexit; \
336 _noexit_res = setjmp(_noexit); \
337 if (!_noexit_res) do {x;} while(0); \
338 toys.rebound = 0; \
339 y = _noexit_res; \
Rob Landley12a487b2015-11-26 21:16:12 -0600340} while(0);
Rob Landley2f69ee22015-11-08 16:47:52 -0600341
Rob Landleye32e8022015-12-23 18:16:23 -0600342// Wrapper that discards true/false "did it exit" value.
Rob Landley2f69ee22015-11-08 16:47:52 -0600343#define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
344
Rob Landley5a159cc2017-05-23 15:48:26 -0500345#define minof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa<bb ? aa : bb;})
346#define maxof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa>bb ? aa : bb;})
347
Rob Landley5b405822014-03-29 18:11:00 -0500348// Functions in need of further review/cleanup
Rob Landley34b91a92013-11-10 18:20:16 -0600349#include "lib/pending.h"