1 /* lib.h - header file for lib directory 2 * 3 * Copyright 2006 Rob Landley <[email protected]> 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[]; 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 struct dev_ino { 28 dev_t dev; 29 ino_t ino; 30 }; 31 32 void llist_free_arg(void *node); 33 void llist_free_double(void *node); 34 void llist_traverse(void *list, void (*using)(void *node)); 35 void *llist_pop(void *list); // actually void **list 36 void *dlist_pop(void *list); // actually struct double_list **list 37 void *dlist_lpop(void *list); // also struct double_list **list 38 void dlist_add_nomalloc(struct double_list **list, struct double_list *new); 39 struct double_list *dlist_add(struct double_list **list, char *data); 40 void *dlist_terminate(void *list); 41 42 // args.c 43 #define FLAGS_NODASH (1LL<<63) 44 void get_optflags(void); 45 46 // dirtree.c 47 48 // Values returnable from callback function (bitfield, or them together) 49 // Default with no callback is 0 50 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.) 57 #define DIRTREE_COMEAGAIN 4 58 // Follow symlinks to directories 59 #define DIRTREE_SYMFOLLOW 8 60 // Don't warn about failure to stat 61 #define DIRTREE_SHUTUP 16 62 // Breadth first traversal, conserves filehandles at the expense of memory 63 #define DIRTREE_BREADTH 32 // TODO not implemented yet 64 // skip non-numeric entries 65 #define DIRTREE_PROC 64 66 // Return files we can't stat 67 #define DIRTREE_STATLESS 128 68 // Don't look at any more files in this directory. 69 #define DIRTREE_ABORT 256 70 71 #define DIRTREE_ABORTVAL ((struct dirtree *)1) 72 73 struct dirtree { 74 struct dirtree *next, *parent, *child; 75 long extra; // place for user to store their stuff (can be pointer) 76 char *symlink; 77 int dirfd; 78 struct stat st; 79 char again, name[]; 80 }; 81 82 int isdotdot(char *name); 83 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags); 84 char *dirtree_path(struct dirtree *node, int *plen); 85 int dirtree_notdotdot(struct dirtree *catch); 86 int dirtree_parentfd(struct dirtree *node); 87 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node), 88 int dirfd, int symfollow); 89 struct dirtree *dirtree_flagread(char *path, int flags, 90 int (*callback)(struct dirtree *node)); 91 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); 92 93 // Tell xopen and friends to print warnings but return -1 as necessary 94 // The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so 95 // plenty of headroom. 96 #define WARN_ONLY (1<<31) // don't exit, just warn 97 #define LOOPFILES_ANYWAY (1<<30) // call function with fd -1 98 99 // xabspath flags 100 #define ABS_PATH 1 // all but last path component must exist 101 #define ABS_FILE 2 // last path component must exist 102 #define ABS_KEEP 4 // don't resolve symlinks in path to last component 103 #define ABS_LAST 8 // don't resolve symlink in last path component 104 105 // xwrap.c 106 void xstrncpy(char *dest, char *src, size_t size); 107 void xstrncat(char *dest, char *src, size_t size); 108 _Noreturn void _xexit(void); 109 _Noreturn void xexit(void); 110 void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off); 111 void *xmalloc(size_t size); 112 void *xzalloc(size_t size); 113 void *xrealloc(void *ptr, size_t size); 114 char *xstrndup(char *s, size_t n); 115 char *xstrdup(char *s); 116 void *xmemdup(void *s, long len); 117 char *xmprintf(char *format, ...) printf_format; 118 void xferror(FILE *fp); 119 void xprintf(char *format, ...) printf_format; 120 void xputsl(char *s, int len); 121 void xputsn(char *s); 122 void xputs(char *s); 123 void xputc(char c); 124 void xvdaemon(void); 125 void xexec(char **argv); 126 pid_t xpopen_setup(char **argv, int *pipes, void (*callback)(char **argv)); 127 pid_t xpopen_both(char **argv, int *pipes); 128 int xwaitpid(pid_t pid); 129 int xpclose_both(pid_t pid, int *pipes); 130 pid_t xpopen(char **argv, int *pipe, int isstdout); 131 pid_t xpclose(pid_t pid, int pipe); 132 int xrun(char **argv); 133 char *xrunread(char *argv[], char *to_stdin); 134 int xpspawn(char **argv, int*pipes); 135 void xaccess(char *path, int flags); 136 void xunlink(char *path); 137 void xrename(char *from, char *to); 138 int xtempfile(char *name, char **tempname); 139 int xcreate(char *path, int flags, int mode); 140 int xopen(char *path, int flags); 141 int xcreate_stdio(char *path, int flags, int mode); 142 int xopen_stdio(char *path, int flags); 143 int openro(char *path, int flags); 144 int xopenro(char *path); 145 void xpipe(int *pp); 146 void xclose(int fd); 147 int xdup(int fd); 148 int xnotstdio(int fd); 149 FILE *xfdopen(int fd, char *mode); 150 FILE *xfopen(char *path, char *mode); 151 size_t xread(int fd, void *buf, size_t len); 152 void xreadall(int fd, void *buf, size_t len); 153 void xwrite(int fd, void *buf, size_t len); 154 off_t xlseek(int fd, off_t offset, int whence); 155 char *xreadfile(char *name, char *buf, off_t len); 156 int xioctl(int fd, int request, void *data); 157 char *xgetcwd(void); 158 void xstat(char *path, struct stat *st); 159 char *xabspath(char *path, int exact); 160 void xchdir(char *path); 161 void xchroot(char *path); 162 struct passwd *xgetpwuid(uid_t uid); 163 struct group *xgetgrgid(gid_t gid); 164 struct passwd *xgetpwnam(char *name); 165 struct group *xgetgrnam(char *name); 166 unsigned xgetuid(char *name); 167 unsigned xgetgid(char *name); 168 void xsetuser(struct passwd *pwd); 169 char *xreadlinkat(int dir, char *name); 170 char *xreadlink(char *name); 171 double xstrtod(char *s); 172 long xparsetime(char *arg, long units, long *fraction); 173 void xparsetimespec(char *arg, struct timespec *ts); 174 long long xparsemillitime(char *arg); 175 void xpidfile(char *name); 176 void xregcomp(regex_t *preg, char *rexec, int cflags); 177 char *xtzset(char *new); 178 void xsignal_flags(int signal, void *handler, int flags); 179 void xsignal(int signal, void *handler); 180 time_t xvali_date(struct tm *tm, char *str); 181 void xparsedate(char *str, time_t *t, unsigned *nano, int endian); 182 char *xgetdelim(FILE *fp, int delim); 183 char *xgetline(FILE *fp); 184 time_t xmktime(struct tm *tm, int utc); 185 186 // lib.c 187 void verror_msg(char *msg, int err, va_list va); 188 void error_msg(char *msg, ...) printf_format; 189 void perror_msg(char *msg, ...) printf_format; 190 _Noreturn void error_exit(char *msg, ...) printf_format; 191 _Noreturn void perror_exit(char *msg, ...) printf_format; 192 _Noreturn void help_exit(char *msg, ...) printf_format; 193 void error_msg_raw(char *msg); 194 void perror_msg_raw(char *msg); 195 _Noreturn void error_exit_raw(char *msg); 196 _Noreturn void perror_exit_raw(char *msg); 197 ssize_t readall(int fd, void *buf, size_t len); 198 ssize_t writeall(int fd, void *buf, size_t len); 199 off_t lskip(int fd, off_t offset); 200 #define MKPATHAT_MKLAST 1 201 #define MKPATHAT_MAKE 2 202 #define MKPATHAT_VERBOSE 4 203 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags); 204 int mkpath(char *dir); 205 struct string_list **splitpath(char *path, struct string_list **list); 206 char *readfd(int fd, char *ibuf, off_t *plen); 207 char *readfileat(int dirfd, char *name, char *buf, off_t *len); 208 char *readfile(char *name, char *buf, off_t len); 209 void msleep(long milliseconds); 210 void nanomove(struct timespec *ts, long long offset); 211 long long nanodiff(struct timespec *old, struct timespec *new); 212 int highest_bit(unsigned long l); 213 long long peek_le(void *ptr, unsigned size); 214 long long peek_be(void *ptr, unsigned size); 215 long long peek(void *ptr, unsigned size); 216 void poke_le(void *ptr, long long val, unsigned size); 217 void poke_be(void *ptr, long long val, unsigned size); 218 void poke(void *ptr, long long val, unsigned size); 219 struct string_list *find_in_path(char *path, char *filename); 220 long long estrtol(char *str, char **end, int base); 221 long long xstrtol(char *str, char **end, int base); 222 long long atolx(char *c); 223 long long atolx_range(char *numstr, long long low, long long high); 224 int stridx(char *haystack, char needle); 225 int wctoutf8(char *s, unsigned wc); 226 int utf8towc(unsigned *wc, char *str, unsigned len); 227 char *strlower(char *s); 228 char *strafter(char *haystack, char *needle); 229 char *chomp(char *s); 230 int unescape(char c); 231 int unescape2(char **c, int echo); 232 char *strend(char *str, char *suffix); 233 int strstart(char **a, char *b); 234 int strcasestart(char **a, char *b); 235 int anystart(char *s, char **try); 236 int same_file(struct stat *st1, struct stat *st2); 237 int same_dev_ino(struct stat *st, struct dev_ino *di); 238 off_t fdlength(int fd); 239 void loopfiles_rw(char **argv, int flags, int permissions, 240 void (*function)(int fd, char *name)); 241 void loopfiles(char **argv, void (*function)(int fd, char *name)); 242 void loopfiles_lines(char **argv, void (*function)(char **pline, long len)); 243 long long sendfile_len(int in, int out, long long len, long long *consumed); 244 long long xsendfile_len(int in, int out, long long len); 245 void xsendfile_pad(int in, int out, long long len); 246 long long xsendfile(int in, int out); 247 int wfchmodat(int rc, char *name, mode_t mode); 248 int copy_tempfile(int fdin, char *name, char **tempname); 249 void delete_tempfile(int fdin, int fdout, char **tempname); 250 void replace_tempfile(int fdin, int fdout, char **tempname); 251 void crc_init(unsigned *crc_table, int little_endian); 252 void base64_init(char *p); 253 int yesno(int def); 254 int fyesno(FILE *fp, int def); 255 int qstrcmp(const void *a, const void *b); 256 void create_uuid(char *uuid); 257 char *show_uuid(char *uuid); 258 char *next_printf(char *s, char **start); 259 struct passwd *bufgetpwnamuid(char *name, uid_t uid); 260 struct passwd *bufgetpwuid(uid_t uid); 261 struct group *bufgetgrnamgid(char *name, gid_t gid); 262 struct group *bufgetgrgid(gid_t gid); 263 int readlinkat0(int dirfd, char *path, char *buf, int len); 264 int readlink0(char *path, char *buf, int len); 265 int regexec0(regex_t *preg, char *string, long len, int nmatch, 266 regmatch_t *pmatch, int eflags); 267 char *getusername(uid_t uid); 268 char *getgroupname(gid_t gid); 269 void do_lines(int fd, char delim, void (*call)(char **pline, long len)); 270 long long millitime(void); 271 char *format_iso_time(char *buf, size_t len, struct timespec *ts); 272 void loggit(int priority, char *format, ...); 273 unsigned tar_cksum(void *data); 274 int is_tar_header(void *pkt); 275 void octal_deslash(char *s); 276 int smemcmp(char *one, char *two, unsigned long len); 277 278 #define HR_SPACE 1 // Space between number and units 279 #define HR_B 2 // Use "B" for single byte units 280 #define HR_1000 4 // Use decimal instead of binary units 281 #define HR_NODOT 8 // No tenths for single digit units 282 int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, 283 int style); 284 int human_readable(char *buf, unsigned long long num, int style); 285 286 // elf.c 287 288 char *elf_arch_name(int type); 289 void elf_print_flags(int arch, int flags); 290 291 // hash.c 292 293 void hash_by_name(int fd, char *name, char *result); 294 295 // env.c 296 297 long environ_bytes(void); 298 char *xsetenv(char *name, char *val); 299 void xunsetenv(char *name); 300 char *xpop_env(char *name); // because xpopenv() looks like xpopen_v() 301 void xclearenv(void); 302 void reset_env(struct passwd *p, int clear); 303 304 // utf8.c 305 306 int crunch_escape(FILE *out, int cols, int wc); 307 int crunch_rev_escape(FILE *out, int cols, int wc); 308 int crunch_str(char **str, int width, FILE *out, char *escmore, 309 int (*escout)(FILE *out, int cols, int wc)); 310 int draw_str(char *start, int width); 311 int utf8len(char *str); 312 int utf8skip(char *str, int width); 313 int draw_trim_esc(char *str, int padto, int width, char *escmore, 314 int (*escout)(FILE *out, int cols,int wc)); 315 int draw_trim(char *str, int padto, int width); 316 317 // tty.c 318 int tty_fd(void); 319 int terminal_size(unsigned *xx, unsigned *yy); 320 int terminal_probesize(unsigned *xx, unsigned *yy); 321 #define KEY_UP 0 322 #define KEY_DOWN 1 323 #define KEY_RIGHT 2 324 #define KEY_LEFT 3 325 #define KEY_PGUP 4 326 #define KEY_PGDN 5 327 #define KEY_HOME 6 328 #define KEY_END 7 329 #define KEY_INSERT 8 330 #define KEY_DELETE 9 331 #define KEY_FN 10 // F1 = KEY_FN+1, F2 = KEY_FN+2, ... 332 #define KEY_SHIFT (1<<16) 333 #define KEY_CTRL (1<<17) 334 #define KEY_ALT (1<<18) 335 int scan_key(char *scratch, int timeout_ms); 336 int scan_key_getsize(char *scratch, int timeout_ms, unsigned *xx, unsigned *yy); 337 unsigned cfspeed2bps(unsigned speed); 338 unsigned bps2cfspeed(unsigned baud); 339 void xsetspeed(struct termios *tio, int speed); 340 int set_terminal(int fd, int raw, int speed, struct termios *old); 341 void xset_terminal(int fd, int raw, int speed, struct termios *old); 342 void tty_reset(void); 343 void tty_sigreset(int i); 344 void start_redraw(unsigned *width, unsigned *height); 345 346 // net.c 347 348 union socksaddr { 349 struct sockaddr s; 350 struct sockaddr_in in; 351 struct sockaddr_in6 in6; 352 }; 353 354 int xsocket(int domain, int type, int protocol); 355 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len); 356 struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype, 357 int protocol, int flags); 358 void xbind(int fd, const struct sockaddr *sa, socklen_t len); 359 void xconnect(int fd, const struct sockaddr *sa, socklen_t len); 360 int xconnectany(struct addrinfo *ai); 361 int xbindany(struct addrinfo *ai); 362 int xpoll(struct pollfd *fds, int nfds, int timeout); 363 int pollinate(int in1, int in2, int out1, int out2, 364 void (*callback)(int fd, void *buf, size_t len), 365 int timeout, int shutdown_timeout); 366 char *ntop(struct sockaddr *sa); 367 void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest); 368 int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout); 369 char *escape_url(char *str, char *and); 370 char *unescape_url(char *str, int do_cut); 371 372 // password.c 373 int get_salt(char *salt, char *algo, int rand); 374 int read_password(char *buff, int buflen, char *mesg); 375 char **get_userline(char *filename, char *username); 376 int update_password(char *filename, char *username, char *entry, int pos); 377 378 // commas.c 379 void comma_args(struct arg_list *al, void *data, char *err, 380 char *(*callback)(void *data, char *str, int len)); 381 void comma_collate(char **old, char *new); 382 char *comma_iterate(char **list, int *len); 383 int comma_scan(char *optlist, char *opt, int clean); 384 int comma_scanall(char *optlist, char *scanlist); 385 int comma_remove(char *optlist, char *opt); 386 387 // deflate.c 388 389 long long gzip_fd(int infd, int outfd); 390 long long gunzip_fd(int infd, int outfd); 391 long long gunzip_mem(char *inbuf, int inlen, char *outbuf, int outlen); 392 393 // getmountlist.c 394 struct mtab_list { 395 struct mtab_list *next, *prev; 396 struct stat stat; 397 struct statvfs statvfs; 398 char *dir; 399 char *device; 400 char *opts; 401 char type[0]; 402 }; 403 404 int mountlist_istype(struct mtab_list *ml, char *typelist); 405 struct mtab_list *xgetmountlist(char *path); 406 407 // signal 408 409 void generic_signal(int signal); 410 void exit_signal(int signal); 411 void sigatexit(void *handler); 412 void list_signals(void); 413 414 unsigned string_to_mode(char *mode_str, unsigned base); 415 void mode_to_string(unsigned mode, char *buf); 416 char *getbasename(char *name); 417 char *fileunderdir(char *file, char *dir); 418 void *mepcpy(void *from, void *to, unsigned long len); 419 char *relative_path(char *from, char *to, int abs); 420 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name), 421 int scripts); 422 423 pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid); 424 #define XVFORK() xvforkwrap(vfork()) 425 426 // Wrapper to make xfuncs() return (via siglongjmp) instead of exiting. 427 // Assigns true/false "did it exit" value to first argument. 428 #define WOULD_EXIT(y, x) do { sigjmp_buf _noexit; \ 429 int _noexit_res; \ 430 toys.rebound = &_noexit; \ 431 _noexit_res = sigsetjmp(_noexit, 1); \ 432 if (!_noexit_res) do {x;} while(0); \ 433 toys.rebound = 0; \ 434 y = _noexit_res; \ 435 } while(0) 436 437 // Wrapper that discards true/false "did it exit" value. 438 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x) 439 440 #define minof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa<bb ? aa : bb;}) 441 #define maxof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa>bb ? aa : bb;}) 442