Patch from Vladimir N. Oleynik to save 128 bytes by moving error
[oweals/busybox.git] / include / busybox.h
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Busybox main internal header file
4  *
5  *
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.
10  *
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.
15  *
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
19  *
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.
22  *
23  */
24 #ifndef _BB_INTERNAL_H_
25 #define _BB_INTERNAL_H_    1
26
27 #include "Config.h"
28
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33
34 #ifdef DMALLOC
35 #include "dmalloc.h"
36 #endif
37
38 /* Some useful definitions */
39 #define FALSE   ((int) 0)
40 #define TRUE    ((int) 1)
41 #define SKIP    ((int) 2)
42
43 /* for mtab.c */
44 #define MTAB_GETMOUNTPT '1'
45 #define MTAB_GETDEVICE  '2'
46
47 #define BUF_SIZE        8192
48 #define EXPAND_ALLOC    1024
49
50 static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); }
51 static inline int is_octal(ch)   { return ((ch >= '0') && (ch <= '7')); }
52
53 /* Macros for min/max.  */
54 #ifndef MIN
55 #define MIN(a,b) (((a)<(b))?(a):(b))
56 #endif
57
58 #ifndef MAX
59 #define MAX(a,b) (((a)>(b))?(a):(b))
60 #endif
61
62
63 enum Location {
64         _BB_DIR_ROOT = 0,
65         _BB_DIR_BIN,
66         _BB_DIR_SBIN,
67         _BB_DIR_USR_BIN,
68         _BB_DIR_USR_SBIN
69 };
70
71 struct BB_applet {
72         const   char*   name;
73         int     (*main)(int argc, char** argv);
74         enum    Location        location;
75 };
76 /* From busybox.c */
77 extern const struct BB_applet applets[];
78
79 /* Automagically pull in all the applet function prototypes and
80  * applet usage strings.  These are all of the form:
81  *              extern int foo_main(int argc, char **argv);
82  *              extern const char foo_usage[];
83  * These are all autogenerated from the set of currently defined applets. 
84  */
85 #define PROTOTYPES
86 #include "applets.h"
87 #undef PROTOTYPES
88
89 extern const char *applet_name;
90
91 extern void show_usage(void) __attribute__ ((noreturn));
92 extern void error_msg(const char *s, ...);
93 extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
94 extern void perror_msg(const char *s, ...);
95 extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
96
97 const char *mode_string(int mode);
98 const char *time_string(time_t timeVal);
99 int is_directory(const char *name, const int followLinks, struct stat *statBuf);
100 int isDevice(const char *name);
101
102 typedef struct ino_dev_hash_bucket_struct {
103   struct ino_dev_hash_bucket_struct *next;
104   ino_t ino;
105   dev_t dev;
106   char name[1];
107 } ino_dev_hashtable_bucket_t;
108 int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
109 void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
110 void reset_ino_dev_hashtable(void);
111
112 int copy_file(const char *srcName, const char *destName,
113                  int setModes, int followLinks, int forceFlag);
114 int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
115 char *buildName(const char *dirName, const char *fileName);
116 int makeString(int argc, const char **argv, char *buf, int bufLen);
117 char *getChunk(int size);
118 char *chunkstrdup(const char *str);
119 void freeChunks(void);
120 ssize_t safe_read(int fd, void *buf, size_t count);
121 int full_write(int fd, const char *buf, int len);
122 int full_read(int fd, char *buf, int len);
123 int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
124           int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
125           int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
126           void* userData);
127
128 extern int create_path (const char *name, int mode);
129 extern int parse_mode( const char* s, mode_t* theMode);
130
131 extern int get_kernel_revision(void);
132
133 extern int get_console_fd(char* tty_name);
134 extern struct mntent *find_mount_point(const char *name, const char *table);
135 extern void write_mtab(char* blockDevice, char* directory, 
136         char* filesystemType, long flags, char* string_flags);
137 extern void erase_mtab(const char * name);
138 extern void mtab_read(void);
139 extern char *mtab_first(void **iter);
140 extern char *mtab_next(void **iter);
141 extern char *mtab_getinfo(const char *match, const char which);
142 extern int check_wildcard_match(const char* text, const char* pattern);
143 extern long atoi_w_units (const char *cp);
144 extern pid_t* find_pid_by_name( char* pidName);
145 extern int find_real_root_device_name(char* name);
146 extern char *get_line_from_file(FILE *file);
147 extern void print_file(FILE *file);
148 extern int print_file_by_name(char *filename);
149 extern char process_escape_sequence(char **ptr);
150 extern char *get_last_path_component(char *path);
151 extern FILE *wfopen(const char *path, const char *mode);
152 extern FILE *xfopen(const char *path, const char *mode);
153 extern void chomp(char *s);
154 extern struct BB_applet *find_applet_by_name(const char *name);
155 void run_applet_by_name(const char *name, int argc, char **argv);
156
157 #ifndef DMALLOC
158 extern void *xmalloc (size_t size);
159 extern void *xrealloc(void *old, size_t size);
160 extern void *xcalloc(size_t nmemb, size_t size);
161 extern char *xstrdup (const char *s);
162 #endif
163 extern char *xstrndup (const char *s, int n);
164 extern char * safe_strncpy(char *dst, const char *src, size_t size);
165
166 struct suffix_mult {
167         char *suffix;
168         int mult;
169 };
170
171 extern unsigned long parse_number(const char *numstr,
172                 const struct suffix_mult *suffixes);
173
174
175 /* These parse entries in /etc/passwd and /etc/group.  This is desirable
176  * for BusyBox since we want to avoid using the glibc NSS stuff, which
177  * increases target size and is often not needed embedded systems.  */
178 extern long my_getpwnam(const char *name);
179 extern long my_getgrnam(const char *name);
180 extern void my_getpwuid(char *name, long uid);
181 extern void my_getgrgid(char *group, long gid);
182 extern long my_getpwnamegid(const char *name);
183
184 extern int device_open(char *device, int mode);
185
186 #if defined BB_FEATURE_MOUNT_LOOP
187 extern int del_loop(const char *device);
188 extern int set_loop(const char *device, const char *file, int offset, int *loopro);
189 extern char *find_unused_loop_device (void);
190 #endif
191
192
193 #if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
194 extern int vdprintf(int d, const char *format, va_list ap);
195 #endif
196
197 #if defined BB_NFSMOUNT
198 int nfsmount(const char *spec, const char *node, int *flags,
199              char **extra_opts, char **mount_opts, int running_bg);
200 #endif
201
202 #ifndef RB_POWER_OFF
203 /* Stop system and switch power off if possible.  */
204 #define RB_POWER_OFF   0x4321fedc
205 #endif
206
207 /* Include our own copy of struct sysinfo to avoid binary compatability
208  * problems with Linux 2.4, which changed things.  Grumble, grumble. */
209 struct sysinfo {
210         long uptime;                    /* Seconds since boot */
211         unsigned long loads[3];         /* 1, 5, and 15 minute load averages */
212         unsigned long totalram;         /* Total usable main memory size */
213         unsigned long freeram;          /* Available memory size */
214         unsigned long sharedram;        /* Amount of shared memory */
215         unsigned long bufferram;        /* Memory used by buffers */
216         unsigned long totalswap;        /* Total swap space size */
217         unsigned long freeswap;         /* swap space still available */
218         unsigned short procs;           /* Number of current processes */
219         unsigned long totalhigh;        /* Total high memory size */
220         unsigned long freehigh;         /* Available high memory size */
221         unsigned int mem_unit;          /* Memory unit size in bytes */
222         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
223 };
224 extern int sysinfo (struct sysinfo* info);
225
226 /* Bit map related macros -- libc5 doens't provide these... sigh.  */
227 #ifndef setbit
228 #define NBBY            CHAR_BIT
229 #define setbit(a,i)     ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
230 #define clrbit(a,i)     ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
231 #define isset(a,i)      ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
232 #define isclr(a,i)      (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
233 #endif
234
235 #ifdef BB_FEATURE_HUMAN_READABLE
236 const char *format(unsigned long val, unsigned long hr);
237 #define KILOBYTE 1024
238 #define MEGABYTE (KILOBYTE*1024)
239 #define GIGABYTE (MEGABYTE*1024)
240 #endif
241
242 #ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
243 #define RESERVE_BB_BUFFER(buffer,len)           char buffer[len]
244 #define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
245 #else
246 #define RESERVE_BB_BUFFER(buffer,len)           char *buffer=xmalloc(len)
247 #define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
248 #endif
249
250 #endif /* _BB_INTERNAL_H_ */