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