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[]; 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 anystr(char *s, char **try); 237 int same_file(struct stat *st1, struct stat *st2); 238 int same_dev_ino(struct stat *st, struct dev_ino *di); 239 off_t fdlength(int fd); 240 void loopfiles_rw(char **argv, int flags, int permissions, 241 void (*function)(int fd, char *name)); 242 void loopfiles(char **argv, void (*function)(int fd, char *name)); 243 void loopfiles_lines(char **argv, void (*function)(char **pline, long len)); 244 long long sendfile_len(int in, int out, long long len, long long *consumed); 245 long long xsendfile_len(int in, int out, long long len); 246 void xsendfile_pad(int in, int out, long long len); 247 long long xsendfile(int in, int out); 248 int wfchmodat(int rc, char *name, mode_t mode); 249 int copy_tempfile(int fdin, char *name, char **tempname); 250 void delete_tempfile(int fdin, int fdout, char **tempname); 251 void replace_tempfile(int fdin, int fdout, char **tempname); 252 void crc_init(unsigned *crc_table, int little_endian); 253 void base64_init(char *p); 254 int yesno(int def); 255 int fyesno(FILE *fp, int def); 256 int qstrcmp(const void *a, const void *b); 257 void create_uuid(char *uuid); 258 char *show_uuid(char *uuid); 259 char *next_printf(char *s, char **start); 260 struct passwd *bufgetpwnamuid(char *name, uid_t uid); 261 struct passwd *bufgetpwuid(uid_t uid); 262 struct group *bufgetgrnamgid(char *name, gid_t gid); 263 struct group *bufgetgrgid(gid_t gid); 264 int readlinkat0(int dirfd, char *path, char *buf, int len); 265 int readlink0(char *path, char *buf, int len); 266 int regexec0(regex_t *preg, char *string, long len, int nmatch, 267 regmatch_t *pmatch, int eflags); 268 char *getusername(uid_t uid); 269 char *getgroupname(gid_t gid); 270 void do_lines(int fd, char delim, void (*call)(char **pline, long len)); 271 long long millitime(void); 272 char *format_iso_time(char *buf, size_t len, struct timespec *ts); 273 void loggit(int priority, char *format, ...); 274 unsigned tar_cksum(void *data); 275 int is_tar_header(void *pkt); 276 void octal_deslash(char *s); 277 int smemcmp(char *one, char *two, unsigned long len); 278 279 #define HR_SPACE 1 // Space between number and units 280 #define HR_B 2 // Use "B" for single byte units 281 #define HR_1000 4 // Use decimal instead of binary units 282 #define HR_NODOT 8 // No tenths for single digit units 283 int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, 284 int style); 285 int human_readable(char *buf, unsigned long long num, int style); 286 287 // elf.c 288 289 char *elf_arch_name(int type); 290 void elf_print_flags(int arch, int flags); 291 292 // hash.c 293 294 void hash_by_name(int fd, char *name, char *result); 295 296 // env.c 297 298 long environ_bytes(void); 299 char *xsetenv(char *name, char *val); 300 void xunsetenv(char *name); 301 char *xpop_env(char *name); // because xpopenv() looks like xpopen_v() 302 void xclearenv(void); 303 void reset_env(struct passwd *p, int clear); 304 305 // utf8.c 306 307 int crunch_escape(FILE *out, int cols, int wc); 308 int crunch_rev_escape(FILE *out, int cols, int wc); 309 int crunch_str(char **str, int width, FILE *out, char *escmore, 310 int (*escout)(FILE *out, int cols, int wc)); 311 int draw_str(char *start, int width); 312 int utf8len(char *str); 313 int utf8skip(char *str, int width); 314 int draw_trim_esc(char *str, int padto, int width, char *escmore, 315 int (*escout)(FILE *out, int cols,int wc)); 316 int draw_trim(char *str, int padto, int width); 317 318 // tty.c 319 int tty_fd(void); 320 int terminal_size(unsigned *xx, unsigned *yy); 321 int terminal_probesize(unsigned *xx, unsigned *yy); 322 #define KEY_UP 0 323 #define KEY_DOWN 1 324 #define KEY_RIGHT 2 325 #define KEY_LEFT 3 326 #define KEY_PGUP 4 327 #define KEY_PGDN 5 328 #define KEY_HOME 6 329 #define KEY_END 7 330 #define KEY_INSERT 8 331 #define KEY_DELETE 9 332 #define KEY_FN 10 // F1 = KEY_FN+1, F2 = KEY_FN+2, ... 333 #define KEY_SHIFT (1<<16) 334 #define KEY_CTRL (1<<17) 335 #define KEY_ALT (1<<18) 336 int scan_key(char *scratch, int timeout_ms); 337 int scan_key_getsize(char *scratch, int timeout_ms, unsigned *xx, unsigned *yy); 338 unsigned cfspeed2bps(unsigned speed); 339 unsigned bps2cfspeed(unsigned baud); 340 void xsetspeed(struct termios *tio, int speed); 341 int set_terminal(int fd, int raw, int speed, struct termios *old); 342 void xset_terminal(int fd, int raw, int speed, struct termios *old); 343 void tty_reset(void); 344 void tty_sigreset(int i); 345 void start_redraw(unsigned *width, unsigned *height); 346 347 // net.c 348 349 union socksaddr { 350 struct sockaddr s; 351 struct sockaddr_in in; 352 struct sockaddr_in6 in6; 353 }; 354 355 int xsocket(int domain, int type, int protocol); 356 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len); 357 struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype, 358 int protocol, int flags); 359 void xbind(int fd, const struct sockaddr *sa, socklen_t len); 360 void xconnect(int fd, const struct sockaddr *sa, socklen_t len); 361 int xconnectany(struct addrinfo *ai); 362 int xbindany(struct addrinfo *ai); 363 int xpoll(struct pollfd *fds, int nfds, int timeout); 364 int pollinate(int in1, int in2, int out1, int out2, 365 void (*callback)(int fd, void *buf, size_t len), 366 int timeout, int shutdown_timeout); 367 char *ntop(struct sockaddr *sa); 368 void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest); 369 int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout); 370 char *escape_url(char *str, char *and); 371 char *unescape_url(char *str, int do_cut); 372 373 // password.c 374 int get_salt(char *salt, char *algo, int rand); 375 int read_password(char *buff, int buflen, char *mesg); 376 char **get_userline(char *filename, char *username); 377 int update_password(char *filename, char *username, char *entry, int pos); 378 379 // commas.c 380 void comma_args(struct arg_list *al, void *data, char *err, 381 char *(*callback)(void *data, char *str, int len)); 382 void comma_collate(char **old, char *new); 383 char *comma_iterate(char **list, int *len); 384 int comma_scan(char *optlist, char *opt, int clean); 385 int comma_scanall(char *optlist, char *scanlist); 386 int comma_remove(char *optlist, char *opt); 387 388 // deflate.c 389 390 long long gzip_fd(int infd, int outfd); 391 long long gunzip_fd(int infd, int outfd); 392 long long gunzip_mem(char *inbuf, int inlen, char *outbuf, int outlen); 393 394 // getmountlist.c 395 struct mtab_list { 396 struct mtab_list *next, *prev; 397 struct stat stat; 398 struct statvfs statvfs; 399 char *dir; 400 char *device; 401 char *opts; 402 char type[0]; 403 }; 404 405 int mountlist_istype(struct mtab_list *ml, char *typelist); 406 struct mtab_list *xgetmountlist(char *path); 407 408 // signal 409 410 void generic_signal(int signal); 411 void exit_signal(int signal); 412 void sigatexit(void *handler); 413 void list_signals(void); 414 415 unsigned string_to_mode(char *mode_str, unsigned base); 416 void mode_to_string(unsigned mode, char *buf); 417 char *getbasename(char *name); 418 char *fileunderdir(char *file, char *dir); 419 void *mepcpy(void *from, void *to, unsigned long len); 420 char *relative_path(char *from, char *to, int abs); 421 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name), 422 int scripts); 423 424 pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid); 425 #define XVFORK() xvforkwrap(vfork()) 426 427 // Wrapper to make xfuncs() return (via siglongjmp) instead of exiting. 428 // Assigns true/false "did it exit" value to first argument. 429 #define WOULD_EXIT(y, x) do { sigjmp_buf _noexit; \ 430 int _noexit_res; \ 431 toys.rebound = &_noexit; \ 432 _noexit_res = sigsetjmp(_noexit, 1); \ 433 if (!_noexit_res) do {x;} while(0); \ 434 toys.rebound = 0; \ 435 y = _noexit_res; \ 436 } while(0) 437 438 // Wrapper that discards true/false "did it exit" value. 439 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x) 440 441 #define minof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa<bb ? aa : bb;}) 442 #define maxof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa>bb ? aa : bb;}) 443