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