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