1 /* vi: set sw=4 ts=4: */
3 * Busybox main internal header file
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
21 * Permission has been granted to redistribute this code under the GPL.
24 #ifndef _BB_INTERNAL_H_
25 #define _BB_INTERNAL_H_ 1
39 #include <sys/param.h>
42 /* for the _syscall() macros */
43 #include <sys/syscall.h>
44 #include <linux/unistd.h>
47 /* Some useful definitions */
48 #define FALSE ((int) 1)
49 #define TRUE ((int) 0)
52 #define MTAB_GETMOUNTPT '1'
53 #define MTAB_GETDEVICE '2'
56 #define EXPAND_ALLOC 1024
59 #define isBlank(ch) (((ch) == ' ') || ((ch) == '\t'))
60 #define isDecimal(ch) (((ch) >= '0') && ((ch) <= '9'))
61 #define isOctal(ch) (((ch) >= '0') && ((ch) <= '7'))
62 #define isWildCard(ch) (((ch) == '*') || ((ch) == '?') || ((ch) == '['))
64 /* Macros for min/max. */
66 #define MIN(a,b) (((a)<(b))?(a):(b))
70 #define MAX(a,b) (((a)>(b))?(a):(b))
74 /* I don't like nested includes, but the string and io functions are used
78 #if !defined(NO_STRING_H) || defined(STDC_HEADERS)
80 # if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
83 # define memzero(s, n) memset ((void *)(s), 0, (n))
87 # define strrchr rindex
88 # define memcpy(d, s, n) bcopy((s), (d), (n))
89 # define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
90 # define memzero(s, n) bzero((s), (n))
104 int (*main)(int argc, char** argv);
105 enum Location location;
109 extern const struct BB_applet applets[];
111 /* Automagically pull in all the applet function prototypes and
112 * applet usage strings. These are all of the form:
113 * extern int foo_main(int argc, char **argv);
114 * extern const char foo_usage[];
115 * These are all autogenerated from the set of currently defined applets.
121 extern const char *applet_name;
122 extern int applet_name_compare(const void *x, const void *y);
124 extern void usage(const char *usage) __attribute__ ((noreturn));
125 extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
126 extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
127 extern void perrorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
128 extern void fatalPerror(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
130 const char *modeString(int mode);
131 const char *timeString(time_t timeVal);
132 int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
133 int isDevice(const char *name);
135 typedef struct ino_dev_hash_bucket_struct {
136 struct ino_dev_hash_bucket_struct *next;
140 } ino_dev_hashtable_bucket_t;
141 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
142 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
143 void reset_ino_dev_hashtable(void);
145 int copyFile(const char *srcName, const char *destName,
146 int setModes, int followLinks, int forceFlag);
147 int copySubFile(int srcFd, int dstFd, size_t remaining);
148 char *buildName(const char *dirName, const char *fileName);
149 int makeString(int argc, const char **argv, char *buf, int bufLen);
150 char *getChunk(int size);
151 char *chunkstrdup(const char *str);
152 void freeChunks(void);
153 int fullWrite(int fd, const char *buf, int len);
154 int fullRead(int fd, char *buf, int len);
155 int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
156 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
157 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
160 extern int createPath (const char *name, int mode);
161 extern int parse_mode( const char* s, mode_t* theMode);
163 extern int get_kernel_revision(void);
165 extern int get_console_fd(char* tty_name);
166 extern struct mntent *findMountPoint(const char *name, const char *table);
167 extern void write_mtab(char* blockDevice, char* directory,
168 char* filesystemType, long flags, char* string_flags);
169 extern void erase_mtab(const char * name);
170 extern void mtab_read(void);
171 extern char *mtab_first(void **iter);
172 extern char *mtab_next(void **iter);
173 extern char *mtab_getinfo(const char *match, const char which);
174 extern int check_wildcard_match(const char* text, const char* pattern);
175 extern long getNum (const char *cp);
176 extern pid_t* findPidByName( char* pidName);
177 extern int find_real_root_device_name(char* name);
178 extern char *get_line_from_file(FILE *file);
179 extern void print_file(FILE *file);
180 extern int print_file_by_name(char *filename);
181 extern char process_escape_sequence(char **ptr);
182 extern char *get_last_path_component(char *path);
183 extern void xregcomp(regex_t *preg, const char *regex, int cflags);
184 extern FILE *wfopen(const char *path, const char *mode);
185 extern FILE *xfopen(const char *path, const char *mode);
188 extern void *xmalloc (size_t size);
189 extern void *xrealloc(void *old, size_t size);
190 extern void *xcalloc(size_t nmemb, size_t size);
191 extern char *xstrdup (const char *s);
193 extern char *xstrndup (const char *s, int n);
196 /* These parse entries in /etc/passwd and /etc/group. This is desirable
197 * for BusyBox since we want to avoid using the glibc NSS stuff, which
198 * increases target size and is often not needed embedded systems. */
199 extern long my_getpwnam(char *name);
200 extern long my_getgrnam(char *name);
201 extern void my_getpwuid(char *name, long uid);
202 extern void my_getgrgid(char *group, long gid);
203 extern long my_getpwnamegid(char *name);
205 extern int device_open(char *device, int mode);
207 #if defined BB_FEATURE_MOUNT_LOOP
208 extern int del_loop(const char *device);
209 extern int set_loop(const char *device, const char *file, int offset, int *loopro);
210 extern char *find_unused_loop_device (void);
214 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
215 extern int vdprintf(int d, const char *format, va_list ap);
218 #if defined BB_NFSMOUNT
219 int nfsmount(const char *spec, const char *node, int *flags,
220 char **extra_opts, char **mount_opts, int running_bg);
224 /* Stop system and switch power off if possible. */
225 #define RB_POWER_OFF 0x4321fedc
228 /* Include our own copy of struct sysinfo to avoid binary compatability
229 * problems with Linux 2.4, which changed things. Grumble, grumble. */
231 long uptime; /* Seconds since boot */
232 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
233 unsigned long totalram; /* Total usable main memory size */
234 unsigned long freeram; /* Available memory size */
235 unsigned long sharedram; /* Amount of shared memory */
236 unsigned long bufferram; /* Memory used by buffers */
237 unsigned long totalswap; /* Total swap space size */
238 unsigned long freeswap; /* swap space still available */
239 unsigned short procs; /* Number of current processes */
240 unsigned long totalhigh; /* Total high memory size */
241 unsigned long freehigh; /* Available high memory size */
242 unsigned int mem_unit; /* Memory unit size in bytes */
243 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
245 extern int sysinfo (struct sysinfo* info);
247 /* Bit map related macros -- libc5 doens't provide these... sigh. */
249 #define NBBY CHAR_BIT
250 #define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
251 #define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
252 #define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
253 #define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
256 #endif /* _BB_INTERNAL_H_ */