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