1 /* lib.h - header file for lib directory
3 * Copyright 2006 Rob Landley <rob@landley.net>
8 // All these list types can be handled by the same code because first element
9 // is always next pointer, so next = (mytype *)&struct. (The payloads are
10 // named differently to catch using the wrong type early.)
13 struct string_list *next;
18 struct arg_list *next;
23 struct double_list *next, *prev;
27 void llist_free_arg(void *node);
28 void llist_free_double(void *node);
29 void llist_traverse(void *list, void (*using)(void *node));
30 void *llist_pop(void *list); // actually void **list
31 void *dlist_pop(void *list); // actually struct double_list **list
32 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
33 struct double_list *dlist_add(struct double_list **list, char *data);
34 void *dlist_terminate(void *list);
37 void get_optflags(void);
41 // Values returnable from callback function (bitfield, or them together)
42 // Default with no callback is 0
44 // 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.)
50 #define DIRTREE_COMEAGAIN 4
51 // Follow symlinks to directories
52 #define DIRTREE_SYMFOLLOW 8
53 // Don't look at any more files in this directory.
54 #define DIRTREE_ABORT 256
56 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
59 struct dirtree *next, *parent, *child;
60 long extra; // place for user to store their stuff (can be pointer)
63 int data; // dirfd for directory, linklen for symlink, -1 = comeagain
67 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int symfollow);
68 char *dirtree_path(struct dirtree *node, int *plen);
69 int dirtree_notdotdot(struct dirtree *catch);
70 int dirtree_parentfd(struct dirtree *node);
71 struct dirtree *dirtree_handle_callback(struct dirtree *new,
72 int (*callback)(struct dirtree *node));
73 void dirtree_recurse(struct dirtree *node,
74 int (*callback)(struct dirtree *node), int symfollow);
75 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
82 void xstrncpy(char *dest, char *src, size_t size);
83 void xexit(void) noreturn;
84 void *xmalloc(size_t size);
85 void *xzalloc(size_t size);
86 void *xrealloc(void *ptr, size_t size);
87 char *xstrndup(char *s, size_t n);
88 char *xstrdup(char *s);
89 char *xmprintf(char *format, ...);
90 void xprintf(char *format, ...);
95 void xexec_optargs(int skip);
96 void xexec(char **argv);
97 void xaccess(char *path, int flags);
98 void xunlink(char *path);
99 int xcreate(char *path, int flags, int mode);
100 int xopen(char *path, int flags);
103 FILE *xfdopen(int fd, char *mode);
104 FILE *xfopen(char *path, char *mode);
105 size_t xread(int fd, void *buf, size_t len);
106 void xreadall(int fd, void *buf, size_t len);
107 void xwrite(int fd, void *buf, size_t len);
108 off_t xlseek(int fd, off_t offset, int whence);
109 char *xreadfile(char *name, char *buf, off_t len);
110 int xioctl(int fd, int request, void *data);
112 void xstat(char *path, struct stat *st);
113 char *xabspath(char *path, int exact);
114 char *xrealpath(char *path);
115 void xchdir(char *path);
116 void xchroot(char *path);
117 struct passwd *xgetpwuid(uid_t uid);
118 struct group *xgetgrgid(gid_t gid);
119 struct passwd *xgetpwnam(char *name);
120 void xsetuser(struct passwd *pwd);
121 char *xreadlink(char *name);
122 long xparsetime(char *arg, long units, long *fraction);
123 void xpidfile(char *name);
124 void xregcomp(regex_t *preg, char *rexec, int cflags);
127 void verror_msg(char *msg, int err, va_list va);
128 void error_msg(char *msg, ...);
129 void perror_msg(char *msg, ...);
130 void error_exit(char *msg, ...) noreturn;
131 void perror_exit(char *msg, ...) noreturn;
132 ssize_t readall(int fd, void *buf, size_t len);
133 ssize_t writeall(int fd, void *buf, size_t len);
134 off_t lskip(int fd, off_t offset);
135 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
136 struct string_list **splitpath(char *path, struct string_list **list);
137 char *readfile(char *name, char *buf, off_t len);
138 void msleep(long miliseconds);
139 int64_t peek(void *ptr, int size);
140 void poke(void *ptr, uint64_t val, int size);
141 struct string_list *find_in_path(char *path, char *filename);
143 long atolx_range(char *numstr, long low, long high);
145 int stridx(char *haystack, char needle);
146 off_t fdlength(int fd);
147 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
148 void (*function)(int fd, char *name));
149 void loopfiles(char **argv, void (*function)(int fd, char *name));
150 char *get_rawline(int fd, long *plen, char end);
151 char *get_line(int fd);
152 void xsendfile(int in, int out);
153 int wfchmodat(int rc, char *name, mode_t mode);
154 int copy_tempfile(int fdin, char *name, char **tempname);
155 void delete_tempfile(int fdin, int fdout, char **tempname);
156 void replace_tempfile(int fdin, int fdout, char **tempname);
157 void crc_init(unsigned int *crc_table, int little_endian);
158 int terminal_size(unsigned *x, unsigned *y);
159 int yesno(char *prompt, int def);
160 int human_readable(char *buf, unsigned long long num);
163 int xsocket(int domain, int type, int protocol);
167 struct mtab_list *next, *prev;
169 struct statvfs statvfs;
176 struct mtab_list *xgetmountlist(char *path);
180 void generic_signal(int signal);
181 void sigatexit(void *handler);
182 int sig_to_num(char *pidstr);
183 char *num_to_sig(int sig);
185 mode_t string_to_mode(char *mode_str, mode_t base);
186 void mode_to_string(mode_t mode, char *buf);
187 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
189 // Functions in need of further review/cleanup
190 #include "lib/pending.h"