landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 1 | /* lib.h - header file for lib directory |
| 2 | * |
| 3 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 4 | */ |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 5 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 6 | // llist.c |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 7 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 8 | // All these list types can be handled by the same code because first element |
Rob Landley | e5f3a0b | 2013-03-17 17:57:28 -0500 | [diff] [blame] | 9 | // is always next pointer, so next = (mytype *)&struct. (The payloads are |
| 10 | // named differently to catch using the wrong type early.) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 11 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 12 | struct string_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 13 | struct string_list *next; |
| 14 | char str[0]; |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 15 | }; |
| 16 | |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 17 | struct arg_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 18 | struct arg_list *next; |
| 19 | char *arg; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 20 | }; |
| 21 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 22 | struct double_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 23 | struct double_list *next, *prev; |
| 24 | char *data; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 25 | }; |
| 26 | |
Rob Landley | 9e2b6db | 2012-07-15 17:22:04 -0500 | [diff] [blame] | 27 | void llist_traverse(void *list, void (*using)(void *data)); |
Rob Landley | 5f57bcc | 2013-09-09 04:26:03 -0500 | [diff] [blame] | 28 | void *llist_pop(void *list); // actually void **list |
| 29 | void *dlist_pop(void *list); // actually struct double_list **list |
Rob Landley | 2c48247 | 2012-03-12 00:25:40 -0500 | [diff] [blame] | 30 | void dlist_add_nomalloc(struct double_list **list, struct double_list *new); |
Rob Landley | bdf037f | 2008-10-23 16:44:30 -0500 | [diff] [blame] | 31 | struct double_list *dlist_add(struct double_list **list, char *data); |
Rob Landley | 6ef04ef | 2008-01-20 17:34:53 -0600 | [diff] [blame] | 32 | |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 33 | // args.c |
| 34 | void get_optflags(void); |
| 35 | |
| 36 | // dirtree.c |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 37 | |
| 38 | // Values returnable from callback function (bitfield, or them together) |
| 39 | // Default with no callback is 0 |
| 40 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 41 | // Add this node to the tree |
| 42 | #define DIRTREE_SAVE 1 |
| 43 | // Recurse into children |
| 44 | #define DIRTREE_RECURSE 2 |
| 45 | // Call again after handling all children of this directory |
| 46 | // (Ignored for non-directories, sets linklen = -1 before second call.) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 47 | #define DIRTREE_COMEAGAIN 4 |
| 48 | // Follow symlinks to directories |
| 49 | #define DIRTREE_SYMFOLLOW 8 |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 50 | // Don't look at any more files in this directory. |
| 51 | #define DIRTREE_ABORT 256 |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 52 | |
| 53 | #define DIRTREE_ABORTVAL ((struct dirtree *)1) |
| 54 | |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 55 | struct dirtree { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 56 | struct dirtree *next, *parent, *child; |
| 57 | long extra; // place for user to store their stuff (can be pointer) |
| 58 | struct stat st; |
| 59 | char *symlink; |
| 60 | int data; // dirfd for directory, linklen for symlink, -1 = comeagain |
| 61 | char name[]; |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 64 | struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int symfollow); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 65 | char *dirtree_path(struct dirtree *node, int *plen); |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 66 | int dirtree_notdotdot(struct dirtree *catch); |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 67 | int dirtree_parentfd(struct dirtree *node); |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 68 | struct dirtree *dirtree_handle_callback(struct dirtree *new, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 69 | int (*callback)(struct dirtree *node)); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 70 | void dirtree_recurse(struct dirtree *node, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 71 | int (*callback)(struct dirtree *node), int symfollow); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 72 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 73 | |
Rob Landley | 36ffc5a | 2013-04-14 21:43:22 -0500 | [diff] [blame] | 74 | // help.c |
| 75 | |
| 76 | void show_help(void); |
| 77 | |
Rob Landley | 7275667 | 2013-07-17 17:22:46 -0500 | [diff] [blame] | 78 | // xwrap.c |
Rob Landley | be93c91 | 2013-04-20 23:33:48 -0500 | [diff] [blame] | 79 | void xstrncpy(char *dest, char *src, size_t size); |
Rob Landley | 5583030 | 2013-06-16 19:59:51 -0500 | [diff] [blame] | 80 | void xexit(void) noreturn; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 81 | void *xmalloc(size_t size); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 82 | void *xzalloc(size_t size); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 83 | void *xrealloc(void *ptr, size_t size); |
Rob Landley | 1e01cd1 | 2010-01-05 10:48:32 -0600 | [diff] [blame] | 84 | char *xstrndup(char *s, size_t n); |
| 85 | char *xstrdup(char *s); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 86 | char *xmsprintf(char *format, ...); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 87 | void xprintf(char *format, ...); |
Rob Landley | 5084fea | 2007-06-18 00:14:03 -0400 | [diff] [blame] | 88 | void xputs(char *s); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 89 | void xputc(char c); |
| 90 | void xflush(void); |
Rob Landley | 7275667 | 2013-07-17 17:22:46 -0500 | [diff] [blame] | 91 | void xexec_optargs(int skip); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 92 | void xexec(char **argv); |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 93 | void xaccess(char *path, int flags); |
Rob Landley | e745d8e | 2007-12-20 06:30:19 -0600 | [diff] [blame] | 94 | void xunlink(char *path); |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 95 | int xcreate(char *path, int flags, int mode); |
| 96 | int xopen(char *path, int flags); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 97 | void xclose(int fd); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 98 | int xdup(int fd); |
Rob Landley | 1aa7511 | 2013-08-07 12:19:51 -0500 | [diff] [blame] | 99 | FILE *xfdopen(int fd, char *mode); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 100 | FILE *xfopen(char *path, char *mode); |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 101 | size_t xread(int fd, void *buf, size_t len); |
| 102 | void xreadall(int fd, void *buf, size_t len); |
| 103 | void xwrite(int fd, void *buf, size_t len); |
Rob Landley | 5247671 | 2009-01-18 16:19:25 -0600 | [diff] [blame] | 104 | off_t xlseek(int fd, off_t offset, int whence); |
Rob Landley | e824ed1 | 2008-07-18 08:43:18 -0500 | [diff] [blame] | 105 | char *xreadfile(char *name); |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 106 | int xioctl(int fd, int request, void *data); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 107 | char *xgetcwd(void); |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 108 | void xstat(char *path, struct stat *st); |
Rob Landley | fe91e68 | 2012-11-22 21:18:09 -0600 | [diff] [blame] | 109 | char *xabspath(char *path, int exact); |
Rob Landley | eec4637 | 2012-06-01 13:50:41 -0500 | [diff] [blame] | 110 | char *xrealpath(char *path); |
Rob Landley | 988abb3 | 2008-05-12 00:52:27 -0500 | [diff] [blame] | 111 | void xchdir(char *path); |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 112 | void xmkpath(char *path, int mode); |
Rob Landley | e0377fb | 2010-01-05 12:17:05 -0600 | [diff] [blame] | 113 | void xsetuid(uid_t uid); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 114 | char *xreadlink(char *name); |
Rob Landley | 7275667 | 2013-07-17 17:22:46 -0500 | [diff] [blame] | 115 | long xparsetime(char *arg, long units, long *fraction); |
Felix Janda | dccfb2a | 2013-08-26 21:55:33 +0200 | [diff] [blame] | 116 | void xpidfile(char *name); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 117 | |
| 118 | // lib.c |
| 119 | void verror_msg(char *msg, int err, va_list va); |
| 120 | void error_msg(char *msg, ...); |
| 121 | void perror_msg(char *msg, ...); |
| 122 | void error_exit(char *msg, ...) noreturn; |
| 123 | void perror_exit(char *msg, ...) noreturn; |
| 124 | ssize_t readall(int fd, void *buf, size_t len); |
| 125 | ssize_t writeall(int fd, void *buf, size_t len); |
| 126 | off_t lskip(int fd, off_t offset); |
| 127 | struct string_list **splitpath(char *path, struct string_list **list); |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 128 | char *readfile(char *name, char *buf, off_t len); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 129 | void msleep(long miliseconds); |
| 130 | int64_t peek(void *ptr, int size); |
| 131 | void poke(void *ptr, uint64_t val, int size); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 132 | struct string_list *find_in_path(char *path, char *filename); |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 133 | long atolx(char *c); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 134 | int numlen(long l); |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 135 | int stridx(char *haystack, char needle); |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 136 | off_t fdlength(int fd); |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 137 | void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 138 | void (*function)(int fd, char *name)); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 139 | void loopfiles(char **argv, void (*function)(int fd, char *name)); |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 140 | char *get_rawline(int fd, long *plen, char end); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 141 | char *get_line(int fd); |
| 142 | void xsendfile(int in, int out); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 143 | int wfchmodat(int rc, char *name, mode_t mode); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 144 | int copy_tempfile(int fdin, char *name, char **tempname); |
| 145 | void delete_tempfile(int fdin, int fdout, char **tempname); |
| 146 | void replace_tempfile(int fdin, int fdout, char **tempname); |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 147 | void crc_init(unsigned int *crc_table, int little_endian); |
Rob Landley | 10bdaa4 | 2013-11-07 09:04:50 -0600 | [diff] [blame] | 148 | int terminal_size(unsigned *x, unsigned *y); |
Rob Landley | f793d53 | 2012-02-27 21:56:49 -0600 | [diff] [blame] | 149 | int yesno(char *prompt, int def); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 150 | |
Rob Landley | f015344 | 2013-04-26 02:41:05 -0500 | [diff] [blame] | 151 | // net.c |
| 152 | int xsocket(int domain, int type, int protocol); |
| 153 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 154 | // getmountlist.c |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 155 | struct mtab_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 156 | struct mtab_list *next; |
| 157 | struct stat stat; |
| 158 | struct statvfs statvfs; |
| 159 | char *dir; |
| 160 | char *device; |
| 161 | char type[0]; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 162 | }; |
| 163 | |
Rob Landley | 42adb7a | 2013-08-30 17:34:24 -0500 | [diff] [blame] | 164 | struct mtab_list *xgetmountlist(char *path); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 165 | |
Rob Landley | 6000f13 | 2007-01-18 22:00:12 -0500 | [diff] [blame] | 166 | void bunzipStream(int src_fd, int dst_fd); |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 167 | |
| 168 | // signal |
| 169 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 170 | void sigatexit(void *handler); |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 171 | int sig_to_num(char *pidstr); |
| 172 | char *num_to_sig(int sig); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 173 | |
| 174 | mode_t string_to_mode(char *mode_str, mode_t base); |
Rob Landley | 5a26a86 | 2013-06-02 00:24:24 -0500 | [diff] [blame] | 175 | void mode_to_string(mode_t mode, char *buf); |
Rob Landley | 2c917f5 | 2012-07-17 08:54:47 -0500 | [diff] [blame] | 176 | |
Rob Landley | 34b91a9 | 2013-11-10 18:20:16 -0600 | [diff] [blame^] | 177 | #include "lib/pending.h" |