OSDN Git Service

34ded0009f380000b1355bb693df419f3259c0f8
[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 // Unfortunately, sizeof() doesn't work in a preprocessor test.  TODO.
7
8 //#if sizeof(double) <= sizeof(long)
9 //typedef double FLOAT;
10 //#else
11 typedef float FLOAT;
12 //#endif
13
14 // libc generally has this, but the headers are screwed up
15 ssize_t getline(char **lineptr, size_t *n, FILE *stream);
16
17 // llist.c
18
19 // All these list types can be handled by the same code because first element
20 // is always next pointer, so next = (mytype *)&struct.
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 void llist_traverse(void *list, void (*using)(void *data));
38 void *llist_pop(void *list);  // actually void **list, but the compiler's dumb
39 void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
40 struct double_list *dlist_add(struct double_list **list, char *data);
41
42 // args.c
43 void get_optflags(void);
44
45 // dirtree.c
46
47 // Values returnable from callback function (bitfield, or them together)
48 // Default with no callback is 0
49
50 // 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.)
56 #define DIRTREE_COMEAGAIN    4
57 // Follow symlinks to directories
58 #define DIRTREE_SYMFOLLOW    8
59 // Don't look at any more files in this directory.
60 #define DIRTREE_ABORT      256
61
62 #define DIRTREE_ABORTVAL ((struct dirtree *)1)
63
64 struct dirtree {
65   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[];
71 };
72
73 struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow);
74 char *dirtree_path(struct dirtree *node, int *plen);
75 int dirtree_notdotdot(struct dirtree *catch);
76 int dirtree_parentfd(struct dirtree *node);
77 struct dirtree *handle_callback(struct dirtree *new,
78   int (*callback)(struct dirtree *node));
79 void dirtree_recurse(struct dirtree *node,
80   int (*callback)(struct dirtree *node), int symfollow);
81 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
82
83 // lib.c
84 void xstrcpy(char *dest, char *src, size_t size);
85 void verror_msg(char *msg, int err, va_list va);
86 void error_msg(char *msg, ...);
87 void perror_msg(char *msg, ...);
88 void error_exit(char *msg, ...) noreturn;
89 void perror_exit(char *msg, ...) noreturn;
90 void *xmalloc(size_t size);
91 void *xzalloc(size_t size);
92 void *xrealloc(void *ptr, size_t size);
93 char *xstrndup(char *s, size_t n);
94 char *xstrdup(char *s);
95 char *xmsprintf(char *format, ...);
96 void xprintf(char *format, ...);
97 void xputs(char *s);
98 void xputc(char c);
99 void xflush(void);
100 void xexec(char **argv);
101 void xaccess(char *path, int flags);
102 void xunlink(char *path);
103 int xcreate(char *path, int flags, int mode);
104 int xopen(char *path, int flags);
105 void xclose(int fd);
106 int xdup(int fd);
107 FILE *xfopen(char *path, char *mode);
108 ssize_t readall(int fd, void *buf, size_t len);
109 ssize_t writeall(int fd, void *buf, size_t len);
110 size_t xread(int fd, void *buf, size_t len);
111 void xreadall(int fd, void *buf, size_t len);
112 void xwrite(int fd, void *buf, size_t len);
113 off_t xlseek(int fd, off_t offset, int whence);
114 off_t lskip(int fd, off_t offset);
115 char *readfile(char *name);
116 char *xreadfile(char *name);
117 char *xgetcwd(void);
118 void xstat(char *path, struct stat *st);
119 char *xabspath(char *path, unsigned missing);
120 char *xrealpath(char *path);
121 void xchdir(char *path);
122 void xmkpath(char *path, int mode);
123 void xsetuid(uid_t uid);
124 struct string_list *find_in_path(char *path, char *filename);
125 void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
126 void itoa_to_buf(int n, char *buf, unsigned buflen);
127 char *utoa(unsigned n);
128 char *itoa(int n);
129 long atolx(char *c);
130 int numlen(long l);
131 int stridx(char *haystack, char needle);
132 off_t fdlength(int fd);
133 char *xreadlink(char *name);
134 void loopfiles_rw(char **argv, int flags, int permissions, int failok,
135   void (*function)(int fd, char *name));
136 void loopfiles(char **argv, void (*function)(int fd, char *name));
137 char *get_rawline(int fd, long *plen, char end);
138 char *get_line(int fd);
139 void xsendfile(int in, int out);
140 int wfchmodat(int rc, char *name, mode_t mode);
141 int copy_tempfile(int fdin, char *name, char **tempname);
142 void delete_tempfile(int fdin, int fdout, char **tempname);
143 void replace_tempfile(int fdin, int fdout, char **tempname);
144 void crc_init(unsigned int *crc_table, int little_endian);
145 void terminal_size(unsigned *x, unsigned *y);
146 int yesno(char *prompt, int def);
147 void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid));
148
149
150 // getmountlist.c
151 struct mtab_list {
152   struct mtab_list *next;
153   struct stat stat;
154   struct statvfs statvfs;
155   char *dir;
156   char *device;
157   char type[0];
158 };
159
160 struct mtab_list *getmountlist(int die);
161
162 void bunzipStream(int src_fd, int dst_fd);
163
164 // signal
165
166 void sigatexit(void *handler);
167 int sig_to_num(char *pidstr);
168 char *num_to_sig(int sig);
169
170 mode_t string_to_mode(char *mode_str, mode_t base);
171
172 // password helper functions
173 int read_password(char * buff, int buflen, char* mesg);
174 int update_password(char *filename, char* username, char* encrypted);
175
176 // du helper functions
177 char* make_human_readable(unsigned long long size, unsigned long unit);
178
179 // cut helper functions
180 unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange);