aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/toybox.h
blob: 1f64310de2105ac96528d19defd40354e8321455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

#ifdef __GNUC__
#define printf_format	__attribute__((format(printf, 1, 2)))
#else
#define printf_format
#endif

// General posix-2008 headers
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <grp.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <paths.h>
#include <pwd.h>
#include <regex.h>
#include <sched.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <syslog.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>

// Posix networking

#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/un.h>

// Linux headers not listed by POSIX or LSB
#include <sys/mount.h>
#ifdef __linux__
#include <sys/statfs.h>
#include <sys/swap.h>
#include <sys/sysinfo.h>
#endif

#define WARN_ONLY (1<<31)

// xwrap.c
//void xstrncpy(char *dest, char *src, size_t size);
//void xstrncat(char *dest, char *src, size_t size);
void _xexit(void) __attribute__((__noreturn__));
void xexit(void) __attribute__((__noreturn__));
//void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t off);
void *xmalloc(size_t size);
void *xzalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrndup(char *s, size_t n);
char *xstrdup(char *s);
void *xmemdup(void *s, long len);
char *xmprintf(char *format, ...) printf_format;
//void xprintf(char *format, ...) printf_format;
//void xputsl(char *s, int len);
//void xputsn(char *s);
//void xputs(char *s);
//void xputc(char c);
void xflush(int flush);
//void xexec(char **argv);
//pid_t xpopen_setup(char **argv, int *pipes, void (*callback)(void));
//pid_t xpopen_both(char **argv, int *pipes);
//int xwaitpid(pid_t pid);
//int xpclose_both(pid_t pid, int *pipes);
//pid_t xpopen(char **argv, int *pipe, int isstdout);
//pid_t xpclose(pid_t pid, int pipe);
//int xrun(char **argv);
//int xpspawn(char **argv, int*pipes);
//void xaccess(char *path, int flags);
//void xunlink(char *path);
//void xrename(char *from, char *to);
//int xtempfile(char *name, char **tempname);
//int xcreate(char *path, int flags, int mode);
int xopen(char *path, int flags);
int xcreate_stdio(char *path, int flags, int mode);
int xopen_stdio(char *path, int flags);
int openro(char *path, int flags);
int xopenro(char *path);
//void xpipe(int *pp);
void xclose(int fd);
int xdup(int fd);
int notstdio(int fd);
//FILE *xfdopen(int fd, char *mode);
//FILE *xfopen(char *path, char *mode);
//size_t xread(int fd, void *buf, size_t len);
//void xreadall(int fd, void *buf, size_t len);
//void xwrite(int fd, void *buf, size_t len);
//off_t xlseek(int fd, off_t offset, int whence);
//char *xreadfile(char *name, char *buf, off_t len);
//int xioctl(int fd, int request, void *data);
//char *xgetcwd(void);
//void xstat(char *path, struct stat *st);
//char *xabspath(char *path, int exact);
//void xchdir(char *path);
//void xchroot(char *path);
//struct passwd *xgetpwuid(uid_t uid);
//struct group *xgetgrgid(gid_t gid);
//struct passwd *xgetpwnam(char *name);
//struct group *xgetgrnam(char *name);
//unsigned xgetuid(char *name);
//unsigned xgetgid(char *name);
//void xsetuser(struct passwd *pwd);
//char *xreadlink(char *name);
//double xstrtod(char *s);
//long xparsetime(char *arg, long units, long *fraction);
//long long xparsemillitime(char *arg);
//void xpidfile(char *name);
void xregcomp(regex_t *preg, char *rexec, int cflags);
//char *xtzset(char *new);
//void xsignal_flags(int signal, void *handler, int flags);
//void xsignal(int signal, void *handler);
//time_t xvali_date(struct tm *tm, char *str);
//void xparsedate(char *str, time_t *t, unsigned *nano, int endian);
//char *xgetline(FILE *fp, int *len);

// lib.c
void tb_verror_msg(char *msg, int err, va_list va);
void tb_error_msg(char *msg, ...) printf_format;
void tb_perror_msg(char *msg, ...) printf_format;
void tb_error_exit(char *msg, ...) printf_format __attribute__((__noreturn__));
void tb_perror_exit(char *msg, ...) printf_format __attribute__((__noreturn__));
//void help_exit(char *msg, ...) printf_format __attribute__((__noreturn__));
void tb_error_msg_raw(char *msg);
void tb_perror_msg_raw(char *msg);
void tb_error_exit_raw(char *msg);
void tb_perror_exit_raw(char *msg);
//ssize_t readall(int fd, void *buf, size_t len);
//ssize_t writeall(int fd, void *buf, size_t len);
//off_t lskip(int fd, off_t offset);
//#define MKPATHAT_MKLAST  1
//#define MKPATHAT_MAKE    2
//#define MKPATHAT_VERBOSE 4
//int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
//int mkpath(char *dir);
//struct string_list **splitpath(char *path, struct string_list **list);
//char *readfileat(int dirfd, char *name, char *buf, off_t *len);
//char *readfile(char *name, char *buf, off_t len);
void msleep(long milliseconds);
//void nanomove(struct timespec *ts, long long offset);
//long long nanodiff(struct timespec *old, struct timespec *new);
//int highest_bit(unsigned long l);
//int64_t peek_le(void *ptr, unsigned size);
//int64_t peek_be(void *ptr, unsigned size);
//int64_t peek(void *ptr, unsigned size);
//void poke_le(void *ptr, long long val, unsigned size);
//void poke_be(void *ptr, long long val, unsigned size);
//void poke(void *ptr, long long val, unsigned size);
//struct string_list *find_in_path(char *path, char *filename);
//long long estrtol(char *str, char **end, int base);
//long long xstrtol(char *str, char **end, int base);
//long long atolx(char *c);
//long long atolx_range(char *numstr, long long low, long long high);
//int stridx(char *haystack, char needle);
//int utf8towc(wchar_t *wc, char *str, unsigned len);
//char *strlower(char *s);
//char *strafter(char *haystack, char *needle);
//char *chomp(char *s);
//int unescape(char c);
//char *strend(char *str, char *suffix);
//int strstart(char **a, char *b);
//int strcasestart(char **a, char *b);
//off_t fdlength(int fd);
//void loopfiles_rw(char **argv, int flags, int permissions,
//  void (*function)(int fd, char *name));
//void loopfiles(char **argv, void (*function)(int fd, char *name));
//void loopfiles_lines(char **argv, void (*function)(char **pline, long len));
//long long sendfile_len(int in, int out, long long len, long long *consumed);
//long long xsendfile_len(int in, int out, long long len);
//void xsendfile_pad(int in, int out, long long len);
//long long xsendfile(int in, int out);
//int wfchmodat(int rc, char *name, mode_t mode);
//int copy_tempfile(int fdin, char *name, char **tempname);
//void delete_tempfile(int fdin, int fdout, char **tempname);
//void replace_tempfile(int fdin, int fdout, char **tempname);
//void crc_init(unsigned int *crc_table, int little_endian);
//void base64_init(char *p);
//int yesno(int def);
//int fyesno(FILE *fp, int def);
int qstrcmp(const void *a, const void *b);
//void tb_create_uuid(char *uuid);
//char *tb_show_uuid(char *uuid);
//char *next_printf(char *s, char **start);
//struct passwd *bufgetpwuid(uid_t uid);
//struct group *bufgetgrgid(gid_t gid);
//int readlinkat0(int dirfd, char *path, char *buf, int len);
//int readlink0(char *path, char *buf, int len);
//int regexec0(regex_t *preg, char *string, long len, int nmatch,
//  regmatch_t pmatch[], int eflags);
//char *getusername(uid_t uid);
//char *getgroupname(gid_t gid);
//void do_lines(int fd, char delim, void (*call)(char **pline, long len));
//long long millitime(void);
//char *format_iso_time(char *buf, size_t len, struct timespec *ts);
//void reset_env(struct passwd *p, int clear);
//void loggit(int priority, char *format, ...);
//unsigned tar_cksum(void *data);
//int is_tar_header(void *pkt);
//char *elf_arch_name(int type);


// pending.c
char *get_line(int fd);


// dirtree.c

// Values returnable from callback function (bitfield, or them together)
// Default with no callback is 0

// Add this node to the tree
#define TB_DIRTREE_SAVE         1
// Recurse into children
#define TB_DIRTREE_RECURSE      2
// Call again after handling all children of this directory
// (Ignored for non-directories, sets linklen = -1 before second call.)
#define TB_DIRTREE_COMEAGAIN    4
// Follow symlinks to directories
#define TB_DIRTREE_SYMFOLLOW    8
// Don't warn about failure to stat
#define TB_DIRTREE_SHUTUP      16
// Breadth first traversal, conserves filehandles at the expense of memory
#define TB_DIRTREE_BREADTH     32
// skip non-numeric entries
#define TB_DIRTREE_PROC        64
// Return files we can't stat
#define TB_DIRTREE_STATLESS    128
// Don't look at any more files in this directory.
#define TB_DIRTREE_ABORT      256

#define TB_DIRTREE_ABORTVAL ((struct tb_dirtree *)1)

struct tb_dirtree {
  struct tb_dirtree *next, *parent, *child;
  long extra; // place for user to store their stuff (can be pointer)
  char *symlink;
  int dirfd;
  struct stat st;
  char again;
  char name[];
};

int tb_isdotdot(char *name);
struct tb_dirtree *tb_dirtree_add_node(struct tb_dirtree *p, char *name, int flags);
char *tb_dirtree_path(struct tb_dirtree *node, int *plen);
int tb_dirtree_notdotdot(struct tb_dirtree *catch);
int tb_dirtree_parentfd(struct tb_dirtree *node);
int tb_dirtree_recurse(struct tb_dirtree *node, int (*callback)(struct tb_dirtree *node),
  int dirfd, int symfollow);

// Rob forget to do this, but at least he didn't declare it static.
struct tb_dirtree *tb_dirtree_handle_callback(struct tb_dirtree *new, int (*callback)(struct tb_dirtree *node));
void tb_dirtree_free(struct tb_dirtree *new);