blob: b7b10ff4605c873d04adc34c0304140463ff87da [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 Landleyf01503d2012-02-02 07:26:39 -06006// Unfortunately, sizeof() doesn't work in a preprocessor test. TODO.
7
8//#if sizeof(double) <= sizeof(long)
9//typedef double FLOAT;
10//#else
11typedef float FLOAT;
12//#endif
13
Rob Landley1521a9e2006-11-25 16:06:55 -050014// libc generally has this, but the headers are screwed up
15ssize_t getline(char **lineptr, size_t *n, FILE *stream);
16
Rob Landley0a04b3e2006-11-03 00:05:52 -050017// llist.c
Rob Landley0a04b3e2006-11-03 00:05:52 -050018
Rob Landleyeb7ea222012-04-14 22:30:41 -050019// All these list types can be handled by the same code because first element
Rob Landleye5f3a0b2013-03-17 17:57:28 -050020// is always next pointer, so next = (mytype *)&struct. (The payloads are
21// named differently to catch using the wrong type early.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050022
Rob Landley0a04b3e2006-11-03 00:05:52 -050023struct string_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060024 struct string_list *next;
25 char str[0];
Rob Landley0a04b3e2006-11-03 00:05:52 -050026};
27
Rob Landley8324b892006-11-19 02:49:22 -050028struct arg_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060029 struct arg_list *next;
30 char *arg;
Rob Landley8324b892006-11-19 02:49:22 -050031};
32
Rob Landleybc078652007-12-15 21:47:25 -060033struct double_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060034 struct double_list *next, *prev;
35 char *data;
Rob Landleybc078652007-12-15 21:47:25 -060036};
37
Rob Landley9e2b6db2012-07-15 17:22:04 -050038void llist_traverse(void *list, void (*using)(void *data));
Rob Landley6ef04ef2008-01-20 17:34:53 -060039void *llist_pop(void *list); // actually void **list, but the compiler's dumb
Rob Landley2c482472012-03-12 00:25:40 -050040void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
Rob Landleybdf037f2008-10-23 16:44:30 -050041struct double_list *dlist_add(struct double_list **list, char *data);
Rob Landley6ef04ef2008-01-20 17:34:53 -060042
Rob Landley103b7e02007-10-04 02:04:10 -050043// args.c
44void get_optflags(void);
45
46// dirtree.c
Rob Landleyeb7ea222012-04-14 22:30:41 -050047
48// Values returnable from callback function (bitfield, or them together)
49// Default with no callback is 0
50
Rob Landley8c4ae8a2012-05-20 15:00:19 -050051// Add this node to the tree
52#define DIRTREE_SAVE 1
53// Recurse into children
54#define DIRTREE_RECURSE 2
55// Call again after handling all children of this directory
56// (Ignored for non-directories, sets linklen = -1 before second call.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050057#define DIRTREE_COMEAGAIN 4
58// Follow symlinks to directories
59#define DIRTREE_SYMFOLLOW 8
Rob Landley8c4ae8a2012-05-20 15:00:19 -050060// Don't look at any more files in this directory.
61#define DIRTREE_ABORT 256
Rob Landleyeb7ea222012-04-14 22:30:41 -050062
63#define DIRTREE_ABORTVAL ((struct dirtree *)1)
64
Rob Landleyd25f7e42007-02-03 14:11:26 -050065struct dirtree {
Rob Landley7aa651a2012-11-13 17:14:08 -060066 struct dirtree *next, *parent, *child;
67 long extra; // place for user to store their stuff (can be pointer)
68 struct stat st;
69 char *symlink;
70 int data; // dirfd for directory, linklen for symlink, -1 = comeagain
71 char name[];
Rob Landleyd25f7e42007-02-03 14:11:26 -050072};
73
Rob Landley3162c272012-12-06 15:13:30 -060074struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int symfollow);
Rob Landleyeb7ea222012-04-14 22:30:41 -050075char *dirtree_path(struct dirtree *node, int *plen);
Rob Landley8c4ae8a2012-05-20 15:00:19 -050076int dirtree_notdotdot(struct dirtree *catch);
Rob Landley6ec21782012-06-16 15:16:08 -050077int dirtree_parentfd(struct dirtree *node);
Rob Landley090c5c62012-12-31 14:38:13 -060078struct dirtree *dirtree_handle_callback(struct dirtree *new,
Rob Landley7aa651a2012-11-13 17:14:08 -060079 int (*callback)(struct dirtree *node));
Rob Landleyeb7ea222012-04-14 22:30:41 -050080void dirtree_recurse(struct dirtree *node,
Rob Landley7aa651a2012-11-13 17:14:08 -060081 int (*callback)(struct dirtree *node), int symfollow);
Rob Landleyeb7ea222012-04-14 22:30:41 -050082struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
Rob Landley8324b892006-11-19 02:49:22 -050083
Rob Landley36ffc5a2013-04-14 21:43:22 -050084// help.c
85
86void show_help(void);
87
Rob Landley72756672013-07-17 17:22:46 -050088// xwrap.c
Rob Landleybe93c912013-04-20 23:33:48 -050089void xstrncpy(char *dest, char *src, size_t size);
Rob Landley55830302013-06-16 19:59:51 -050090void xexit(void) noreturn;
landley4f344e32006-10-05 16:18:03 -040091void *xmalloc(size_t size);
landleycd9dfc32006-10-18 18:38:16 -040092void *xzalloc(size_t size);
Rob Landley0c93f6c2007-04-29 19:55:21 -040093void *xrealloc(void *ptr, size_t size);
Rob Landley1e01cd12010-01-05 10:48:32 -060094char *xstrndup(char *s, size_t n);
95char *xstrdup(char *s);
landley00f87f12006-10-25 18:38:37 -040096char *xmsprintf(char *format, ...);
Rob Landley24d1d452007-01-20 18:04:20 -050097void xprintf(char *format, ...);
Rob Landley5084fea2007-06-18 00:14:03 -040098void xputs(char *s);
Rob Landley24d1d452007-01-20 18:04:20 -050099void xputc(char c);
100void xflush(void);
Rob Landley72756672013-07-17 17:22:46 -0500101void xexec_optargs(int skip);
landley09ea7ac2006-10-30 01:38:00 -0500102void xexec(char **argv);
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 Landleye824ed12008-07-18 08:43:18 -0500115char *xreadfile(char *name);
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 Landleyeec46372012-06-01 13:50:41 -0500120char *xrealpath(char *path);
Rob Landley988abb32008-05-12 00:52:27 -0500121void xchdir(char *path);
Rob Landley35483412007-12-27 21:36:33 -0600122void xmkpath(char *path, int mode);
Rob Landleye0377fb2010-01-05 12:17:05 -0600123void xsetuid(uid_t uid);
Rob Landleyd3904932013-07-16 00:04:56 -0500124char *xreadlink(char *name);
Rob Landley72756672013-07-17 17:22:46 -0500125long xparsetime(char *arg, long units, long *fraction);
Felix Jandadccfb2a2013-08-26 21:55:33 +0200126void xpidfile(char *name);
Rob Landleyd3904932013-07-16 00:04:56 -0500127
128// lib.c
129void verror_msg(char *msg, int err, va_list va);
130void error_msg(char *msg, ...);
131void perror_msg(char *msg, ...);
132void error_exit(char *msg, ...) noreturn;
133void perror_exit(char *msg, ...) noreturn;
134ssize_t readall(int fd, void *buf, size_t len);
135ssize_t writeall(int fd, void *buf, size_t len);
136off_t lskip(int fd, off_t offset);
137struct string_list **splitpath(char *path, struct string_list **list);
138char *readfile(char *name);
139void msleep(long miliseconds);
140int64_t peek(void *ptr, int size);
141void poke(void *ptr, uint64_t val, int size);
Rob Landley0a04b3e2006-11-03 00:05:52 -0500142struct string_list *find_in_path(char *path, char *filename);
landley09ea7ac2006-10-30 01:38:00 -0500143void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
144void itoa_to_buf(int n, char *buf, unsigned buflen);
145char *utoa(unsigned n);
146char *itoa(int n);
Rob Landleyf5757162007-02-16 21:08:22 -0500147long atolx(char *c);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500148int numlen(long l);
Rob Landley2037b832012-07-15 16:56:20 -0500149int stridx(char *haystack, char needle);
Rob Landleye2580db2007-01-23 13:20:38 -0500150off_t fdlength(int fd);
Rob Landleyad63f4b2011-12-12 15:19:52 -0600151void loopfiles_rw(char **argv, int flags, int permissions, int failok,
Rob Landley7aa651a2012-11-13 17:14:08 -0600152 void (*function)(int fd, char *name));
Rob Landley7634b552007-11-29 17:49:50 -0600153void loopfiles(char **argv, void (*function)(int fd, char *name));
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500154char *get_rawline(int fd, long *plen, char end);
Rob Landleybc078652007-12-15 21:47:25 -0600155char *get_line(int fd);
156void xsendfile(int in, int out);
Rob Landley67a069d2012-06-03 00:32:12 -0500157int wfchmodat(int rc, char *name, mode_t mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600158int copy_tempfile(int fdin, char *name, char **tempname);
159void delete_tempfile(int fdin, int fdout, char **tempname);
160void replace_tempfile(int fdin, int fdout, char **tempname);
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600161void crc_init(unsigned int *crc_table, int little_endian);
Rob Landley26e7b5e2012-02-02 07:27:35 -0600162void terminal_size(unsigned *x, unsigned *y);
Rob Landleyf793d532012-02-27 21:56:49 -0600163int yesno(char *prompt, int def);
Elie De Brauwerca4035b2012-12-16 13:43:36 +0100164void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name));
Jonathan Clairembault939fa742012-11-23 00:06:28 +0100165unsigned long xstrtoul(const char *nptr, char **endptr, int base);
landley4f344e32006-10-05 16:18:03 -0400166
Rob Landleyf0153442013-04-26 02:41:05 -0500167// net.c
168int xsocket(int domain, int type, int protocol);
169
landleycd9dfc32006-10-18 18:38:16 -0400170// getmountlist.c
landley4f344e32006-10-05 16:18:03 -0400171struct mtab_list {
Rob Landley7aa651a2012-11-13 17:14:08 -0600172 struct mtab_list *next;
173 struct stat stat;
174 struct statvfs statvfs;
175 char *dir;
176 char *device;
177 char type[0];
landley4f344e32006-10-05 16:18:03 -0400178};
179
Rob Landley078d31c2013-05-10 18:57:01 -0500180struct mtab_list *xgetmountlist(void);
landley4f344e32006-10-05 16:18:03 -0400181
Rob Landley6000f132007-01-18 22:00:12 -0500182void bunzipStream(int src_fd, int dst_fd);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600183
184// signal
185
Rob Landleyc52db602012-07-30 01:01:33 -0500186void sigatexit(void *handler);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600187int sig_to_num(char *pidstr);
188char *num_to_sig(int sig);
Daniel Walter05744b32012-03-19 19:57:56 -0500189
190mode_t string_to_mode(char *mode_str, mode_t base);
Rob Landley5a26a862013-06-02 00:24:24 -0500191void mode_to_string(mode_t mode, char *buf);
Rob Landley2c917f52012-07-17 08:54:47 -0500192
193// password helper functions
Rob Landley6ba38c22012-07-21 18:38:36 -0500194int read_password(char * buff, int buflen, char* mesg);
195int update_password(char *filename, char* username, char* encrypted);
Rob Landley2c917f52012-07-17 08:54:47 -0500196
Rob Landley734b5302012-11-16 12:26:48 -0600197// cut helper functions
198unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange);
Strakee999ca02013-07-12 18:10:52 -0500199
200// grep helper functions
201char *astrcat (char *, char *);
202char *xastrcat (char *, char *);
Felix Jandae49fe142013-08-10 20:18:18 +0200203
204void daemonize(void);