lib.h 15 KB

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