OSDN Git Service

Merge remote-tracking branch 'toybox/master' into HEAD
[android-x86/external-toybox.git] / lib / lib.h
1 /* lib.h - header file for lib directory
2  *
3  * Copyright 2006 Rob Landley <rob@landley.net>
4  */
5
6 struct ptr_len {
7   void *ptr;
8   long len;
9 };
10
11 struct str_len {
12   char *str;
13   long len;
14 };
15
16 // llist.c
17
18 // All these list types can be handled by the same code because first element
19 // is always next pointer, so next = (mytype *)&struct. (The payloads are
20 // named differently to catch using the wrong type early.)
21
22 struct string_list {
23   struct string_list *next;
24   char str[0];
25 };
26
27 struct arg_list {
28   struct arg_list *next;
29   char *arg;
30 };
31
32 struct double_list {
33   struct double_list *next, *prev;
34   char *data;
35 };
36
37 struct num_cache {
38   struct num_cache *next;
39   long long num;
40   char data[];
41 };
42
43 void llist_free_arg(void *node);
44 void llist_free_double(void *node);
45 void llist_traverse(void *list, void (*using)(void *node));
46 void *llist_pop(void *list);  // actually void **list
47 void *dlist_pop(void *list);  // actually struct double_list **list
48 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
49 struct double_list *dlist_add(struct double_list **list, char *data);
50 void *dlist_terminate(void *list);
51 struct num_cache *get_num_cache(struct num_cache *cache, long long num);
52 struct num_cache *add_num_cache(struct num_cache **cache, long long num,
53   void *data, int len);
54
55 // args.c
56 void get_optflags(void);
57
58 // dirtree.c
59
60 // Values returnable from callback function (bitfield, or them together)
61 // Default with no callback is 0
62
63 // Add this node to the tree
64 #define DIRTREE_SAVE         1
65 // Recurse into children
66 #define DIRTREE_RECURSE      2
67 // Call again after handling all children of this directory
68 // (Ignored for non-directories, sets linklen = -1 before second call.)
69 #define DIRTREE_COMEAGAIN    4
70 // Follow symlinks to directories
71 #define DIRTREE_SYMFOLLOW    8
72 // Don't warn about failure to stat
73 #define DIRTREE_SHUTUP      16
74 // Breadth first traversal, conserves filehandles at the expense of memory
75 #define DIRTREE_BREADTH     32
76 // skip non-numeric entries
77 #define DIRTREE_PROC        64
78 // Don't look at any more files in this directory.
79 #define DIRTREE_ABORT      256
80
81 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
82
83 struct dirtree {
84   struct dirtree *next, *parent, *child;
85   long extra; // place for user to store their stuff (can be pointer)
86   struct stat st;
87   char *symlink;
88   int dirfd;
89   char again;
90   char name[];
91 };
92
93 int isdotdot(char *name);
94 struct dirtree *dirtree_add_node(struct dirtree *p, char *name, int flags);
95 char *dirtree_path(struct dirtree *node, int *plen);
96 int dirtree_notdotdot(struct dirtree *catch);
97 int dirtree_parentfd(struct dirtree *node);
98 int dirtree_recurse(struct dirtree *node, int (*callback)(struct dirtree *node),
99   int dirfd, int symfollow);
100 struct dirtree *dirtree_flagread(char *path, int flags,
101   int (*callback)(struct dirtree *node));
102 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
103
104 // help.c
105
106 void show_help(FILE *out);
107
108 // Tell xopen and friends to print warnings but return -1 as necessary
109 // The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
110 // plenty of headroom.
111 #define WARN_ONLY (1<<31)
112
113 // xwrap.c
114 void xstrncpy(char *dest, char *src, size_t size);
115 void xstrncat(char *dest, char *src, size_t size);
116 void _xexit(void) noreturn;
117 void xexit(void) noreturn;
118 void *xmalloc(size_t size);
119 void *xzalloc(size_t size);
120 void *xrealloc(void *ptr, size_t size);
121 char *xstrndup(char *s, size_t n);
122 char *xstrdup(char *s);
123 void *xmemdup(void *s, long len);
124 char *xmprintf(char *format, ...) printf_format;
125 void xprintf(char *format, ...) printf_format;
126 void xputs(char *s);
127 void xputc(char c);
128 void xflush(void);
129 void xexec(char **argv);
130 pid_t xpopen_both(char **argv, int *pipes);
131 int xwaitpid(pid_t pid);
132 int xpclose_both(pid_t pid, int *pipes);
133 pid_t xpopen(char **argv, int *pipe, int isstdout);
134 pid_t xpclose(pid_t pid, int pipe);
135 int xrun(char **argv);
136 int xpspawn(char **argv, int*pipes);
137 void xaccess(char *path, int flags);
138 void xunlink(char *path);
139 int xcreate(char *path, int flags, int mode);
140 int xopen(char *path, int flags);
141 int xcreate_stdio(char *path, int flags, int mode);
142 int xopen_stdio(char *path, int flags);
143 int openro(char *path, int flags);
144 int xopenro(char *path);
145 void xpipe(int *pp);
146 void xclose(int fd);
147 int xdup(int fd);
148 int notstdio(int fd);
149 FILE *xfdopen(int fd, char *mode);
150 FILE *xfopen(char *path, char *mode);
151 size_t xread(int fd, void *buf, size_t len);
152 void xreadall(int fd, void *buf, size_t len);
153 void xwrite(int fd, void *buf, size_t len);
154 off_t xlseek(int fd, off_t offset, int whence);
155 char *xreadfile(char *name, char *buf, off_t len);
156 int xioctl(int fd, int request, void *data);
157 char *xgetcwd(void);
158 void xstat(char *path, struct stat *st);
159 char *xabspath(char *path, int exact);
160 void xchdir(char *path);
161 void xchroot(char *path);
162 struct passwd *xgetpwuid(uid_t uid);
163 struct group *xgetgrgid(gid_t gid);
164 struct passwd *xgetpwnam(char *name);
165 struct group *xgetgrnam(char *name);
166 unsigned xgetuid(char *name);
167 unsigned xgetgid(char *name);
168 void xsetuser(struct passwd *pwd);
169 char *xreadlink(char *name);
170 long xparsetime(char *arg, long units, long *fraction);
171 void xpidfile(char *name);
172 void xregcomp(regex_t *preg, char *rexec, int cflags);
173 char *xtzset(char *new);
174 void xsignal(int signal, void *handler);
175
176 // lib.c
177 void verror_msg(char *msg, int err, va_list va);
178 void error_msg(char *msg, ...) printf_format;
179 void perror_msg(char *msg, ...) printf_format;
180 void error_exit(char *msg, ...) printf_format noreturn;
181 void perror_exit(char *msg, ...) printf_format noreturn;
182 void help_exit(char *msg, ...) printf_format noreturn;
183 void error_msg_raw(char *msg);
184 void perror_msg_raw(char *msg);
185 void error_exit_raw(char *msg);
186 void perror_exit_raw(char *msg);
187 ssize_t readall(int fd, void *buf, size_t len);
188 ssize_t writeall(int fd, void *buf, size_t len);
189 off_t lskip(int fd, off_t offset);
190 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
191 struct string_list **splitpath(char *path, struct string_list **list);
192 char *readfileat(int dirfd, char *name, char *buf, off_t *len);
193 char *readfile(char *name, char *buf, off_t len);
194 void msleep(long miliseconds);
195 int64_t peek_le(void *ptr, unsigned size);
196 int64_t peek_be(void *ptr, unsigned size);
197 int64_t peek(void *ptr, unsigned size);
198 void poke(void *ptr, uint64_t val, int size);
199 struct string_list *find_in_path(char *path, char *filename);
200 long long estrtol(char *str, char **end, int base);
201 long long xstrtol(char *str, char **end, int base);
202 long long atolx(char *c);
203 long long atolx_range(char *numstr, long long low, long long high);
204 int stridx(char *haystack, char needle);
205 char *strlower(char *s);
206 char *strafter(char *haystack, char *needle);
207 char *chomp(char *s);
208 int unescape(char c);
209 int strstart(char **a, char *b);
210 off_t fdlength(int fd);
211 void loopfiles_rw(char **argv, int flags, int permissions,
212   void (*function)(int fd, char *name));
213 void loopfiles(char **argv, void (*function)(int fd, char *name));
214 long long xsendfile(int in, int out);
215 int wfchmodat(int rc, char *name, mode_t mode);
216 int copy_tempfile(int fdin, char *name, char **tempname);
217 void delete_tempfile(int fdin, int fdout, char **tempname);
218 void replace_tempfile(int fdin, int fdout, char **tempname);
219 void crc_init(unsigned int *crc_table, int little_endian);
220 void base64_init(char *p);
221 int yesno(int def);
222 int qstrcmp(const void *a, const void *b);
223 void create_uuid(char *uuid);
224 char *show_uuid(char *uuid);
225 char *next_printf(char *s, char **start);
226 char *strnstr(char *line, char *str);
227 int dev_minor(int dev);
228 int dev_major(int dev);
229 int dev_makedev(int major, int minor);
230 struct passwd *bufgetpwuid(uid_t uid);
231 struct group *bufgetgrgid(gid_t gid);
232 int readlinkat0(int dirfd, char *path, char *buf, int len);
233 int readlink0(char *path, char *buf, int len);
234 int regexec0(regex_t *preg, char *string, long len, int nmatch,
235   regmatch_t pmatch[], int eflags);
236 char *getusername(uid_t uid);
237 char *getgroupname(gid_t gid);
238 void do_lines(int fd, void (*call)(char **pline, long len));
239
240 #define HR_SPACE 1 // Space between number and units
241 #define HR_B     2 // Use "B" for single byte units
242 #define HR_1000  4 // Use decimal instead of binary units
243 int human_readable(char *buf, unsigned long long num, int style);
244
245 // linestack.c
246
247 struct linestack {
248   long len, max;
249   struct ptr_len idx[];
250 };
251
252 void linestack_addstack(struct linestack **lls, struct linestack *throw,
253   long pos);
254 void linestack_insert(struct linestack **lls, long pos, char *line, long len);
255 void linestack_append(struct linestack **lls, char *line);
256 struct linestack *linestack_load(char *name);
257 int crunch_escape(FILE *out, int cols, int wc);
258 int crunch_rev_escape(FILE *out, int cols, int wc);
259 int crunch_str(char **str, int width, FILE *out, char *escmore,
260   int (*escout)(FILE *out, int cols, int wc));
261 int draw_str(char *start, int width);
262 int utf8len(char *str);
263 int utf8skip(char *str, int width);
264 int draw_trim_esc(char *str, int padto, int width, char *escmore,
265   int (*escout)(FILE *out, int cols,int wc));
266 int draw_trim(char *str, int padto, int width);
267
268 // interestingtimes.c
269 int xgettty(void);
270 int terminal_size(unsigned *xx, unsigned *yy);
271 int terminal_probesize(unsigned *xx, unsigned *yy);
272 int scan_key_getsize(char *scratch, int miliwait, unsigned *xx, unsigned *yy);
273 int set_terminal(int fd, int raw, struct termios *old);
274 void xset_terminal(int fd, int raw, struct termios *old);
275 int scan_key(char *scratch, int miliwait);
276 void tty_esc(char *s);
277 void tty_jump(int x, int y);
278 void tty_reset(void);
279 void tty_sigreset(int i);
280
281 // net.c
282 int xsocket(int domain, int type, int protocol);
283 void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
284 int xconnect(char *host, char *port, int family, int socktype, int protocol,
285   int flags);
286 int xpoll(struct pollfd *fds, int nfds, int timeout);
287 int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
288
289 // password.c
290 int get_salt(char *salt, char * algo);
291
292 // getmountlist.c
293 struct mtab_list {
294   struct mtab_list *next, *prev;
295   struct stat stat;
296   struct statvfs statvfs;
297   char *dir;
298   char *device;
299   char *opts;
300   char type[0];
301 };
302
303 void comma_args(struct arg_list *al, void *data, char *err,
304   char *(*callback)(void *data, char *str, int len));
305 void comma_collate(char **old, char *new);
306 char *comma_iterate(char **list, int *len);
307 int comma_scan(char *optlist, char *opt, int clean);
308 int comma_scanall(char *optlist, char *scanlist);
309 int mountlist_istype(struct mtab_list  *ml, char *typelist);
310 struct mtab_list *xgetmountlist(char *path);
311
312 // signal
313
314 void generic_signal(int signal);
315 void exit_signal(int signal);
316 void sigatexit(void *handler);
317 int sig_to_num(char *pidstr);
318 char *num_to_sig(int sig);
319
320 mode_t string_to_mode(char *mode_str, mode_t base);
321 void mode_to_string(mode_t mode, char *buf);
322 char *getbasename(char *name);
323 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
324
325 pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid);
326 #define XVFORK() xvforkwrap(vfork())
327
328 // Wrapper to make xfuncs() return (via longjmp) instead of exiting.
329 // Assigns true/false "did it exit" value to first argument.
330 #define WOULD_EXIT(y, x) do { jmp_buf _noexit; \
331   int _noexit_res; \
332   toys.rebound = &_noexit; \
333   _noexit_res = setjmp(_noexit); \
334   if (!_noexit_res) do {x;} while(0); \
335   toys.rebound = 0; \
336   y = _noexit_res; \
337 } while(0);
338
339 // Wrapper that discards true/false "did it exit" value.
340 #define NOEXIT(x) WOULD_EXIT(_noexit_res, x)
341
342 // Functions in need of further review/cleanup
343 #include "lib/pending.h"