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