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