1 /* lib.h - header file for lib directory 2 * 3 * Copyright 2006 Rob Landley <rob@landley.net> 4 */ 5 6 struct ptr_len { 7 void *ptr; 8 long len; 9 }; 10 11 struct str_len { 12 char *str; 13 long len; 14 }; 15 16 // llist.c 17 18 // All these list types can be handled by the same code because first element 19 // is always next pointer, so next = (mytype *)&struct. (The payloads are 20 // named differently to catch using the wrong type early.) 21 22 struct string_list { 23 struct string_list *next; 24 char str[0]; 25 }; 26 27 struct arg_list { 28 struct arg_list *next; 29 char *arg; 30 }; 31 32 struct double_list { 33 struct double_list *next, *prev; 34 char *data; 35 }; 36 37 void llist_free_arg(void *node); 38 void llist_free_double(void *node); 39 void llist_traverse(void *list, void (*using)(void *node)); 40 void *llist_pop(void *list); // actually void **list 41 void *dlist_pop(void *list); // actually struct double_list **list 42 void dlist_add_nomalloc(struct double_list **list, struct double_list *new); 43 struct double_list *dlist_add(struct double_list **list, char *data); 44 void *dlist_terminate(void *list); 45 46 // args.c 47 void get_optflags(void); 48 49 // dirtree.c 50 51 // Values returnable from callback function (bitfield, or them together) 52 // Default with no callback is 0 53 54 // Add this node to the tree 55 #define DIRTREE_SAVE 1 56 // Recurse into children 57 #define DIRTREE_RECURSE 2 58 // Call again after handling all children of this directory 59 // (Ignored for non-directories, sets linklen = -1 before second call.) 60 #define DIRTREE_COMEAGAIN 4 61 // Follow symlinks to directories 62 #define DIRTREE_SYMFOLLOW 8 63 // Don't warn about failure to stat 64 #define DIRTREE_SHUTUP 16 65 // Don't look at any more files in this directory. 66 #define DIRTREE_ABORT 256 67 68 #define DIRTREE_ABORTVAL ((struct dirtree *)1) 69 70 struct dirtree { 71 struct dirtree *next, *parent, *child; 72 long extra; // place for user to store their stuff (can be pointer) 73 struct stat st; 74 char *symlink; 75 int dirfd; 76 char again; 77 char name[]; 78 }; 79 80 struct dirtree *dirtree_start(char *name, int symfollow); 81 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags); 82 char *dirtree_path(struct dirtree *node, int *plen); 83 int dirtree_notdotdot(struct dirtree *catch); 84 int dirtree_parentfd(struct dirtree *node); 85 struct dirtree *dirtree_handle_callback(struct dirtree *new, 86 int (*callback)(struct dirtree *node)); 87 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node), 88 int symfollow); 89 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); 90 91 // help.c 92 93 void show_help(FILE *out); 94 95 // xwrap.c 96 void xstrncpy(char *dest, char *src, size_t size); 97 void xstrncat(char *dest, char *src, size_t size); 98 void xexit(void) noreturn; 99 void *xmalloc(size_t size); 100 void *xzalloc(size_t size); 101 void *xrealloc(void *ptr, size_t size); 102 char *xstrndup(char *s, size_t n); 103 char *xstrdup(char *s); 104 void *xmemdup(void *s, long len); 105 char *xmprintf(char *format, ...) printf_format; 106 void xprintf(char *format, ...) printf_format; 107 void xputs(char *s); 108 void xputc(char c); 109 void xflush(void); 110 void xexec(char **argv); 111 pid_t xpopen_both(char **argv, int *pipes); 112 int xwaitpid(pid_t pid); 113 int xpclose_both(pid_t pid, int *pipes); 114 pid_t xpopen(char **argv, int *pipe, int stdout); 115 pid_t xpclose(pid_t pid, int pipe); 116 int xrun(char **argv); 117 int xpspawn(char **argv, int*pipes); 118 void xaccess(char *path, int flags); 119 void xunlink(char *path); 120 int xcreate(char *path, int flags, int mode); 121 int xopen(char *path, int flags); 122 void xpipe(int *pp); 123 void xclose(int fd); 124 int xdup(int fd); 125 FILE *xfdopen(int fd, char *mode); 126 FILE *xfopen(char *path, char *mode); 127 size_t xread(int fd, void *buf, size_t len); 128 void xreadall(int fd, void *buf, size_t len); 129 void xwrite(int fd, void *buf, size_t len); 130 off_t xlseek(int fd, off_t offset, int whence); 131 char *xreadfile(char *name, char *buf, off_t len); 132 int xioctl(int fd, int request, void *data); 133 char *xgetcwd(void); 134 void xstat(char *path, struct stat *st); 135 char *xabspath(char *path, int exact); 136 void xchdir(char *path); 137 void xchroot(char *path); 138 struct passwd *xgetpwuid(uid_t uid); 139 struct group *xgetgrgid(gid_t gid); 140 struct passwd *xgetpwnam(char *name); 141 struct group *xgetgrnam(char *name); 142 struct passwd *xgetpwnamid(char *user); 143 struct group *xgetgrnamid(char *group); 144 void xsetuser(struct passwd *pwd); 145 char *xreadlink(char *name); 146 long xparsetime(char *arg, long units, long *fraction); 147 void xpidfile(char *name); 148 void xregcomp(regex_t *preg, char *rexec, int cflags); 149 char *xtzset(char *new); 150 void xsignal(int signal, void *handler); 151 152 // lib.c 153 void verror_msg(char *msg, int err, va_list va); 154 void error_msg(char *msg, ...) printf_format; 155 void perror_msg(char *msg, ...) printf_format; 156 void error_exit(char *msg, ...) printf_format noreturn; 157 void perror_exit(char *msg, ...) printf_format noreturn; 158 void help_exit(char *msg, ...) printf_format noreturn; 159 void error_msg_raw(char *msg); 160 void perror_msg_raw(char *msg); 161 void error_exit_raw(char *msg); 162 void perror_exit_raw(char *msg); 163 ssize_t readall(int fd, void *buf, size_t len); 164 ssize_t writeall(int fd, void *buf, size_t len); 165 off_t lskip(int fd, off_t offset); 166 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags); 167 struct string_list **splitpath(char *path, struct string_list **list); 168 char *readfileat(int dirfd, char *name, char *buf, off_t *len); 169 char *readfile(char *name, char *buf, off_t len); 170 void msleep(long miliseconds); 171 int64_t peek_le(void *ptr, unsigned size); 172 int64_t peek_be(void *ptr, unsigned size); 173 int64_t peek(void *ptr, unsigned size); 174 void poke(void *ptr, uint64_t val, int size); 175 struct string_list *find_in_path(char *path, char *filename); 176 long estrtol(char *str, char **end, int base); 177 long xstrtol(char *str, char **end, int base); 178 long atolx(char *c); 179 long atolx_range(char *numstr, long low, long high); 180 int stridx(char *haystack, char needle); 181 char *strlower(char *s); 182 char *strafter(char *haystack, char *needle); 183 char *chomp(char *s); 184 int unescape(char c); 185 int strstart(char **a, char *b); 186 off_t fdlength(int fd); 187 void loopfiles_rw(char **argv, int flags, int permissions, int failok, 188 void (*function)(int fd, char *name)); 189 void loopfiles(char **argv, void (*function)(int fd, char *name)); 190 void xsendfile(int in, int out); 191 int wfchmodat(int rc, char *name, mode_t mode); 192 int copy_tempfile(int fdin, char *name, char **tempname); 193 void delete_tempfile(int fdin, int fdout, char **tempname); 194 void replace_tempfile(int fdin, int fdout, char **tempname); 195 void crc_init(unsigned int *crc_table, int little_endian); 196 void base64_init(char *p); 197 int yesno(int def); 198 int qstrcmp(const void *a, const void *b); 199 void create_uuid(char *uuid); 200 char *show_uuid(char *uuid); 201 char *next_printf(char *s, char **start); 202 char *strnstr(char *line, char *str); 203 204 #define HR_SPACE 1 // Space between number and units 205 #define HR_B 2 // Use "B" for single byte units 206 #define HR_1000 4 // Use decimal instead of binary units 207 int human_readable(char *buf, unsigned long long num, int style); 208 209 // linestack.c 210 211 struct linestack { 212 long len, max; 213 struct ptr_len idx[]; 214 }; 215 216 void linestack_addstack(struct linestack **lls, struct linestack *throw, 217 long pos); 218 void linestack_insert(struct linestack **lls, long pos, char *line, long len); 219 void linestack_append(struct linestack **lls, char *line); 220 struct linestack *linestack_load(char *name); 221 int crunch_str(char **str, int width, FILE *out, 222 int (*escout)(FILE *out, int cols, char **buf)); 223 int draw_str(char *start, int width); 224 int utf8len(char *str); 225 int utf8skip(char *str, int width); 226 int draw_trim(char *str, int padto, int width); 227 228 // interestingtimes.c 229 int xgettty(void); 230 int terminal_size(unsigned *xx, unsigned *yy); 231 int terminal_probesize(unsigned *xx, unsigned *yy); 232 int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy); 233 int set_terminal(int fd, int raw, struct termios *old); 234 void xset_terminal(int fd, int raw, struct termios *old); 235 int scan_key(char *scratch, int miliwait); 236 void tty_esc(char *s); 237 void tty_jump(int x, int y); 238 void tty_reset(void); 239 void tty_sigreset(int i); 240 241 // net.c 242 int xsocket(int domain, int type, int protocol); 243 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len); 244 int xconnect(char *host, char *port, int family, int socktype, int protocol, 245 int flags); 246 int xpoll(struct pollfd *fds, int nfds, int timeout); 247 248 // password.c 249 int get_salt(char *salt, char * algo); 250 251 // getmountlist.c 252 struct mtab_list { 253 struct mtab_list *next, *prev; 254 struct stat stat; 255 struct statvfs statvfs; 256 char *dir; 257 char *device; 258 char *opts; 259 char type[0]; 260 }; 261 262 void comma_args(struct arg_list *al, void *data, char *err, 263 char *(*callback)(void *data, char *str, int len)); 264 void comma_collate(char **old, char *new); 265 char *comma_iterate(char **list, int *len); 266 int comma_scan(char *optlist, char *opt, int clean); 267 int comma_scanall(char *optlist, char *scanlist); 268 int mountlist_istype(struct mtab_list *ml, char *typelist); 269 struct mtab_list *xgetmountlist(char *path); 270 271 // signal 272 273 void generic_signal(int signal); 274 void sigatexit(void *handler); 275 int sig_to_num(char *pidstr); 276 char *num_to_sig(int sig); 277 278 mode_t string_to_mode(char *mode_str, mode_t base); 279 void mode_to_string(mode_t mode, char *buf); 280 char *basename_r(char *name); 281 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)); 282 283 pid_t xvforkwrap(pid_t pid); 284 #define XVFORK() xvforkwrap(vfork()) 285 286 // Wrapper to make xfuncs() return (via longjmp) instead of exiting. 287 // Assigns true/false "did it exit" value to first argument. 288 #define WOULD_EXIT(y, x) do { jmp_buf _noexit; \ 289 int _noexit_res; \ 290 toys.rebound = &_noexit; \ 291 _noexit_res = setjmp(_noexit); \ 292 if (!_noexit_res) do {x;} while(0); \ 293 toys.rebound = 0; \ 294 y = _noexit_res; \ 295 } while(0); 296 297 // Wrapper that discards true/false "did it exit" value. 298 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x) 299 300 // Functions in need of further review/cleanup 301 #include "lib/pending.h" 302