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