1 /* lib.h - header file for lib directory 2 * 3 * Copyright 2006 Rob Landley <rob@landley.net> 4 */ 5 6 // llist.c 7 8 // All these list types can be handled by the same code because first element 9 // is always next pointer, so next = (mytype *)&struct. (The payloads are 10 // named differently to catch using the wrong type early.) 11 12 struct string_list { 13 struct string_list *next; 14 char str[0]; 15 }; 16 17 struct arg_list { 18 struct arg_list *next; 19 char *arg; 20 }; 21 22 struct double_list { 23 struct double_list *next, *prev; 24 char *data; 25 }; 26 27 void llist_free_arg(void *node); 28 void llist_free_double(void *node); 29 void llist_traverse(void *list, void (*using)(void *node)); 30 void *llist_pop(void *list); // actually void **list 31 void *dlist_pop(void *list); // actually struct double_list **list 32 void dlist_add_nomalloc(struct double_list **list, struct double_list *new); 33 struct double_list *dlist_add(struct double_list **list, char *data); 34 void *dlist_terminate(void *list); 35 36 // args.c 37 void get_optflags(void); 38 39 // dirtree.c 40 41 // Values returnable from callback function (bitfield, or them together) 42 // Default with no callback is 0 43 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.) 50 #define DIRTREE_COMEAGAIN 4 51 // Follow symlinks to directories 52 #define DIRTREE_SYMFOLLOW 8 53 // Don't warn about failure to stat 54 #define DIRTREE_SHUTUP 16 55 // Don't look at any more files in this directory. 56 #define DIRTREE_ABORT 256 57 58 #define DIRTREE_ABORTVAL ((struct dirtree *)1) 59 60 struct dirtree { 61 struct dirtree *next, *parent, *child; 62 long extra; // place for user to store their stuff (can be pointer) 63 struct stat st; 64 char *symlink; 65 int data; // dirfd for directory, linklen for symlink 66 char again; 67 char name[]; 68 }; 69 70 struct dirtree *dirtree_start(char *name, int symfollow); 71 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags); 72 char *dirtree_path(struct dirtree *node, int *plen); 73 int dirtree_notdotdot(struct dirtree *catch); 74 int dirtree_parentfd(struct dirtree *node); 75 struct dirtree *dirtree_handle_callback(struct dirtree *new, 76 int (*callback)(struct dirtree *node)); 77 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node), 78 int symfollow); 79 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); 80 81 // help.c 82 83 void show_help(void); 84 85 // xwrap.c 86 void xstrncpy(char *dest, char *src, size_t size); 87 void xstrncat(char *dest, char *src, size_t size); 88 void xexit(void) noreturn; 89 void *xmalloc(size_t size); 90 void *xzalloc(size_t size); 91 void *xrealloc(void *ptr, size_t size); 92 char *xstrndup(char *s, size_t n); 93 char *xstrdup(char *s); 94 char *xmprintf(char *format, ...) printf_format; 95 void xprintf(char *format, ...) printf_format; 96 void xputs(char *s); 97 void xputc(char c); 98 void xflush(void); 99 void xexec(char **argv); 100 pid_t xpopen_both(char **argv, int *pipes); 101 int xpclose_both(pid_t pid, int *pipes); 102 pid_t xpopen(char **argv, int *pipe, int stdout); 103 pid_t xpclose(pid_t pid, int pipe); 104 int xrun(char **argv); 105 int xpspawn(char **argv, int*pipes); 106 void xaccess(char *path, int flags); 107 void xunlink(char *path); 108 int xcreate(char *path, int flags, int mode); 109 int xopen(char *path, int flags); 110 void xclose(int fd); 111 int xdup(int fd); 112 FILE *xfdopen(int fd, char *mode); 113 FILE *xfopen(char *path, char *mode); 114 size_t xread(int fd, void *buf, size_t len); 115 void xreadall(int fd, void *buf, size_t len); 116 void xwrite(int fd, void *buf, size_t len); 117 off_t xlseek(int fd, off_t offset, int whence); 118 char *xreadfile(char *name, char *buf, off_t len); 119 int xioctl(int fd, int request, void *data); 120 char *xgetcwd(void); 121 void xstat(char *path, struct stat *st); 122 char *xabspath(char *path, int exact); 123 void xchdir(char *path); 124 void xchroot(char *path); 125 struct passwd *xgetpwuid(uid_t uid); 126 struct group *xgetgrgid(gid_t gid); 127 struct passwd *xgetpwnam(char *name); 128 struct group *xgetgrnam(char *name); 129 struct passwd *xgetpwnamid(char *user); 130 struct group *xgetgrnamid(char *group); 131 void xsetuser(struct passwd *pwd); 132 char *xreadlink(char *name); 133 long xparsetime(char *arg, long units, long *fraction); 134 void xpidfile(char *name); 135 void xregcomp(regex_t *preg, char *rexec, int cflags); 136 char *xtzset(char *new); 137 void xsignal(int signal, void *handler); 138 139 // lib.c 140 void verror_msg(char *msg, int err, va_list va); 141 void error_msg(char *msg, ...) printf_format; 142 void perror_msg(char *msg, ...) printf_format; 143 void error_exit(char *msg, ...) printf_format noreturn; 144 void perror_exit(char *msg, ...) printf_format noreturn; 145 ssize_t readall(int fd, void *buf, size_t len); 146 ssize_t writeall(int fd, void *buf, size_t len); 147 off_t lskip(int fd, off_t offset); 148 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags); 149 struct string_list **splitpath(char *path, struct string_list **list); 150 char *readfileat(int dirfd, char *name, char *buf, off_t len); 151 char *readfile(char *name, char *buf, off_t len); 152 void msleep(long miliseconds); 153 int64_t peek_le(void *ptr, unsigned size); 154 int64_t peek_be(void *ptr, unsigned size); 155 int64_t peek(void *ptr, unsigned size); 156 void poke(void *ptr, uint64_t val, int size); 157 struct string_list *find_in_path(char *path, char *filename); 158 long estrtol(char *str, char **end, int base); 159 long xstrtol(char *str, char **end, int base); 160 long atolx(char *c); 161 long atolx_range(char *numstr, long low, long high); 162 int stridx(char *haystack, char needle); 163 int unescape(char c); 164 int strstart(char **a, char *b); 165 off_t fdlength(int fd); 166 void loopfiles_rw(char **argv, int flags, int permissions, int failok, 167 void (*function)(int fd, char *name)); 168 void loopfiles(char **argv, void (*function)(int fd, char *name)); 169 char *get_rawline(int fd, long *plen, char end); 170 char *get_line(int fd); 171 void xsendfile(int in, int out); 172 int wfchmodat(int rc, char *name, mode_t mode); 173 int copy_tempfile(int fdin, char *name, char **tempname); 174 void delete_tempfile(int fdin, int fdout, char **tempname); 175 void replace_tempfile(int fdin, int fdout, char **tempname); 176 void crc_init(unsigned int *crc_table, int little_endian); 177 void base64_init(char *p); 178 int yesno(char *prompt, int def); 179 int human_readable(char *buf, unsigned long long num); 180 int qstrcmp(const void *a, const void *b); 181 int xpoll(struct pollfd *fds, int nfds, int timeout); 182 183 // interestingtimes.c 184 int xgettty(void); 185 int terminal_size(unsigned *xx, unsigned *yy); 186 int set_terminal(int fd, int raw, struct termios *old); 187 int scan_key(char *scratch, char **seqs, int block); 188 189 // net.c 190 int xsocket(int domain, int type, int protocol); 191 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len); 192 193 // password.c 194 int get_salt(char *salt, char * algo); 195 196 // getmountlist.c 197 struct mtab_list { 198 struct mtab_list *next, *prev; 199 struct stat stat; 200 struct statvfs statvfs; 201 char *dir; 202 char *device; 203 char *opts; 204 char type[0]; 205 }; 206 207 void comma_collate(char **old, char *new); 208 char *comma_iterate(char **list, int *len); 209 int comma_scan(char *optlist, char *opt, int clean); 210 int comma_scanall(char *optlist, char *scanlist); 211 int mountlist_istype(struct mtab_list *ml, char *typelist); 212 struct mtab_list *xgetmountlist(char *path); 213 214 // signal 215 216 void generic_signal(int signal); 217 void sigatexit(void *handler); 218 int sig_to_num(char *pidstr); 219 char *num_to_sig(int sig); 220 221 mode_t string_to_mode(char *mode_str, mode_t base); 222 void mode_to_string(mode_t mode, char *buf); 223 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)); 224 225 // Functions in need of further review/cleanup 226 #include "lib/pending.h" 227