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