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