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