blob: 34ded0009f380000b1355bb693df419f3259c0f8 [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
20// is always next pointer, so next = (mytype *)&struct.
21
Rob Landley0a04b3e2006-11-03 00:05:52 -050022struct string_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060023 struct string_list *next;
24 char str[0];
Rob Landley0a04b3e2006-11-03 00:05:52 -050025};
26
Rob Landley8324b892006-11-19 02:49:22 -050027struct arg_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060028 struct arg_list *next;
29 char *arg;
Rob Landley8324b892006-11-19 02:49:22 -050030};
31
Rob Landleybc078652007-12-15 21:47:25 -060032struct double_list {
Rob Landley7aa651a2012-11-13 17:14:08 -060033 struct double_list *next, *prev;
34 char *data;
Rob Landleybc078652007-12-15 21:47:25 -060035};
36
Rob Landley9e2b6db2012-07-15 17:22:04 -050037void llist_traverse(void *list, void (*using)(void *data));
Rob Landley6ef04ef2008-01-20 17:34:53 -060038void *llist_pop(void *list); // actually void **list, but the compiler's dumb
Rob Landley2c482472012-03-12 00:25:40 -050039void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
Rob Landleybdf037f2008-10-23 16:44:30 -050040struct double_list *dlist_add(struct double_list **list, char *data);
Rob Landley6ef04ef2008-01-20 17:34:53 -060041
Rob Landley103b7e02007-10-04 02:04:10 -050042// args.c
43void get_optflags(void);
44
45// dirtree.c
Rob Landleyeb7ea222012-04-14 22:30:41 -050046
47// Values returnable from callback function (bitfield, or them together)
48// Default with no callback is 0
49
Rob Landley8c4ae8a2012-05-20 15:00:19 -050050// Add this node to the tree
51#define DIRTREE_SAVE 1
52// Recurse into children
53#define DIRTREE_RECURSE 2
54// Call again after handling all children of this directory
55// (Ignored for non-directories, sets linklen = -1 before second call.)
Rob Landleyeb7ea222012-04-14 22:30:41 -050056#define DIRTREE_COMEAGAIN 4
57// Follow symlinks to directories
58#define DIRTREE_SYMFOLLOW 8
Rob Landley8c4ae8a2012-05-20 15:00:19 -050059// Don't look at any more files in this directory.
60#define DIRTREE_ABORT 256
Rob Landleyeb7ea222012-04-14 22:30:41 -050061
62#define DIRTREE_ABORTVAL ((struct dirtree *)1)
63
Rob Landleyd25f7e42007-02-03 14:11:26 -050064struct dirtree {
Rob Landley7aa651a2012-11-13 17:14:08 -060065 struct dirtree *next, *parent, *child;
66 long extra; // place for user to store their stuff (can be pointer)
67 struct stat st;
68 char *symlink;
69 int data; // dirfd for directory, linklen for symlink, -1 = comeagain
70 char name[];
Rob Landleyd25f7e42007-02-03 14:11:26 -050071};
72
Rob Landley4af1e1d2012-06-09 22:25:49 -050073struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow);
Rob Landleyeb7ea222012-04-14 22:30:41 -050074char *dirtree_path(struct dirtree *node, int *plen);
Rob Landley8c4ae8a2012-05-20 15:00:19 -050075int dirtree_notdotdot(struct dirtree *catch);
Rob Landley6ec21782012-06-16 15:16:08 -050076int dirtree_parentfd(struct dirtree *node);
Rob Landleyeb7ea222012-04-14 22:30:41 -050077struct dirtree *handle_callback(struct dirtree *new,
Rob Landley7aa651a2012-11-13 17:14:08 -060078 int (*callback)(struct dirtree *node));
Rob Landleyeb7ea222012-04-14 22:30:41 -050079void dirtree_recurse(struct dirtree *node,
Rob Landley7aa651a2012-11-13 17:14:08 -060080 int (*callback)(struct dirtree *node), int symfollow);
Rob Landleyeb7ea222012-04-14 22:30:41 -050081struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
Rob Landley8324b892006-11-19 02:49:22 -050082
Rob Landley103b7e02007-10-04 02:04:10 -050083// lib.c
Rob Landleye15850a2007-11-19 01:51:00 -060084void xstrcpy(char *dest, char *src, size_t size);
landley09ea7ac2006-10-30 01:38:00 -050085void verror_msg(char *msg, int err, va_list va);
86void error_msg(char *msg, ...);
87void perror_msg(char *msg, ...);
Rob Landleyefa93b92007-11-15 21:12:24 -060088void error_exit(char *msg, ...) noreturn;
89void perror_exit(char *msg, ...) noreturn;
landley4f344e32006-10-05 16:18:03 -040090void *xmalloc(size_t size);
landleycd9dfc32006-10-18 18:38:16 -040091void *xzalloc(size_t size);
Rob Landley0c93f6c2007-04-29 19:55:21 -040092void *xrealloc(void *ptr, size_t size);
Rob Landley1e01cd12010-01-05 10:48:32 -060093char *xstrndup(char *s, size_t n);
94char *xstrdup(char *s);
landley00f87f12006-10-25 18:38:37 -040095char *xmsprintf(char *format, ...);
Rob Landley24d1d452007-01-20 18:04:20 -050096void xprintf(char *format, ...);
Rob Landley5084fea2007-06-18 00:14:03 -040097void xputs(char *s);
Rob Landley24d1d452007-01-20 18:04:20 -050098void xputc(char c);
99void xflush(void);
landley09ea7ac2006-10-30 01:38:00 -0500100void xexec(char **argv);
Rob Landleyd3e9d642007-01-08 03:25:47 -0500101void xaccess(char *path, int flags);
Rob Landleye745d8e2007-12-20 06:30:19 -0600102void xunlink(char *path);
Rob Landley1322beb2007-01-07 22:51:12 -0500103int xcreate(char *path, int flags, int mode);
104int xopen(char *path, int flags);
Rob Landleybc078652007-12-15 21:47:25 -0600105void xclose(int fd);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500106int xdup(int fd);
landley4f344e32006-10-05 16:18:03 -0400107FILE *xfopen(char *path, char *mode);
Rob Landley90163772007-01-18 21:54:08 -0500108ssize_t readall(int fd, void *buf, size_t len);
109ssize_t writeall(int fd, void *buf, size_t len);
110size_t xread(int fd, void *buf, size_t len);
111void xreadall(int fd, void *buf, size_t len);
112void xwrite(int fd, void *buf, size_t len);
Rob Landley52476712009-01-18 16:19:25 -0600113off_t xlseek(int fd, off_t offset, int whence);
Rob Landley2037b832012-07-15 16:56:20 -0500114off_t lskip(int fd, off_t offset);
Rob Landleye824ed12008-07-18 08:43:18 -0500115char *readfile(char *name);
116char *xreadfile(char *name);
landley00f87f12006-10-25 18:38:37 -0400117char *xgetcwd(void);
Rob Landleyd25f7e42007-02-03 14:11:26 -0500118void xstat(char *path, struct stat *st);
Rob Landleybd2e2272012-11-20 09:21:52 -0600119char *xabspath(char *path, unsigned missing);
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 Landley0a04b3e2006-11-03 00:05:52 -0500124struct string_list *find_in_path(char *path, char *filename);
landley09ea7ac2006-10-30 01:38:00 -0500125void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
126void itoa_to_buf(int n, char *buf, unsigned buflen);
127char *utoa(unsigned n);
128char *itoa(int n);
Rob Landleyf5757162007-02-16 21:08:22 -0500129long atolx(char *c);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500130int numlen(long l);
Rob Landley2037b832012-07-15 16:56:20 -0500131int stridx(char *haystack, char needle);
Rob Landleye2580db2007-01-23 13:20:38 -0500132off_t fdlength(int fd);
Rob Landley0c93f6c2007-04-29 19:55:21 -0400133char *xreadlink(char *name);
Rob Landleyad63f4b2011-12-12 15:19:52 -0600134void loopfiles_rw(char **argv, int flags, int permissions, int failok,
Rob Landley7aa651a2012-11-13 17:14:08 -0600135 void (*function)(int fd, char *name));
Rob Landley7634b552007-11-29 17:49:50 -0600136void loopfiles(char **argv, void (*function)(int fd, char *name));
Rob Landley3fc4e0f2008-04-13 00:29:00 -0500137char *get_rawline(int fd, long *plen, char end);
Rob Landleybc078652007-12-15 21:47:25 -0600138char *get_line(int fd);
139void xsendfile(int in, int out);
Rob Landley67a069d2012-06-03 00:32:12 -0500140int wfchmodat(int rc, char *name, mode_t mode);
Rob Landley42ecbab2007-12-18 02:02:21 -0600141int copy_tempfile(int fdin, char *name, char **tempname);
142void delete_tempfile(int fdin, int fdout, char **tempname);
143void replace_tempfile(int fdin, int fdout, char **tempname);
Rob Landleyb15b8fa2009-01-05 01:05:43 -0600144void crc_init(unsigned int *crc_table, int little_endian);
Rob Landley26e7b5e2012-02-02 07:27:35 -0600145void terminal_size(unsigned *x, unsigned *y);
Rob Landleyf793d532012-02-27 21:56:49 -0600146int yesno(char *prompt, int def);
Rob Landleyebcf0be2012-02-18 18:53:57 -0600147void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid));
Rob Landley26e7b5e2012-02-02 07:27:35 -0600148
landley4f344e32006-10-05 16:18:03 -0400149
landleycd9dfc32006-10-18 18:38:16 -0400150// getmountlist.c
landley4f344e32006-10-05 16:18:03 -0400151struct mtab_list {
Rob Landley7aa651a2012-11-13 17:14:08 -0600152 struct mtab_list *next;
153 struct stat stat;
154 struct statvfs statvfs;
155 char *dir;
156 char *device;
157 char type[0];
landley4f344e32006-10-05 16:18:03 -0400158};
159
160struct mtab_list *getmountlist(int die);
161
Rob Landley6000f132007-01-18 22:00:12 -0500162void bunzipStream(int src_fd, int dst_fd);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600163
164// signal
165
Rob Landleyc52db602012-07-30 01:01:33 -0500166void sigatexit(void *handler);
Rob Landley2dd50ad2012-02-26 13:48:00 -0600167int sig_to_num(char *pidstr);
168char *num_to_sig(int sig);
Daniel Walter05744b32012-03-19 19:57:56 -0500169
170mode_t string_to_mode(char *mode_str, mode_t base);
Rob Landley2c917f52012-07-17 08:54:47 -0500171
172// password helper functions
Rob Landley6ba38c22012-07-21 18:38:36 -0500173int read_password(char * buff, int buflen, char* mesg);
174int update_password(char *filename, char* username, char* encrypted);
Rob Landley2c917f52012-07-17 08:54:47 -0500175
Ashwini Kumar1a0eedf2012-08-26 21:17:00 -0500176// du helper functions
177char* make_human_readable(unsigned long long size, unsigned long unit);
Rob Landley734b5302012-11-16 12:26:48 -0600178
179// cut helper functions
180unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange);