blob: bdd78c9a5d66f5ea39765284f0c7a3d9282b55f6 [file] [log] [blame]
landley09ea7ac2006-10-30 01:38:00 -05001/* lib.h - header file for lib directory
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 */
landley4f344e32006-10-05 16:18:03 -04005
Rob Landley0a04b3e2006-11-03 00:05:52 -05006// llist.c
Rob Landley0a04b3e2006-11-03 00:05:52 -05007
Rob Landleyeb7ea222012-04-14 22:30:41 -05008// All these list types can be handled by the same code because first element
Rob Landleye5f3a0b2013-03-17 17:57:28 -05009// is always next pointer, so next = (mytype *)&struct. (The payloads are
10// named differently to catch using the wrong type early.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050011
Rob Landley0a04b3e2006-11-03 00:05:52 -050012struct string_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060013 struct string_list *next;
14 char str[0];
Rob Landley0a04b3e2006-11-03 00:05:52 -050015};
16
Rob Landley8324b892006-11-19 02:49:22 -050017struct arg_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060018 struct arg_list *next;
19 char *arg;
Rob Landley8324b892006-11-19 02:49:22 -050020};
21
Rob Landleybc078652007-12-15 21:47:25 -060022struct double_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060023 struct double_list *next, *prev;
24 char *data;
Rob Landleybc078652007-12-15 21:47:25 -060025};
26
Rob Landleye604d532014-05-21 06:57:43 -050027void llist_free_arg(void *node);
28void llist_free_double(void *node);
29void llist_traverse(void *list, void (*using)(void *node));
Rob Landley5f57bcc2013-09-09 04:26:03 -050030void *llist_pop(void *list); // actually void **list
31void *dlist_pop(void *list); // actually struct double_list **list
Rob Landley2c482472012-03-12 00:25:40 -050032void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
Rob Landleybdf037f2008-10-23 16:44:30 -050033struct double_list *dlist_add(struct double_list **list, char *data);
Rob Landleydc640252014-05-29 05:22:02 -050034void *dlist_terminate(void *list);
Rob Landley6ef04ef2008-01-20 17:34:53 -060035
Rob Landley103b7e02007-10-04 02:04:10 -050036// args.c
37void get_optflags(void);
38
39// dirtree.c
Rob Landleyeb7ea222012-04-14 22:30:41 -050040
41// Values returnable from callback function (bitfield, or them together)
42// Default with no callback is 0
43
Rob Landley8c4ae8a2012-05-20 15:00:19 -050044// Add this node to the tree
45#define DIRTREE_SAVE 1
46// Recurse into children
47#define DIRTREE_RECURSE 2
48// Call again after handling all children of this directory
49// (Ignored for non-directories, sets linklen = -1 before second call.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050050#define DIRTREE_COMEAGAIN 4
51// Follow symlinks to directories
52#define DIRTREE_SYMFOLLOW 8
Rob Landley8c4ae8a2012-05-20 15:00:19 -050053// Don't look at any more files in this directory.
54#define DIRTREE_ABORT 256
Rob Landleyeb7ea222012-04-14 22:30:41 -050055
56#define DIRTREE_ABORTVAL ((struct dirtree *)1)
57
Rob Landleyd25f7e42007-02-03 14:11:26 -050058struct dirtree {
Rob Landley7aa651a2012-11-13 17:14:08 -060059 struct dirtree *next, *parent, *child;
60 long extra; // place for user to store their stuff (can be pointer)
61 struct stat st;
62 char *symlink;
Rob Landleyfec3fd12014-07-26 13:30:40 -050063 int data; // dirfd for directory, linklen for symlink
64 char again;
Rob Landley7aa651a2012-11-13 17:14:08 -060065 char name[];
Rob Landleyd25f7e42007-02-03 14:11:26 -050066};
67
Rob Landley3162c272012-12-06 15:13:30 -060068struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int symfollow);
Rob Landleyeb7ea222012-04-14 22:30:41 -050069char *dirtree_path(struct dirtree *node, int *plen);
Rob Landley8c4ae8a2012-05-20 15:00:19 -050070int dirtree_notdotdot(struct dirtree *catch);
Rob Landley6ec21782012-06-16 15:16:08 -050071int dirtree_parentfd(struct dirtree *node);
Rob Landley090c5c62012-12-31 14:38:13 -060072struct dirtree *dirtree_handle_callback(struct dirtree *new,
Rob Landley7aa651a2012-11-13 17:14:08 -060073 int (*callback)(struct dirtree *node));
Rob Landleyfec3fd12014-07-26 13:30:40 -050074int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
75 int symfollow);
Rob Landleyeb7ea222012-04-14 22:30:41 -050076struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
Rob Landley8324b892006-11-19 02:49:22 -050077
Rob Landley36ffc5a2013-04-14 21:43:22 -050078// help.c
79
80void show_help(void);
81
Rob Landley72756672013-07-17 17:22:46 -050082// xwrap.c
Rob Landleybe93c912013-04-20 23:33:48 -050083void xstrncpy(char *dest, char *src, size_t size);
Rob Landley2fb85a32014-12-04 21:41:12 -060084void xstrncat(char *dest, char *src, size_t size);
Rob Landley55830302013-06-16 19:59:51 -050085void xexit(void) noreturn;
landley4f344e32006-10-05 16:18:03 -040086void *xmalloc(size_t size);
landleycd9dfc32006-10-18 18:38:16 -040087void *xzalloc(size_t size);
Rob Landley0c93f6c2007-04-29 19:55:21 -040088void *xrealloc(void *ptr, size_t size);
Rob Landley1e01cd12010-01-05 10:48:32 -060089char *xstrndup(char *s, size_t n);
90char *xstrdup(char *s);
Elliott Hughes1be99e62015-03-01 16:16:50 -060091char *xmprintf(char *format, ...) printf_format;
92void xprintf(char *format, ...) printf_format;
Rob Landley5084fea2007-06-18 00:14:03 -040093void xputs(char *s);
Rob Landley24d1d452007-01-20 18:04:20 -050094void xputc(char c);
95void xflush(void);
landley09ea7ac2006-10-30 01:38:00 -050096void xexec(char **argv);
Rob Landley360d57f2014-09-14 12:29:44 -050097pid_t xpopen_both(char **argv, int *pipes);
98int xpclose_both(pid_t pid, int *pipes);
99pid_t xpopen(char **argv, int *pipe, int stdout);
100pid_t xpclose(pid_t pid, int pipe);
101int xrun(char **argv);
102int xpspawn(char **argv, int*pipes);
Rob Landleyd3e9d642007-01-08 03:25:47 -0500103void xaccess(char *path, int flags);
Rob Landleye745d8e2007-12-20 06:30:19 -0600104void xunlink(char *path);
Rob Landley1322beb2007-01-07 22:51:12 -0500105int xcreate(char *path, int flags, int mode);
106int xopen(char *path, int flags);
Rob Landleybc078652007-12-15 21:47:25 -0600107void xclose(int fd);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500108int xdup(int fd);
Rob Landley1aa75112013-08-07 12:19:51 -0500109FILE *xfdopen(int fd, char *mode);
landley4f344e32006-10-05 16:18:03 -0400110FILE *xfopen(char *path, char *mode);
Rob Landley90163772007-01-18 21:54:08 -0500111size_t xread(int fd, void *buf, size_t len);
112void xreadall(int fd, void *buf, size_t len);
113void xwrite(int fd, void *buf, size_t len);
Rob Landley52476712009-01-18 16:19:25 -0600114off_t xlseek(int fd, off_t offset, int whence);
Rob Landleydc373172013-12-27 18:45:01 -0600115char *xreadfile(char *name, char *buf, off_t len);
Rob Landleyf0153442013-04-26 02:41:05 -0500116int xioctl(int fd, int request, void *data);
landley00f87f12006-10-25 18:38:37 -0400117char *xgetcwd(void);
Rob Landleyd25f7e42007-02-03 14:11:26 -0500118void xstat(char *path, struct stat *st);
Rob Landleyfe91e682012-11-22 21:18:09 -0600119char *xabspath(char *path, int exact);
Rob Landley988abb32008-05-12 00:52:27 -0500120void xchdir(char *path);
Rob Landleyafba5b82013-12-23 06:49:38 -0600121void xchroot(char *path);
Rob Landley9e44a582013-11-28 20:18:04 -0600122struct passwd *xgetpwuid(uid_t uid);
123struct group *xgetgrgid(gid_t gid);
Rob Landley5ec4ab32013-11-28 21:06:15 -0600124struct passwd *xgetpwnam(char *name);
Rob Landley60c35c42014-08-03 15:50:10 -0500125struct group *xgetgrnam(char *name);
Rob Landleyb8140d12015-03-12 11:11:08 -0500126struct passwd *xgetpwnamid(char *user);
127struct group *xgetgrnamid(char *group);
Rob Landleyafba5b82013-12-23 06:49:38 -0600128void xsetuser(struct passwd *pwd);
Rob Landleyd3904932013-07-16 00:04:56 -0500129char *xreadlink(char *name);
Rob Landley72756672013-07-17 17:22:46 -0500130long xparsetime(char *arg, long units, long *fraction);
Felix Jandadccfb2a2013-08-26 21:55:33 +0200131void xpidfile(char *name);
Rob Landley5b405822014-03-29 18:11:00 -0500132void xregcomp(regex_t *preg, char *rexec, int cflags);
Rob Landleyc277f342015-02-09 16:34:24 -0600133char *xtzset(char *new);
Rob Landleye6abb612015-03-09 14:52:32 -0500134void xsignal(int signal, void *handler);
Rob Landleyd3904932013-07-16 00:04:56 -0500135
136// lib.c
137void verror_msg(char *msg, int err, va_list va);
Elliott Hughes1be99e62015-03-01 16:16:50 -0600138void error_msg(char *msg, ...) printf_format;
139void perror_msg(char *msg, ...) printf_format;
140void error_exit(char *msg, ...) printf_format noreturn;
141void perror_exit(char *msg, ...) printf_format noreturn;
Rob Landleyd3904932013-07-16 00:04:56 -0500142ssize_t readall(int fd, void *buf, size_t len);
143ssize_t writeall(int fd, void *buf, size_t len);
144off_t lskip(int fd, off_t offset);
Rob Landleyca1b60e2014-03-11 20:44:55 -0500145int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
Rob Landleyd3904932013-07-16 00:04:56 -0500146struct string_list **splitpath(char *path, struct string_list **list);
Rob Landleye10483f2015-04-03 11:49:31 -0500147char *readfileat(int dirfd, char *name, char *buf, off_t len);
Rob Landley8fdcfdb2013-09-03 17:56:28 -0500148char *readfile(char *name, char *buf, off_t len);
Rob Landleyd3904932013-07-16 00:04:56 -0500149void msleep(long miliseconds);
Rob Landley546b2932014-07-21 19:56:53 -0500150int64_t peek_le(void *ptr, unsigned size);
151int64_t peek_be(void *ptr, unsigned size);
152int64_t peek(void *ptr, unsigned size);
Rob Landleyd3904932013-07-16 00:04:56 -0500153void poke(void *ptr, uint64_t val, int size);
Rob Landley0a04b3e2006-11-03 00:05:52 -0500154struct string_list *find_in_path(char *path, char *filename);
Rob Landley86c747a2015-01-01 16:28:51 -0600155long estrtol(char *str, char **end, int base);
156long xstrtol(char *str, char **end, int base);
Rob Landleyf5757162007-02-16 21:08:22 -0500157long atolx(char *c);
Rob Landleyb5e74162013-11-28 21:11:34 -0600158long atolx_range(char *numstr, long low, long high);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500159int numlen(long l);
Rob Landley2037b832012-07-15 16:56:20 -0500160int stridx(char *haystack, char needle);
Rob Landley5fcc7152014-10-18 17:14:12 -0500161int unescape(char c);
Rob Landley8115fc12014-06-09 07:12:49 -0500162int strstart(char **a, char *b);
Rob Landleye2580db2007-01-23 13:20:38 -0500163off_t fdlength(int fd);
Rob Landleyad63f4b2011-12-12 15:19:52 -0600164void loopfiles_rw(char **argv, int flags, int permissions, int failok,
Rob Landley7aa651a2012-11-13 17:14:08 -0600165 void (*function)(int fd, char *name));
Rob Landley7634b552007-11-29 17:49:50 -0600166void loopfiles(char **argv, void (*function)(int fd, char *name));
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500167char *get_rawline(int fd, long *plen, char end);
Rob Landleybc078652007-12-15 21:47:25 -0600168char *get_line(int fd);
169void xsendfile(int in, int out);
Rob Landley67a069d2012-06-03 00:32:12 -0500170int wfchmodat(int rc, char *name, mode_t mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600171int copy_tempfile(int fdin, char *name, char **tempname);
172void delete_tempfile(int fdin, int fdout, char **tempname);
173void replace_tempfile(int fdin, int fdout, char **tempname);
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600174void crc_init(unsigned int *crc_table, int little_endian);
Rob Landley5bec5ba2014-12-13 11:59:37 -0600175void base64_init(char *p);
Rob Landley10bdaa42013-11-07 09:04:50 -0600176int terminal_size(unsigned *x, unsigned *y);
Rob Landley2fd86242015-04-27 11:13:19 -0500177int set_terminal(int fd, int raw, struct termios *old);
Rob Landleyf793d532012-02-27 21:56:49 -0600178int yesno(char *prompt, int def);
Rob Landley48c172b2014-05-06 06:31:28 -0500179int human_readable(char *buf, unsigned long long num);
Rob Landley5b493dc2015-04-19 21:50:51 -0500180int qstrcmp(const void *a, const void *b);
Rob Landley2fd86242015-04-27 11:13:19 -0500181int xpoll(struct pollfd *fds, int nfds, int timeout);
182int scan_key(char *scratch, char **seqs, int block);
landley4f344e32006-10-05 16:18:03 -0400183
Rob Landleyf0153442013-04-26 02:41:05 -0500184// net.c
185int xsocket(int domain, int type, int protocol);
Rob Landley5bec5ba2014-12-13 11:59:37 -0600186void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
Rob Landleyf0153442013-04-26 02:41:05 -0500187
Rob Landley6d15f0d2014-06-25 22:54:59 -0500188// password.c
189int get_salt(char *salt, char * algo);
190
landleycd9dfc32006-10-18 18:38:16 -0400191// getmountlist.c
landley4f344e32006-10-05 16:18:03 -0400192struct mtab_list {
Rob Landleydc640252014-05-29 05:22:02 -0500193 struct mtab_list *next, *prev;
Rob Landley7aa651a2012-11-13 17:14:08 -0600194 struct stat stat;
195 struct statvfs statvfs;
196 char *dir;
197 char *device;
Rob Landley55e9f352014-05-27 07:56:51 -0500198 char *opts;
Rob Landley7aa651a2012-11-13 17:14:08 -0600199 char type[0];
landley4f344e32006-10-05 16:18:03 -0400200};
201
Rob Landleye996bdd2014-08-24 23:46:23 -0500202void comma_collate(char **old, char *new);
203char *comma_iterate(char **list, int *len);
204int comma_scan(char *optlist, char *opt, int clean);
205int comma_scanall(char *optlist, char *scanlist);
206int mountlist_istype(struct mtab_list *ml, char *typelist);
Rob Landley42adb7a2013-08-30 17:34:24 -0500207struct mtab_list *xgetmountlist(char *path);
landley4f344e32006-10-05 16:18:03 -0400208
Rob Landley2dd50ad2012-02-26 13:48:00 -0600209// signal
210
Rob Landley1bc52242014-05-21 07:24:16 -0500211void generic_signal(int signal);
Rob Landleyc52db602012-07-30 01:01:33 -0500212void sigatexit(void *handler);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600213int sig_to_num(char *pidstr);
214char *num_to_sig(int sig);
Daniel Walter05744b32012-03-19 19:57:56 -0500215
216mode_t string_to_mode(char *mode_str, mode_t base);
Rob Landley5a26a862013-06-02 00:24:24 -0500217void mode_to_string(mode_t mode, char *buf);
Rob Landleydb1009d2013-12-19 09:32:30 -0600218void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
Rob Landley2c917f52012-07-17 08:54:47 -0500219
Rob Landley5b405822014-03-29 18:11:00 -0500220// Functions in need of further review/cleanup
Rob Landley34b91a92013-11-10 18:20:16 -0600221#include "lib/pending.h"