blob: 812908af64cddb258a13a9f831992240d3fdc115 [file] [log] [blame]
landley4f344e32006-10-05 16:18:03 -04001/* vi: set ts=4 :*/
landley09ea7ac2006-10-30 01:38:00 -05002/* lib.h - header file for lib directory
3 *
4 * Copyright 2006 Rob Landley <rob@landley.net>
5 */
landley4f344e32006-10-05 16:18:03 -04006
Rob Landleyf01503d2012-02-02 07:26:39 -06007// Unfortunately, sizeof() doesn't work in a preprocessor test. TODO.
8
9//#if sizeof(double) <= sizeof(long)
10//typedef double FLOAT;
11//#else
12typedef float FLOAT;
13//#endif
14
Rob Landley1521a9e2006-11-25 16:06:55 -050015// libc generally has this, but the headers are screwed up
16ssize_t getline(char **lineptr, size_t *n, FILE *stream);
17
Rob Landley0a04b3e2006-11-03 00:05:52 -050018// llist.c
Rob Landley0a04b3e2006-11-03 00:05:52 -050019
Rob Landleyeb7ea222012-04-14 22:30:41 -050020// All these list types can be handled by the same code because first element
21// is always next pointer, so next = (mytype *)&struct.
22
Rob Landley0a04b3e2006-11-03 00:05:52 -050023struct string_list {
24 struct string_list *next;
25 char str[0];
26};
27
Rob Landley8324b892006-11-19 02:49:22 -050028struct arg_list {
29 struct arg_list *next;
30 char *arg;
31};
32
Rob Landleybc078652007-12-15 21:47:25 -060033struct double_list {
Rob Landleyeb7ea222012-04-14 22:30:41 -050034 struct double_list *next, *prev;
Rob Landleybc078652007-12-15 21:47:25 -060035 char *data;
36};
37
Rob Landley6ef04ef2008-01-20 17:34:53 -060038void llist_free(void *list, void (*freeit)(void *data));
39void *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
51// Do not add this node to the tree
52#define DIRTREE_NOSAVE 1
53// Do not recurse into children
54#define DIRTREE_NORECURSE 2
55// Call again after handling all children (Directories only. Sets linklen = -1)
56#define DIRTREE_COMEAGAIN 4
57// Follow symlinks to directories
58#define DIRTREE_SYMFOLLOW 8
59// Abort recursive dirtree. (Forces NOSAVE and NORECURSE on this entry.)
60#define DIRTREE_ABORT (256|DIRTREE_NOSAVE|DIRTREE_NORECURSE)
61
62#define DIRTREE_ABORTVAL ((struct dirtree *)1)
63
Rob Landleyd25f7e42007-02-03 14:11:26 -050064struct dirtree {
Rob Landleyeb7ea222012-04-14 22:30:41 -050065 struct dirtree *next, *parent, *child;
66 long extra; // place for user to store their stuff (can be pointer)
67 long data; // dirfd for directory, linklen for symlink
Rob Landleyd25f7e42007-02-03 14:11:26 -050068 struct stat st;
Rob Landleyeb7ea222012-04-14 22:30:41 -050069 char *symlink;
Rob Landleyd25f7e42007-02-03 14:11:26 -050070 char name[];
71};
72
Rob Landleyeb7ea222012-04-14 22:30:41 -050073struct dirtree *dirtree_add_node(int dirfd, char *name);
74char *dirtree_path(struct dirtree *node, int *plen);
75int dirtree_isdotdot(struct dirtree *catch);
76struct dirtree *handle_callback(struct dirtree *new,
77 int (*callback)(struct dirtree *node));
78void dirtree_recurse(struct dirtree *node,
79 int (*callback)(struct dirtree *node));
80struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
Rob Landley8324b892006-11-19 02:49:22 -050081
Rob Landley103b7e02007-10-04 02:04:10 -050082// lib.c
Rob Landleye15850a2007-11-19 01:51:00 -060083void xstrcpy(char *dest, char *src, size_t size);
landley09ea7ac2006-10-30 01:38:00 -050084void verror_msg(char *msg, int err, va_list va);
85void error_msg(char *msg, ...);
86void perror_msg(char *msg, ...);
Rob Landleyefa93b92007-11-15 21:12:24 -060087void error_exit(char *msg, ...) noreturn;
88void perror_exit(char *msg, ...) noreturn;
landley4f344e32006-10-05 16:18:03 -040089void *xmalloc(size_t size);
landleycd9dfc32006-10-18 18:38:16 -040090void *xzalloc(size_t size);
Rob Landley0c93f6c2007-04-29 19:55:21 -040091void *xrealloc(void *ptr, size_t size);
Rob Landley1e01cd12010-01-05 10:48:32 -060092char *xstrndup(char *s, size_t n);
93char *xstrdup(char *s);
landley00f87f12006-10-25 18:38:37 -040094char *xmsprintf(char *format, ...);
Rob Landley24d1d452007-01-20 18:04:20 -050095void xprintf(char *format, ...);
Rob Landley5084fea2007-06-18 00:14:03 -040096void xputs(char *s);
Rob Landley24d1d452007-01-20 18:04:20 -050097void xputc(char c);
98void xflush(void);
landley09ea7ac2006-10-30 01:38:00 -050099void xexec(char **argv);
Rob Landleyd3e9d642007-01-08 03:25:47 -0500100void xaccess(char *path, int flags);
Rob Landleye745d8e2007-12-20 06:30:19 -0600101void xunlink(char *path);
Rob Landley1322beb2007-01-07 22:51:12 -0500102int xcreate(char *path, int flags, int mode);
103int xopen(char *path, int flags);
Rob Landleybc078652007-12-15 21:47:25 -0600104void xclose(int fd);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500105int xdup(int fd);
landley4f344e32006-10-05 16:18:03 -0400106FILE *xfopen(char *path, char *mode);
Rob Landley90163772007-01-18 21:54:08 -0500107ssize_t readall(int fd, void *buf, size_t len);
108ssize_t writeall(int fd, void *buf, size_t len);
109size_t xread(int fd, void *buf, size_t len);
110void xreadall(int fd, void *buf, size_t len);
111void xwrite(int fd, void *buf, size_t len);
Rob Landley52476712009-01-18 16:19:25 -0600112off_t xlseek(int fd, off_t offset, int whence);
Rob Landleye824ed12008-07-18 08:43:18 -0500113char *readfile(char *name);
114char *xreadfile(char *name);
landley00f87f12006-10-25 18:38:37 -0400115char *xgetcwd(void);
Rob Landleyd25f7e42007-02-03 14:11:26 -0500116void xstat(char *path, struct stat *st);
Rob Landleyfa98d012006-11-02 02:57:27 -0500117char *xabspath(char *path);
Rob Landley988abb32008-05-12 00:52:27 -0500118void xchdir(char *path);
Rob Landley35483412007-12-27 21:36:33 -0600119void xmkpath(char *path, int mode);
Rob Landleye0377fb2010-01-05 12:17:05 -0600120void xsetuid(uid_t uid);
Rob Landley0a04b3e2006-11-03 00:05:52 -0500121struct string_list *find_in_path(char *path, char *filename);
landley09ea7ac2006-10-30 01:38:00 -0500122void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
123void itoa_to_buf(int n, char *buf, unsigned buflen);
124char *utoa(unsigned n);
125char *itoa(int n);
Rob Landleyf5757162007-02-16 21:08:22 -0500126long atolx(char *c);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500127int numlen(long l);
Rob Landleye2580db2007-01-23 13:20:38 -0500128off_t fdlength(int fd);
Rob Landley0c93f6c2007-04-29 19:55:21 -0400129char *xreadlink(char *name);
Rob Landleyad63f4b2011-12-12 15:19:52 -0600130void loopfiles_rw(char **argv, int flags, int permissions, int failok,
131 void (*function)(int fd, char *name));
Rob Landley7634b552007-11-29 17:49:50 -0600132void loopfiles(char **argv, void (*function)(int fd, char *name));
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500133char *get_rawline(int fd, long *plen, char end);
Rob Landleybc078652007-12-15 21:47:25 -0600134char *get_line(int fd);
135void xsendfile(int in, int out);
Rob Landley42ecbab2007-12-18 02:02:21 -0600136int copy_tempfile(int fdin, char *name, char **tempname);
137void delete_tempfile(int fdin, int fdout, char **tempname);
138void replace_tempfile(int fdin, int fdout, char **tempname);
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600139void crc_init(unsigned int *crc_table, int little_endian);
Rob Landley26e7b5e2012-02-02 07:27:35 -0600140void terminal_size(unsigned *x, unsigned *y);
Rob Landleyf793d532012-02-27 21:56:49 -0600141int yesno(char *prompt, int def);
Rob Landleyebcf0be2012-02-18 18:53:57 -0600142void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid));
Rob Landley26e7b5e2012-02-02 07:27:35 -0600143
landley4f344e32006-10-05 16:18:03 -0400144
landleycd9dfc32006-10-18 18:38:16 -0400145// getmountlist.c
landley4f344e32006-10-05 16:18:03 -0400146struct mtab_list {
147 struct mtab_list *next;
landley09ea7ac2006-10-30 01:38:00 -0500148 struct stat stat;
149 struct statvfs statvfs;
landley4f344e32006-10-05 16:18:03 -0400150 char *dir;
151 char *device;
152 char type[0];
153};
154
155struct mtab_list *getmountlist(int die);
156
Rob Landley6000f132007-01-18 22:00:12 -0500157void bunzipStream(int src_fd, int dst_fd);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600158
159// signal
160
161int sig_to_num(char *pidstr);
162char *num_to_sig(int sig);
Daniel Walter05744b32012-03-19 19:57:56 -0500163
164mode_t string_to_mode(char *mode_str, mode_t base);