lib.h 15 KB

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