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
  33struct dev_ino {
  34  dev_t dev;
  35  ino_t ino;
  36};
  37
  38void llist_free_arg(void *node);
  39void llist_free_double(void *node);
  40void llist_traverse(void *list, void (*using)(void *node));
  41void *llist_pop(void *list);  // actually void **list
  42void *dlist_pop(void *list);  // actually struct double_list **list
  43void *dlist_lpop(void *list); // also struct double_list **list
  44void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
  45struct double_list *dlist_add(struct double_list **list, char *data);
  46void *dlist_terminate(void *list);
  47struct num_cache *get_num_cache(struct num_cache *cache, long long num);
  48struct num_cache *add_num_cache(struct num_cache **cache, long long num,
  49  void *data, int len);
  50
  51// args.c
  52#define FLAGS_NODASH (1LL<<63)
  53void get_optflags(void);
  54
  55// dirtree.c
  56
  57// Values returnable from callback function (bitfield, or them together)
  58// Default with no callback is 0
  59
  60// Add this node to the tree
  61#define DIRTREE_SAVE         1
  62// Recurse into children
  63#define DIRTREE_RECURSE      2
  64// Call again after handling all children of this directory
  65// (Ignored for non-directories, sets linklen = -1 before second call.)
  66#define DIRTREE_COMEAGAIN    4
  67// Follow symlinks to directories
  68#define DIRTREE_SYMFOLLOW    8
  69// Don't warn about failure to stat
  70#define DIRTREE_SHUTUP      16
  71// Breadth first traversal, conserves filehandles at the expense of memory
  72#define DIRTREE_BREADTH     32 // TODO not implemented yet
  73// skip non-numeric entries
  74#define DIRTREE_PROC        64
  75// Return files we can't stat
  76#define DIRTREE_STATLESS   128
  77// Don't look at any more files in this directory.
  78#define DIRTREE_ABORT      256
  79
  80#define DIRTREE_ABORTVAL ((struct dirtree *)1)
  81
  82struct dirtree {
  83  struct dirtree *next, *parent, *child;
  84  long extra; // place for user to store their stuff (can be pointer)
  85  char *symlink;
  86  int dirfd;
  87  struct stat st;
  88  char again, name[];
  89};
  90
  91int isdotdot(char *name);
  92struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags);
  93char *dirtree_path(struct dirtree *node, int *plen);
  94int dirtree_notdotdot(struct dirtree *catch);
  95int dirtree_parentfd(struct dirtree *node);
  96int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
  97  int dirfd, int symfollow);
  98struct dirtree *dirtree_flagread(char *path, int flags,
  99  int (*callback)(struct dirtree *node));
 100struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
 101
 102// Tell xopen and friends to print warnings but return -1 as necessary
 103// The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
 104// plenty of headroom.
 105#define WARN_ONLY        (1<<31) // don't exit, just warn
 106#define LOOPFILES_ANYWAY (1<<30) // call function with fd -1
 107
 108// xabspath flags
 109#define ABS_PATH 1 // all but last path component must exist
 110#define ABS_FILE 2 // last path component must exist
 111#define ABS_KEEP 4 // don't resolve symlinks in path to last component
 112#define ABS_LAST 8 // don't resolve symlink in last path component
 113
 114// xwrap.c
 115void xstrncpy(char *dest, char *src, size_t size);
 116void xstrncat(char *dest, char *src, size_t size);
 117_Noreturn void _xexit(void);
 118_Noreturn void xexit(void);
 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 xflush(int flush);
 128void xprintf(char *format, ...) printf_format;
 129void xputsl(char *s, int len);
 130void xputsn(char *s);
 131void xputs(char *s);
 132void xputc(char c);
 133void xvdaemon(void);
 134void xexec(char **argv);
 135pid_t xpopen_setup(char **argv, int *pipes, void (*callback)(char **argv));
 136pid_t xpopen_both(char **argv, int *pipes);
 137int xwaitpid(pid_t pid);
 138int xpclose_both(pid_t pid, int *pipes);
 139pid_t xpopen(char **argv, int *pipe, int isstdout);
 140pid_t xpclose(pid_t pid, int pipe);
 141int xrun(char **argv);
 142char *xrunread(char *argv[], char *stdin);
 143int xpspawn(char **argv, int*pipes);
 144void xaccess(char *path, int flags);
 145void xunlink(char *path);
 146void xrename(char *from, char *to);
 147int xtempfile(char *name, char **tempname);
 148int xcreate(char *path, int flags, int mode);
 149int xopen(char *path, int flags);
 150int xcreate_stdio(char *path, int flags, int mode);
 151int xopen_stdio(char *path, int flags);
 152int openro(char *path, int flags);
 153int xopenro(char *path);
 154void xpipe(int *pp);
 155void xclose(int fd);
 156int xdup(int fd);
 157int notstdio(int fd);
 158FILE *xfdopen(int fd, char *mode);
 159FILE *xfopen(char *path, char *mode);
 160size_t xread(int fd, void *buf, size_t len);
 161void xreadall(int fd, void *buf, size_t len);
 162void xwrite(int fd, void *buf, size_t len);
 163off_t xlseek(int fd, off_t offset, int whence);
 164char *xreadfile(char *name, char *buf, off_t len);
 165int xioctl(int fd, int request, void *data);
 166char *xgetcwd(void);
 167void xstat(char *path, struct stat *st);
 168char *xabspath(char *path, int exact);
 169void xchdir(char *path);
 170void xchroot(char *path);
 171struct passwd *xgetpwuid(uid_t uid);
 172struct group *xgetgrgid(gid_t gid);
 173struct passwd *xgetpwnam(char *name);
 174struct group *xgetgrnam(char *name);
 175unsigned xgetuid(char *name);
 176unsigned xgetgid(char *name);
 177void xsetuser(struct passwd *pwd);
 178char *xreadlinkat(int dir, char *name);
 179char *xreadlink(char *name);
 180double xstrtod(char *s);
 181long xparsetime(char *arg, long units, long *fraction);
 182void xparsetimespec(char *arg, struct timespec *ts);
 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);
 192time_t xmktime(struct tm *tm, int utc);
 193
 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;
 198_Noreturn void error_exit(char *msg, ...) printf_format;
 199_Noreturn void perror_exit(char *msg, ...) printf_format;
 200_Noreturn void help_exit(char *msg, ...) printf_format;
 201void error_msg_raw(char *msg);
 202void perror_msg_raw(char *msg);
 203_Noreturn void error_exit_raw(char *msg);
 204_Noreturn void 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(unsigned *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);
 243int same_file(struct stat *st1, struct stat *st2);
 244int same_dev_ino(struct stat *st, struct dev_ino *di);
 245off_t fdlength(int fd);
 246void loopfiles_rw(char **argv, int flags, int permissions,
 247  void (*function)(int fd, char *name));
 248void loopfiles(char **argv, void (*function)(int fd, char *name));
 249void loopfiles_lines(char **argv, void (*function)(char **pline, long len));
 250long long sendfile_len(int in, int out, long long len, long long *consumed);
 251long long xsendfile_len(int in, int out, long long len);
 252void xsendfile_pad(int in, int out, long long len);
 253long long xsendfile(int in, int out);
 254int wfchmodat(int rc, char *name, mode_t mode);
 255int copy_tempfile(int fdin, char *name, char **tempname);
 256void delete_tempfile(int fdin, int fdout, char **tempname);
 257void replace_tempfile(int fdin, int fdout, char **tempname);
 258void crc_init(unsigned *crc_table, int little_endian);
 259void base64_init(char *p);
 260int yesno(int def);
 261int fyesno(FILE *fp, int def);
 262int qstrcmp(const void *a, const void *b);
 263void create_uuid(char *uuid);
 264char *show_uuid(char *uuid);
 265char *next_printf(char *s, char **start);
 266struct passwd *bufgetpwnamuid(char *name, uid_t uid);
 267struct passwd *bufgetpwuid(uid_t uid);
 268struct group *bufgetgrnamgid(char *name, gid_t gid);
 269struct group *bufgetgrgid(gid_t gid);
 270int readlinkat0(int dirfd, char *path, char *buf, int len);
 271int readlink0(char *path, char *buf, int len);
 272int regexec0(regex_t *preg, char *string, long len, int nmatch,
 273  regmatch_t pmatch[], int eflags);
 274char *getusername(uid_t uid);
 275char *getgroupname(gid_t gid);
 276void do_lines(int fd, char delim, void (*call)(char **pline, long len));
 277long long millitime(void);
 278char *format_iso_time(char *buf, size_t len, struct timespec *ts);
 279void loggit(int priority, char *format, ...);
 280unsigned tar_cksum(void *data);
 281int is_tar_header(void *pkt);
 282char *elf_arch_name(int type);
 283
 284#define HR_SPACE  1 // Space between number and units
 285#define HR_B      2 // Use "B" for single byte units
 286#define HR_1000   4 // Use decimal instead of binary units
 287#define HR_NODOT  8 // No tenths for single digit units
 288int human_readable_long(char *buf, unsigned long long num, int dgt, int unit,
 289  int style);
 290int human_readable(char *buf, unsigned long long num, int style);
 291
 292// env.c
 293
 294long environ_bytes(void);
 295char *xsetenv(char *name, char *val);
 296void xunsetenv(char *name);
 297char *xpop_env(char *name); // because xpopenv() looks like xpopen_v()
 298void xclearenv(void);
 299void reset_env(struct passwd *p, int clear);
 300
 301// utf8.c
 302
 303int crunch_escape(FILE *out, int cols, int wc);
 304int crunch_rev_escape(FILE *out, int cols, int wc);
 305int crunch_str(char **str, int width, FILE *out, char *escmore,
 306  int (*escout)(FILE *out, int cols, int wc));
 307int draw_str(char *start, int width);
 308int utf8len(char *str);
 309int utf8skip(char *str, int width);
 310int draw_trim_esc(char *str, int padto, int width, char *escmore,
 311  int (*escout)(FILE *out, int cols,int wc));
 312int draw_trim(char *str, int padto, int width);
 313
 314// tty.c
 315int tty_fd(void);
 316int terminal_size(unsigned *xx, unsigned *yy);
 317int terminal_probesize(unsigned *xx, unsigned *yy);
 318#define KEY_UP 0
 319#define KEY_DOWN 1
 320#define KEY_RIGHT 2
 321#define KEY_LEFT 3
 322#define KEY_PGUP 4
 323#define KEY_PGDN 5
 324#define KEY_HOME 6
 325#define KEY_END 7
 326#define KEY_INSERT 8
 327#define KEY_DELETE 9
 328#define KEY_FN 10 // F1 = KEY_FN+1, F2 = KEY_FN+2, ...
 329#define KEY_SHIFT (1<<16)
 330#define KEY_CTRL (1<<17)
 331#define KEY_ALT (1<<18)
 332int scan_key(char *scratch, int timeout_ms);
 333int scan_key_getsize(char *scratch, int timeout_ms, unsigned *xx, unsigned *yy);
 334void xsetspeed(struct termios *tio, int speed);
 335int set_terminal(int fd, int raw, int speed, struct termios *old);
 336void xset_terminal(int fd, int raw, int speed, struct termios *old);
 337void tty_reset(void);
 338void tty_sigreset(int i);
 339void start_redraw(unsigned *width, unsigned *height);
 340
 341// net.c
 342
 343union socksaddr {
 344  struct sockaddr s;
 345  struct sockaddr_in in;
 346  struct sockaddr_in6 in6;
 347};
 348
 349int xsocket(int domain, int type, int protocol);
 350void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
 351struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
 352  int protocol, int flags);
 353void xbind(int fd, const struct sockaddr *sa, socklen_t len);
 354void xconnect(int fd, const struct sockaddr *sa, socklen_t len);
 355int xconnectany(struct addrinfo *ai);
 356int xbindany(struct addrinfo *ai);
 357int xpoll(struct pollfd *fds, int nfds, int timeout);
 358int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
 359char *ntop(struct sockaddr *sa);
 360void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest);
 361int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout);
 362char *escape_url(char *str, char *and);
 363void unescape_url(char *str);
 364
 365// password.c
 366int get_salt(char *salt, char * algo);
 367
 368// commas.c
 369void comma_args(struct arg_list *al, void *data, char *err,
 370  char *(*callback)(void *data, char *str, int len));
 371void comma_collate(char **old, char *new);
 372char *comma_iterate(char **list, int *len);
 373int comma_scan(char *optlist, char *opt, int clean);
 374int comma_scanall(char *optlist, char *scanlist);
 375int comma_remove(char *optlist, char *opt);
 376
 377// deflate.c
 378
 379long long gzip_fd(int infd, int outfd);
 380long long gunzip_fd(int infd, int outfd);
 381long long gunzip_fd_preload(int infd, int outfd, char *buf, unsigned len);
 382
 383
 384// getmountlist.c
 385struct mtab_list {
 386  struct mtab_list *next, *prev;
 387  struct stat stat;
 388  struct statvfs statvfs;
 389  char *dir;
 390  char *device;
 391  char *opts;
 392  char type[0];
 393};
 394
 395int mountlist_istype(struct mtab_list  *ml, char *typelist);
 396struct mtab_list *xgetmountlist(char *path);
 397
 398// signal
 399
 400void generic_signal(int signal);
 401void exit_signal(int signal);
 402void sigatexit(void *handler);
 403void list_signals(void);
 404
 405mode_t string_to_mode(char *mode_str, mode_t base);
 406void mode_to_string(mode_t mode, char *buf);
 407char *getbasename(char *name);
 408char *fileunderdir(char *file, char *dir);
 409char *relative_path(char *from, char *to);
 410void names_to_pid(char **names, int (*callback)(pid_t pid, char *name),
 411    int scripts);
 412
 413pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid);
 414#define XVFORK() xvforkwrap(vfork())
 415
 416// Wrapper to make xfuncs() return (via siglongjmp) instead of exiting.
 417// Assigns true/false "did it exit" value to first argument.
 418#define WOULD_EXIT(y, x) do { sigjmp_buf _noexit; \
 419  int _noexit_res; \
 420  toys.rebound = &_noexit; \
 421  _noexit_res = sigsetjmp(_noexit, 1); \
 422  if (!_noexit_res) do {x;} while(0); \
 423  toys.rebound = 0; \
 424  y = _noexit_res; \
 425} while(0)
 426
 427// Wrapper that discards true/false "did it exit" value.
 428#define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
 429
 430#define minof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa<bb ? aa : bb;})
 431#define maxof(a, b) ({typeof(a) aa = (a); typeof(b) bb = (b); aa>bb ? aa : bb;})
 432
 433// Functions in need of further review/cleanup
 434#include "lib/pending.h"
 435