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 | f01503d | 2012-02-02 07:26:39 -0600 | [diff] [blame] | 6 | // Unfortunately, sizeof() doesn't work in a preprocessor test. TODO. |
| 7 | |
| 8 | //#if sizeof(double) <= sizeof(long) |
| 9 | //typedef double FLOAT; |
| 10 | //#else |
| 11 | typedef float FLOAT; |
| 12 | //#endif |
| 13 | |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 14 | // libc generally has this, but the headers are screwed up |
| 15 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 16 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 17 | // llist.c |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 18 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 19 | // All these list types can be handled by the same code because first element |
| 20 | // is always next pointer, so next = (mytype *)&struct. |
| 21 | |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 22 | struct string_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 23 | struct string_list *next; |
| 24 | char str[0]; |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 25 | }; |
| 26 | |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 27 | struct arg_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 28 | struct arg_list *next; |
| 29 | char *arg; |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 30 | }; |
| 31 | |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 32 | struct double_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 33 | struct double_list *next, *prev; |
| 34 | char *data; |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
Rob Landley | 9e2b6db | 2012-07-15 17:22:04 -0500 | [diff] [blame] | 37 | void llist_traverse(void *list, void (*using)(void *data)); |
Rob Landley | 6ef04ef | 2008-01-20 17:34:53 -0600 | [diff] [blame] | 38 | 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] | 39 | void dlist_add_nomalloc(struct double_list **list, struct double_list *new); |
Rob Landley | bdf037f | 2008-10-23 16:44:30 -0500 | [diff] [blame] | 40 | struct double_list *dlist_add(struct double_list **list, char *data); |
Rob Landley | 6ef04ef | 2008-01-20 17:34:53 -0600 | [diff] [blame] | 41 | |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 42 | // args.c |
| 43 | void get_optflags(void); |
| 44 | |
| 45 | // dirtree.c |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 46 | |
| 47 | // Values returnable from callback function (bitfield, or them together) |
| 48 | // Default with no callback is 0 |
| 49 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 50 | // Add this node to the tree |
| 51 | #define DIRTREE_SAVE 1 |
| 52 | // Recurse into children |
| 53 | #define DIRTREE_RECURSE 2 |
| 54 | // Call again after handling all children of this directory |
| 55 | // (Ignored for non-directories, sets linklen = -1 before second call.) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 56 | #define DIRTREE_COMEAGAIN 4 |
| 57 | // Follow symlinks to directories |
| 58 | #define DIRTREE_SYMFOLLOW 8 |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 59 | // Don't look at any more files in this directory. |
| 60 | #define DIRTREE_ABORT 256 |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 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 | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 65 | struct dirtree *next, *parent, *child; |
| 66 | long extra; // place for user to store their stuff (can be pointer) |
| 67 | struct stat st; |
| 68 | char *symlink; |
| 69 | int data; // dirfd for directory, linklen for symlink, -1 = comeagain |
| 70 | char name[]; |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 71 | }; |
| 72 | |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 73 | struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 74 | char *dirtree_path(struct dirtree *node, int *plen); |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 75 | int dirtree_notdotdot(struct dirtree *catch); |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 76 | int dirtree_parentfd(struct dirtree *node); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 77 | struct dirtree *handle_callback(struct dirtree *new, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 78 | int (*callback)(struct dirtree *node)); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 79 | void dirtree_recurse(struct dirtree *node, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 80 | int (*callback)(struct dirtree *node), int symfollow); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 81 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); |
Rob Landley | 8324b89 | 2006-11-19 02:49:22 -0500 | [diff] [blame] | 82 | |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 83 | // lib.c |
Rob Landley | e15850a | 2007-11-19 01:51:00 -0600 | [diff] [blame] | 84 | void xstrcpy(char *dest, char *src, size_t size); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 85 | void verror_msg(char *msg, int err, va_list va); |
| 86 | void error_msg(char *msg, ...); |
| 87 | void perror_msg(char *msg, ...); |
Rob Landley | efa93b9 | 2007-11-15 21:12:24 -0600 | [diff] [blame] | 88 | void error_exit(char *msg, ...) noreturn; |
| 89 | void perror_exit(char *msg, ...) noreturn; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 90 | void *xmalloc(size_t size); |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 91 | void *xzalloc(size_t size); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 92 | void *xrealloc(void *ptr, size_t size); |
Rob Landley | 1e01cd1 | 2010-01-05 10:48:32 -0600 | [diff] [blame] | 93 | char *xstrndup(char *s, size_t n); |
| 94 | char *xstrdup(char *s); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 95 | char *xmsprintf(char *format, ...); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 96 | void xprintf(char *format, ...); |
Rob Landley | 5084fea | 2007-06-18 00:14:03 -0400 | [diff] [blame] | 97 | void xputs(char *s); |
Rob Landley | 24d1d45 | 2007-01-20 18:04:20 -0500 | [diff] [blame] | 98 | void xputc(char c); |
| 99 | void xflush(void); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 100 | void xexec(char **argv); |
Rob Landley | d3e9d64 | 2007-01-08 03:25:47 -0500 | [diff] [blame] | 101 | void xaccess(char *path, int flags); |
Rob Landley | e745d8e | 2007-12-20 06:30:19 -0600 | [diff] [blame] | 102 | void xunlink(char *path); |
Rob Landley | 1322beb | 2007-01-07 22:51:12 -0500 | [diff] [blame] | 103 | int xcreate(char *path, int flags, int mode); |
| 104 | int xopen(char *path, int flags); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 105 | void xclose(int fd); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 106 | int xdup(int fd); |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 107 | FILE *xfopen(char *path, char *mode); |
Rob Landley | 9016377 | 2007-01-18 21:54:08 -0500 | [diff] [blame] | 108 | ssize_t readall(int fd, void *buf, size_t len); |
| 109 | ssize_t writeall(int fd, void *buf, size_t len); |
| 110 | size_t xread(int fd, void *buf, size_t len); |
| 111 | void xreadall(int fd, void *buf, size_t len); |
| 112 | void xwrite(int fd, void *buf, size_t len); |
Rob Landley | 5247671 | 2009-01-18 16:19:25 -0600 | [diff] [blame] | 113 | off_t xlseek(int fd, off_t offset, int whence); |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 114 | off_t lskip(int fd, off_t offset); |
Rob Landley | e824ed1 | 2008-07-18 08:43:18 -0500 | [diff] [blame] | 115 | char *readfile(char *name); |
| 116 | char *xreadfile(char *name); |
landley | 00f87f1 | 2006-10-25 18:38:37 -0400 | [diff] [blame] | 117 | char *xgetcwd(void); |
Rob Landley | d25f7e4 | 2007-02-03 14:11:26 -0500 | [diff] [blame] | 118 | void xstat(char *path, struct stat *st); |
Rob Landley | bd2e227 | 2012-11-20 09:21:52 -0600 | [diff] [blame^] | 119 | char *xabspath(char *path, unsigned missing); |
Rob Landley | eec4637 | 2012-06-01 13:50:41 -0500 | [diff] [blame] | 120 | char *xrealpath(char *path); |
Rob Landley | 988abb3 | 2008-05-12 00:52:27 -0500 | [diff] [blame] | 121 | void xchdir(char *path); |
Rob Landley | 3548341 | 2007-12-27 21:36:33 -0600 | [diff] [blame] | 122 | void xmkpath(char *path, int mode); |
Rob Landley | e0377fb | 2010-01-05 12:17:05 -0600 | [diff] [blame] | 123 | void xsetuid(uid_t uid); |
Rob Landley | 0a04b3e | 2006-11-03 00:05:52 -0500 | [diff] [blame] | 124 | struct string_list *find_in_path(char *path, char *filename); |
landley | 09ea7ac | 2006-10-30 01:38:00 -0500 | [diff] [blame] | 125 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen); |
| 126 | void itoa_to_buf(int n, char *buf, unsigned buflen); |
| 127 | char *utoa(unsigned n); |
| 128 | char *itoa(int n); |
Rob Landley | f575716 | 2007-02-16 21:08:22 -0500 | [diff] [blame] | 129 | long atolx(char *c); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 130 | int numlen(long l); |
Rob Landley | 2037b83 | 2012-07-15 16:56:20 -0500 | [diff] [blame] | 131 | int stridx(char *haystack, char needle); |
Rob Landley | e2580db | 2007-01-23 13:20:38 -0500 | [diff] [blame] | 132 | off_t fdlength(int fd); |
Rob Landley | 0c93f6c | 2007-04-29 19:55:21 -0400 | [diff] [blame] | 133 | char *xreadlink(char *name); |
Rob Landley | ad63f4b | 2011-12-12 15:19:52 -0600 | [diff] [blame] | 134 | void loopfiles_rw(char **argv, int flags, int permissions, int failok, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 135 | void (*function)(int fd, char *name)); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 136 | void loopfiles(char **argv, void (*function)(int fd, char *name)); |
Rob Landley | 3fc4e0f | 2008-04-13 00:29:00 -0500 | [diff] [blame] | 137 | char *get_rawline(int fd, long *plen, char end); |
Rob Landley | bc07865 | 2007-12-15 21:47:25 -0600 | [diff] [blame] | 138 | char *get_line(int fd); |
| 139 | void xsendfile(int in, int out); |
Rob Landley | 67a069d | 2012-06-03 00:32:12 -0500 | [diff] [blame] | 140 | int wfchmodat(int rc, char *name, mode_t mode); |
Rob Landley | 42ecbab | 2007-12-18 02:02:21 -0600 | [diff] [blame] | 141 | int copy_tempfile(int fdin, char *name, char **tempname); |
| 142 | void delete_tempfile(int fdin, int fdout, char **tempname); |
| 143 | void replace_tempfile(int fdin, int fdout, char **tempname); |
Rob Landley | b15b8fa | 2009-01-05 01:05:43 -0600 | [diff] [blame] | 144 | void crc_init(unsigned int *crc_table, int little_endian); |
Rob Landley | 26e7b5e | 2012-02-02 07:27:35 -0600 | [diff] [blame] | 145 | void terminal_size(unsigned *x, unsigned *y); |
Rob Landley | f793d53 | 2012-02-27 21:56:49 -0600 | [diff] [blame] | 146 | int yesno(char *prompt, int def); |
Rob Landley | ebcf0be | 2012-02-18 18:53:57 -0600 | [diff] [blame] | 147 | 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] | 148 | |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 149 | |
landley | cd9dfc3 | 2006-10-18 18:38:16 -0400 | [diff] [blame] | 150 | // getmountlist.c |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 151 | struct mtab_list { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 152 | struct mtab_list *next; |
| 153 | struct stat stat; |
| 154 | struct statvfs statvfs; |
| 155 | char *dir; |
| 156 | char *device; |
| 157 | char type[0]; |
landley | 4f344e3 | 2006-10-05 16:18:03 -0400 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | struct mtab_list *getmountlist(int die); |
| 161 | |
Rob Landley | 6000f13 | 2007-01-18 22:00:12 -0500 | [diff] [blame] | 162 | void bunzipStream(int src_fd, int dst_fd); |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 163 | |
| 164 | // signal |
| 165 | |
Rob Landley | c52db60 | 2012-07-30 01:01:33 -0500 | [diff] [blame] | 166 | void sigatexit(void *handler); |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 167 | int sig_to_num(char *pidstr); |
| 168 | char *num_to_sig(int sig); |
Daniel Walter | 05744b3 | 2012-03-19 19:57:56 -0500 | [diff] [blame] | 169 | |
| 170 | mode_t string_to_mode(char *mode_str, mode_t base); |
Rob Landley | 2c917f5 | 2012-07-17 08:54:47 -0500 | [diff] [blame] | 171 | |
| 172 | // password helper functions |
Rob Landley | 6ba38c2 | 2012-07-21 18:38:36 -0500 | [diff] [blame] | 173 | int read_password(char * buff, int buflen, char* mesg); |
| 174 | int update_password(char *filename, char* username, char* encrypted); |
Rob Landley | 2c917f5 | 2012-07-17 08:54:47 -0500 | [diff] [blame] | 175 | |
Ashwini Kumar | 1a0eedf | 2012-08-26 21:17:00 -0500 | [diff] [blame] | 176 | // du helper functions |
| 177 | char* make_human_readable(unsigned long long size, unsigned long unit); |
Rob Landley | 734b530 | 2012-11-16 12:26:48 -0600 | [diff] [blame] | 178 | |
| 179 | // cut helper functions |
| 180 | unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange); |