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) noreturn;
 118void xexit(void) 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 xputs(char *s);
 129void xputc(char c);
 130void xflush(void);
 131void xexec(char **argv);
 132pid_t xpopen_both(char **argv, int *pipes);
 133int xwaitpid(pid_t pid);
 134int xpclose_both(pid_t pid, int *pipes);
 135pid_t xpopen(char **argv, int *pipe, int isstdout);
 136pid_t xpclose(pid_t pid, int pipe);
 137int xrun(char **argv);
 138int xpspawn(char **argv, int*pipes);
 139void xaccess(char *path, int flags);
 140void xunlink(char *path);
 141int xtempfile(char *name, char **tempname);
 142int xcreate(char *path, int flags, int mode);
 143int xopen(char *path, int flags);
 144int xcreate_stdio(char *path, int flags, int mode);
 145int xopen_stdio(char *path, int flags);
 146int openro(char *path, int flags);
 147int xopenro(char *path);
 148void xpipe(int *pp);
 149void xclose(int fd);
 150int xdup(int fd);
 151int notstdio(int fd);
 152FILE *xfdopen(int fd, char *mode);
 153FILE *xfopen(char *path, char *mode);
 154size_t xread(int fd, void *buf, size_t len);
 155void xreadall(int fd, void *buf, size_t len);
 156void xwrite(int fd, void *buf, size_t len);
 157off_t xlseek(int fd, off_t offset, int whence);
 158char *xreadfile(char *name, char *buf, off_t len);
 159int xioctl(int fd, int request, void *data);
 160char *xgetcwd(void);
 161void xstat(char *path, struct stat *st);
 162char *xabspath(char *path, int exact);
 163void xchdir(char *path);
 164void xchroot(char *path);
 165struct passwd *xgetpwuid(uid_t uid);
 166struct group *xgetgrgid(gid_t gid);
 167struct passwd *xgetpwnam(char *name);
 168struct group *xgetgrnam(char *name);
 169unsigned xgetuid(char *name);
 170unsigned xgetgid(char *name);
 171void xsetuser(struct passwd *pwd);
 172char *xreadlink(char *name);
 173double xstrtod(char *s);
 174long xparsetime(char *arg, long units, long *fraction);
 175long long xparsemillitime(char *arg);
 176void xpidfile(char *name);
 177void xregcomp(regex_t *preg, char *rexec, int cflags);
 178char *xtzset(char *new);
 179void xsignal_flags(int signal, void *handler, int flags);
 180void xsignal(int signal, void *handler);
 182// lib.c
 183void verror_msg(char *msg, int err, va_list va);
 184void error_msg(char *msg, ...) printf_format;
 185void perror_msg(char *msg, ...) printf_format;
 186void error_exit(char *msg, ...) printf_format noreturn;
 187void perror_exit(char *msg, ...) printf_format noreturn;
 188void help_exit(char *msg, ...) printf_format noreturn;
 189void error_msg_raw(char *msg);
 190void perror_msg_raw(char *msg);
 191void error_exit_raw(char *msg);
 192void perror_exit_raw(char *msg);
 193ssize_t readall(int fd, void *buf, size_t len);
 194ssize_t writeall(int fd, void *buf, size_t len);
 195off_t lskip(int fd, off_t offset);
 196#define MKPATHAT_MKLAST  1
 197#define MKPATHAT_MAKE    2
 198#define MKPATHAT_VERBOSE 4
 199int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
 200int mkpath(char *dir);
 201struct string_list **splitpath(char *path, struct string_list **list);
 202char *readfileat(int dirfd, char *name, char *buf, off_t *len);
 203char *readfile(char *name, char *buf, off_t len);
 204void msleep(long miliseconds);
 205int highest_bit(unsigned long l);
 206int64_t peek_le(void *ptr, unsigned size);
 207int64_t peek_be(void *ptr, unsigned size);
 208int64_t peek(void *ptr, unsigned size);
 209void poke(void *ptr, uint64_t val, int size);
 210struct string_list *find_in_path(char *path, char *filename);
 211long long estrtol(char *str, char **end, int base);
 212long long xstrtol(char *str, char **end, int base);
 213long long atolx(char *c);
 214long long atolx_range(char *numstr, long long low, long long high);
 215int stridx(char *haystack, char needle);
 216int utf8towc(wchar_t *wc, char *str, unsigned len);
 217char *strlower(char *s);
 218char *strafter(char *haystack, char *needle);
 219char *chomp(char *s);
 220int unescape(char c);
 221char *strend(char *str, char *suffix);
 222int strstart(char **a, char *b);
 223off_t fdlength(int fd);
 224void loopfiles_rw(char **argv, int flags, int permissions,
 225  void (*function)(int fd, char *name));
 226void loopfiles(char **argv, void (*function)(int fd, char *name));
 227void loopfiles_lines(char **argv, void (*function)(char **pline, long len));
 228long long xsendfile(int in, int out);
 229int wfchmodat(int rc, char *name, mode_t mode);
 230int copy_tempfile(int fdin, char *name, char **tempname);
 231void delete_tempfile(int fdin, int fdout, char **tempname);
 232void replace_tempfile(int fdin, int fdout, char **tempname);
 233void crc_init(unsigned int *crc_table, int little_endian);
 234void base64_init(char *p);
 235int yesno(int def);
 236int qstrcmp(const void *a, const void *b);
 237void create_uuid(char *uuid);
 238char *show_uuid(char *uuid);
 239char *next_printf(char *s, char **start);
 240char *strnstr(char *line, char *str);
 241int dev_minor(int dev);
 242int dev_major(int dev);
 243int dev_makedev(int major, int minor);
 244struct passwd *bufgetpwuid(uid_t uid);
 245struct group *bufgetgrgid(gid_t gid);
 246int readlinkat0(int dirfd, char *path, char *buf, int len);
 247int readlink0(char *path, char *buf, int len);
 248int regexec0(regex_t *preg, char *string, long len, int nmatch,
 249  regmatch_t pmatch[], int eflags);
 250char *getusername(uid_t uid);
 251char *getgroupname(gid_t gid);
 252void do_lines(int fd, void (*call)(char **pline, long len));
 253long environ_bytes();
 254long long millitime(void);
 255char *format_iso_time(char *buf, size_t len, struct timespec *ts);
 257#define HR_SPACE 1 // Space between number and units
 258#define HR_B     2 // Use "B" for single byte units
 259#define HR_1000  4 // Use decimal instead of binary units
 260int human_readable(char *buf, unsigned long long num, int style);
 262// linestack.c
 264struct linestack {
 265  long len, max;
 266  struct ptr_len idx[];
 269void linestack_addstack(struct linestack **lls, struct linestack *throw,
 270  long pos);
 271void linestack_insert(struct linestack **lls, long pos, char *line, long len);
 272void linestack_append(struct linestack **lls, char *line);
 273struct linestack *linestack_load(char *name);
 274int crunch_escape(FILE *out, int cols, int wc);
 275int crunch_rev_escape(FILE *out, int cols, int wc);
 276int crunch_str(char **str, int width, FILE *out, char *escmore,
 277  int (*escout)(FILE *out, int cols, int wc));
 278int draw_str(char *start, int width);
 279int utf8len(char *str);
 280int utf8skip(char *str, int width);
 281int draw_trim_esc(char *str, int padto, int width, char *escmore,
 282  int (*escout)(FILE *out, int cols,int wc));
 283int draw_trim(char *str, int padto, int width);
 285// interestingtimes.c
 286int tty_fd(void);
 287int terminal_size(unsigned *xx, unsigned *yy);
 288int terminal_probesize(unsigned *xx, unsigned *yy);
 289int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy);
 290int set_terminal(int fd, int raw, int speed, struct termios *old);
 291void xset_terminal(int fd, int raw, int speed, struct termios *old);
 292int scan_key(char *scratch, int miliwait);
 293void tty_esc(char *s);
 294void tty_jump(int x, int y);
 295void tty_reset(void);
 296void tty_sigreset(int i);
 297void start_redraw(unsigned *width, unsigned *height);
 299// net.c
 300int xsocket(int domain, int type, int protocol);
 301void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
 302struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
 303  int protocol, int flags);
 304int xconnect(struct addrinfo *ai_arg);
 305int xpoll(struct pollfd *fds, int nfds, int timeout);
 306int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
 307char *ntop(struct sockaddr *sa);
 309// password.c
 310int get_salt(char *salt, char * algo);
 312// commas.c
 313void comma_args(struct arg_list *al, void *data, char *err,
 314  char *(*callback)(void *data, char *str, int len));
 315void comma_collate(char **old, char *new);
 316char *comma_iterate(char **list, int *len);
 317int comma_scan(char *optlist, char *opt, int clean);
 318int comma_scanall(char *optlist, char *scanlist);
 320// deflate.c
 322long long gzip_fd(int infd, int outfd);
 323long long gunzip_fd(int infd, int outfd);
 325// getmountlist.c
 326struct mtab_list {
 327  struct mtab_list *next, *prev;
 328  struct stat stat;
 329  struct statvfs statvfs;
 330  char *dir;
 331  char *device;
 332  char *opts;
 333  char type[0];
 336int mountlist_istype(struct mtab_list  *ml, char *typelist);
 337struct mtab_list *xgetmountlist(char *path);
 339// signal
 341void generic_signal(int signal);
 342void exit_signal(int signal);
 343void sigatexit(void *handler);
 344int sig_to_num(char *pidstr);
 345char *num_to_sig(int sig);
 347mode_t string_to_mode(char *mode_str, mode_t base);
 348void mode_to_string(mode_t mode, char *buf);
 349char *getdirname(char *name);
 350char *getbasename(char *name);
 351int fileunderdir(char *file, char *dir);
 352void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
 354pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid);
 355#define XVFORK() xvforkwrap(vfork())
 357// Wrapper to make xfuncs() return (via longjmp) instead of exiting.
 358// Assigns true/false "did it exit" value to first argument.
 359#define WOULD_EXIT(y, x) do { jmp_buf _noexit; \
 360  int _noexit_res; \
 361  toys.rebound = &_noexit; \
 362  _noexit_res = setjmp(_noexit); \
 363  if (!_noexit_res) do {x;} while(0); \
 364  toys.rebound = 0; \
 365  y = _noexit_res; \
 366} while(0)
 368// Wrapper that discards true/false "did it exit" value.
 369#define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
 371#define minof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa<bb ? aa : bb;})
 372#define maxof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa>bb ? aa : bb;})
 374// Functions in need of further review/cleanup
 375#include "lib/pending.h"