landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 1 | /* vi: set ts=4 :*/ |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 2 | /* lib.h - header file for lib directory |
| 3 | * |
| 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | */ |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 6 | |
Rob Landley | f01503d | 2012-02-02 07:26:39 -0600 | [diff] [blame] | 7 | // Unfortunately, sizeof() doesn't work in a preprocessor test. TODO. |
| 8 | |
| 9 | //#if sizeof(double) <= sizeof(long) |
| 10 | //typedef double FLOAT; |
| 11 | //#else |
| 12 | typedef float FLOAT; |
| 13 | //#endif |
| 14 | |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 15 | // libc generally has this, but the headers are screwed up |
| 16 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 17 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 18 | // llist.c |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 19 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 20 | // All these list types can be handled by the same code because first element |
| 21 | // is always next pointer, so next = (mytype *)&struct. |
| 22 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 23 | struct string_list { |
| 24 | struct string_list *next; |
| 25 | char str[0]; |
| 26 | }; |
| 27 | |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 28 | struct arg_list { |
| 29 | struct arg_list *next; |
| 30 | char *arg; |
| 31 | }; |
| 32 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 33 | struct double_list { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 34 | struct double_list *next, *prev; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 35 | char *data; |
| 36 | }; |
| 37 | |
Rob Landley | 6ef04ef | 2008-01-20 17:34:53 -0600 | [diff] [blame] | 38 | void llist_free(void *list, void (*freeit)(void *data)); |
| 39 | void *llist_pop(void *list); // actually void **list, but the compiler's dumb |
Rob Landley | 2c48247 | 2012-03-12 00:25:40 -0500 | [diff] [blame] | 40 | void dlist_add_nomalloc(struct double_list **list, struct double_list *new); |
Rob Landley | bdf037f | 2008-10-23 16:44:30 -0500 | [diff] [blame] | 41 | struct double_list *dlist_add(struct double_list **list, char *data); |
Rob Landley | 6ef04ef | 2008-01-20 17:34:53 -0600 | [diff] [blame] | 42 | |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 43 | // args.c |
| 44 | void get_optflags(void); |
| 45 | |
| 46 | // dirtree.c |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 47 | |
| 48 | // Values returnable from callback function (bitfield, or them together) |
| 49 | // Default with no callback is 0 |
| 50 | |
| 51 | // Do not add this node to the tree |
| 52 | #define DIRTREE_NOSAVE 1 |
| 53 | // Do not recurse into children |
| 54 | #define DIRTREE_NORECURSE 2 |
| 55 | // Call again after handling all children (Directories only. Sets linklen = -1) |
| 56 | #define DIRTREE_COMEAGAIN 4 |
| 57 | // Follow symlinks to directories |
| 58 | #define DIRTREE_SYMFOLLOW 8 |
| 59 | // Abort recursive dirtree. (Forces NOSAVE and NORECURSE on this entry.) |
| 60 | #define DIRTREE_ABORT (256|DIRTREE_NOSAVE|DIRTREE_NORECURSE) |
| 61 | |
| 62 | #define DIRTREE_ABORTVAL ((struct dirtree *)1) |
| 63 | |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 64 | struct dirtree { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 65 | struct dirtree *next, *parent, *child; |
| 66 | long extra; // place for user to store their stuff (can be pointer) |
| 67 | long data; // dirfd for directory, linklen for symlink |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 68 | struct stat st; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 69 | char *symlink; |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 70 | char name[]; |
| 71 | }; |
| 72 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 73 | struct dirtree *dirtree_add_node(int dirfd, char *name); |
| 74 | char *dirtree_path(struct dirtree *node, int *plen); |
| 75 | int dirtree_isdotdot(struct dirtree *catch); |
| 76 | struct dirtree *handle_callback(struct dirtree *new, |
| 77 | int (*callback)(struct dirtree *node)); |
| 78 | void dirtree_recurse(struct dirtree *node, |
| 79 | int (*callback)(struct dirtree *node)); |
| 80 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 81 | |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 82 | // lib.c |
Rob Landley | e15850a | 2007-11-19 01:51:00 -0600 | [diff] [blame] | 83 | void xstrcpy(char *dest, char *src, size_t size); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 84 | void verror_msg(char *msg, int err, va_list va); |
| 85 | void error_msg(char *msg, ...); |
| 86 | void perror_msg(char *msg, ...); |
Rob Landley | efa93b9 | 2007-11-15 21:12:24 -0600 | [diff] [blame] | 87 | void error_exit(char *msg, ...) noreturn; |
| 88 | void perror_exit(char *msg, ...) noreturn; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 89 | void *xmalloc(size_t size); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 90 | void *xzalloc(size_t size); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 91 | void *xrealloc(void *ptr, size_t size); |
Rob Landley | 1e01cd1 | 2010-01-05 10:48:32 -0600 | [diff] [blame] | 92 | char *xstrndup(char *s, size_t n); |
| 93 | char *xstrdup(char *s); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 94 | char *xmsprintf(char *format, ...); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 95 | void xprintf(char *format, ...); |
Rob Landley | 5084fea | 2007-06-18 00:14:03 -0400 | [diff] [blame] | 96 | void xputs(char *s); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 97 | void xputc(char c); |
| 98 | void xflush(void); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 99 | void xexec(char **argv); |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 100 | void xaccess(char *path, int flags); |
Rob Landley | e745d8e | 2007-12-20 06:30:19 -0600 | [diff] [blame] | 101 | void xunlink(char *path); |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 102 | int xcreate(char *path, int flags, int mode); |
| 103 | int xopen(char *path, int flags); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 104 | void xclose(int fd); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 105 | int xdup(int fd); |
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 | ssize_t readall(int fd, void *buf, size_t len); |
| 108 | ssize_t writeall(int fd, void *buf, size_t len); |
| 109 | size_t xread(int fd, void *buf, size_t len); |
| 110 | void xreadall(int fd, void *buf, size_t len); |
| 111 | void xwrite(int fd, void *buf, size_t len); |
Rob Landley | 5247671 | 2009-01-18 16:19:25 -0600 | [diff] [blame] | 112 | off_t xlseek(int fd, off_t offset, int whence); |
Rob Landley | e824ed1 | 2008-07-18 08:43:18 -0500 | [diff] [blame] | 113 | char *readfile(char *name); |
| 114 | char *xreadfile(char *name); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 115 | char *xgetcwd(void); |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 116 | void xstat(char *path, struct stat *st); |
Rob Landley | fa98d01 | 2006-11-02 02:57:27 -0500 | [diff] [blame] | 117 | char *xabspath(char *path); |
Rob Landley | 988abb3 | 2008-05-12 00:52:27 -0500 | [diff] [blame] | 118 | void xchdir(char *path); |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 119 | void xmkpath(char *path, int mode); |
Rob Landley | e0377fb | 2010-01-05 12:17:05 -0600 | [diff] [blame] | 120 | void xsetuid(uid_t uid); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 121 | struct string_list *find_in_path(char *path, char *filename); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 122 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen); |
| 123 | void itoa_to_buf(int n, char *buf, unsigned buflen); |
| 124 | char *utoa(unsigned n); |
| 125 | char *itoa(int n); |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 126 | long atolx(char *c); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 127 | int numlen(long l); |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 128 | off_t fdlength(int fd); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 129 | char *xreadlink(char *name); |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 130 | void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
| 131 | void (*function)(int fd, char *name)); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 132 | void loopfiles(char **argv, void (*function)(int fd, char *name)); |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 133 | char *get_rawline(int fd, long *plen, char end); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 134 | char *get_line(int fd); |
| 135 | void xsendfile(int in, int out); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 136 | int copy_tempfile(int fdin, char *name, char **tempname); |
| 137 | void delete_tempfile(int fdin, int fdout, char **tempname); |
| 138 | void replace_tempfile(int fdin, int fdout, char **tempname); |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 139 | void crc_init(unsigned int *crc_table, int little_endian); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 140 | void terminal_size(unsigned *x, unsigned *y); |
Rob Landley | f793d53 | 2012-02-27 21:56:49 -0600 | [diff] [blame] | 141 | int yesno(char *prompt, int def); |
Rob Landley | ebcf0be | 2012-02-18 18:53:57 -0600 | [diff] [blame] | 142 | void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid)); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 143 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 144 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 145 | // getmountlist.c |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 146 | struct mtab_list { |
| 147 | struct mtab_list *next; |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 148 | struct stat stat; |
| 149 | struct statvfs statvfs; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 150 | char *dir; |
| 151 | char *device; |
| 152 | char type[0]; |
| 153 | }; |
| 154 | |
| 155 | struct mtab_list *getmountlist(int die); |
| 156 | |
Rob Landley | 6000f13 | 2007-01-18 22:00:12 -0500 | [diff] [blame] | 157 | void bunzipStream(int src_fd, int dst_fd); |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 158 | |
| 159 | // signal |
| 160 | |
| 161 | int sig_to_num(char *pidstr); |
| 162 | char *num_to_sig(int sig); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 163 | |
| 164 | mode_t string_to_mode(char *mode_str, mode_t base); |