blob: 7f4d55f3d585f41a45c1f0b1a5f7f8ed094d7caf [file] [log] [blame]
Eric Andersenf6b71392000-09-26 01:09:18 +00001/* 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#ifdef DMALLOC
30#include "dmalloc.h"
31#endif
32
33#include <stdlib.h>
34#include <stdarg.h>
35#include <string.h>
36#include <unistd.h>
37#include <errno.h>
38#include <sys/stat.h>
39#include <sys/param.h>
40#include <mntent.h>
41#include <regex.h>
42/* for the _syscall() macros */
43#include <sys/syscall.h>
44#include <linux/unistd.h>
45
46
47/* Some useful definitions */
48#define FALSE ((int) 1)
49#define TRUE ((int) 0)
50
51/* for mtab.c */
52#define MTAB_GETMOUNTPT '1'
53#define MTAB_GETDEVICE '2'
54
55#define BUF_SIZE 8192
56#define EXPAND_ALLOC 1024
57
58
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) == '['))
63
64/* Macros for min/max. */
65#ifndef MIN
66#define MIN(a,b) (((a)<(b))?(a):(b))
67#endif
68
69#ifndef MAX
70#define MAX(a,b) (((a)>(b))?(a):(b))
71#endif
72
73
74/* I don't like nested includes, but the string and io functions are used
75 * too often
76 */
77#include <stdio.h>
78#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
79# include <string.h>
80# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
81# include <memory.h>
82# endif
83# define memzero(s, n) memset ((void *)(s), 0, (n))
84#else
85# include <strings.h>
86# define strchr index
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))
91#endif
92
93
94enum Location {
95 _BB_DIR_ROOT = 0,
96 _BB_DIR_BIN,
97 _BB_DIR_SBIN,
98 _BB_DIR_USR_BIN,
99 _BB_DIR_USR_SBIN
100};
101
102struct BB_applet {
103 const char* name;
104 int (*main)(int argc, char** argv);
105 enum Location location;
106 const char* usage;
107};
108/* From busybox.c */
109extern const struct BB_applet applets[];
110
111extern int ar_main(int argc, char **argv);
112extern int basename_main(int argc, char **argv);
113extern int bogomips_main(int argc, char **argv);
114extern int busybox_main(int argc, char** argv);
115extern int cat_main(int argc, char** argv);
116extern int chmod_chown_chgrp_main(int argc, char** argv);
117extern int chroot_main(int argc, char** argv);
118extern int chvt_main(int argc, char** argv);
119extern int clear_main(int argc, char** argv);
Mark Whitley872138d2000-10-09 18:56:47 +0000120extern int cmp_main(int argc, char** argv);
Eric Andersenf6b71392000-09-26 01:09:18 +0000121extern int cp_mv_main(int argc, char** argv);
122extern int cut_main(int argc, char** argv);
123extern int date_main(int argc, char** argv);
124extern int dc_main(int argc, char** argv);
125extern int dd_main(int argc, char** argv);
126extern int dirname_main(int argc, char** argv);
127extern int deallocvt_main(int argc, char** argv);
128extern int df_main(int argc, char** argv);
129extern int dmesg_main(int argc, char** argv);
130extern int dos2unix_main(int argc, char** argv);
131extern int du_main(int argc, char** argv);
132extern int dumpkmap_main(int argc, char** argv);
133extern int dutmp_main(int argc, char** argv);
134extern int echo_main(int argc, char** argv);
135extern int expr_main(int argc, char** argv);
136extern int false_main(int argc, char** argv);
137extern int fbset_main(int argc, char** argv);
138extern int fdisk_main(int argc, char** argv);
139extern int fdflush_main(int argc, char **argv);
140extern int fsck_minix_main(int argc, char **argv);
141extern int find_main(int argc, char** argv);
142extern int free_main(int argc, char** argv);
143extern int freeramdisk_main(int argc, char** argv);
144extern int getopt_main(int argc, char** argv);
145extern int grep_main(int argc, char** argv);
146extern int gunzip_main (int argc, char** argv);
147extern int gzip_main(int argc, char** argv);
148extern int halt_main(int argc, char** argv);
149extern int head_main(int argc, char** argv);
150extern int hostid_main(int argc, char** argv);
151extern int hostname_main(int argc, char** argv);
152extern int id_main(int argc, char** argv);
153extern int init_main(int argc, char** argv);
154extern int insmod_main(int argc, char** argv);
155extern int kill_main(int argc, char** argv);
156extern int length_main(int argc, char** argv);
157extern int ln_main(int argc, char** argv);
158extern int loadacm_main(int argc, char** argv);
159extern int loadfont_main(int argc, char** argv);
160extern int loadkmap_main(int argc, char** argv);
161extern int losetup_main(int argc, char** argv);
162extern int logger_main(int argc, char **argv);
163extern int logname_main(int argc, char **argv);
164extern int ls_main(int argc, char** argv);
165extern int lsmod_main(int argc, char** argv);
166extern int makedevs_main(int argc, char** argv);
167extern int md5sum_main(int argc, char** argv);
168extern int mkdir_main(int argc, char** argv);
169extern int mkfifo_main(int argc, char **argv);
170extern int mkfs_minix_main(int argc, char **argv);
171extern int mknod_main(int argc, char** argv);
172extern int mkswap_main(int argc, char** argv);
173extern int mktemp_main(int argc, char **argv);
174extern int nc_main(int argc, char** argv);
175extern int more_main(int argc, char** argv);
176extern int mount_main(int argc, char** argv);
177extern int mt_main(int argc, char** argv);
178extern int nslookup_main(int argc, char **argv);
179extern int ping_main(int argc, char **argv);
180extern int poweroff_main(int argc, char **argv);
181extern int printf_main(int argc, char** argv);
182extern int ps_main(int argc, char** argv);
183extern int pwd_main(int argc, char** argv);
184extern int rdate_main(int argc, char** argv);
Mark Whitley872138d2000-10-09 18:56:47 +0000185extern int readlink_main(int argc, char** argv);
Eric Andersenf6b71392000-09-26 01:09:18 +0000186extern int reboot_main(int argc, char** argv);
187extern int renice_main(int argc, char** argv);
188extern int reset_main(int argc, char** argv);
189extern int rm_main(int argc, char** argv);
190extern int rmdir_main(int argc, char **argv);
191extern int rmmod_main(int argc, char** argv);
192extern int sed_main(int argc, char** argv);
193extern int sfdisk_main(int argc, char** argv);
194extern int setkeycodes_main(int argc, char** argv);
195extern int shell_main(int argc, char** argv);
196extern int sleep_main(int argc, char** argv);
197extern int sort_main(int argc, char** argv);
198extern int swap_on_off_main(int argc, char** argv);
199extern int sync_main(int argc, char** argv);
200extern int syslogd_main(int argc, char **argv);
201extern int tail_main(int argc, char** argv);
202extern int tar_main(int argc, char** argv);
203extern int tee_main(int argc, char** argv);
204extern int test_main(int argc, char** argv);
205extern int telnet_main(int argc, char** argv);
206extern int touch_main(int argc, char** argv);
207extern int tr_main(int argc, char** argv);
208extern int true_main(int argc, char** argv);
209extern int tput_main(int argc, char** argv);
210extern int tryopen_main(int argc, char** argv);
211extern int tty_main(int argc, char** argv);
212extern int umount_main(int argc, char** argv);
213extern int uname_main(int argc, char** argv);
214extern int uniq_main(int argc, char** argv);
215extern int unix2dos_main(int argc, char** argv);
216extern int unrpm_main(int argc, char** argv);
217extern int update_main(int argc, char** argv);
218extern int uptime_main(int argc, char** argv);
219extern int usleep_main(int argc, char** argv);
220extern int uuencode_main(int argc, char** argv);
221extern int uudecode_main(int argc, char** argv);
222extern int wc_main(int argc, char** argv);
223extern int wget_main(int argc, char** argv);
224extern int which_main(int argc, char** argv);
225extern int whoami_main(int argc, char** argv);
226extern int xargs_main(int argc, char** argv);
227extern int yes_main(int argc, char** argv);
228
229extern const char ar_usage[];
230extern const char basename_usage[];
231extern const char cat_usage[];
232extern const char chgrp_usage[];
233extern const char chmod_usage[];
234extern const char chown_usage[];
235extern const char chroot_usage[];
236extern const char chvt_usage[];
237extern const char clear_usage[];
Mark Whitley872138d2000-10-09 18:56:47 +0000238extern const char cmp_usage[];
Eric Andersenf6b71392000-09-26 01:09:18 +0000239extern const char cp_usage[];
240extern const char cut_usage[];
241extern const char date_usage[];
242extern const char dc_usage[];
243extern const char dd_usage[];
244extern const char deallocvt_usage[];
245extern const char df_usage[];
246extern const char dirname_usage[];
247extern const char dmesg_usage[];
248extern const char dos2unix_usage[];
249extern const char du_usage[];
250extern const char dumpkmap_usage[];
251extern const char dutmp_usage[];
252extern const char echo_usage[];
253extern const char expr_usage[];
254extern const char false_usage[];
255extern const char fdflush_usage[];
256extern const char find_usage[];
257extern const char free_usage[];
258extern const char freeramdisk_usage[];
259extern const char fsck_minix_usage[];
Mark Whitley60506182000-10-19 22:28:06 +0000260extern const char getopt_usage[];
Eric Andersenf6b71392000-09-26 01:09:18 +0000261extern const char grep_usage[];
262extern const char gunzip_usage[];
263extern const char gzip_usage[];
264extern const char halt_usage[];
265extern const char head_usage[];
266extern const char hostid_usage[];
267extern const char hostname_usage[];
268extern const char id_usage[];
269extern const char insmod_usage[];
270extern const char kill_usage[];
271extern const char killall_usage[];
272extern const char length_usage[];
273extern const char ln_usage[];
274extern const char loadacm_usage[];
275extern const char loadfont_usage[];
276extern const char loadkmap_usage[];
277extern const char logger_usage[];
278extern const char logname_usage[];
279extern const char ls_usage[];
280extern const char lsmod_usage[];
281extern const char makedevs_usage[];
282extern const char md5sum_usage[];
283extern const char mkdir_usage[];
284extern const char mkfifo_usage[];
285extern const char mkfs_minix_usage[];
286extern const char mknod_usage[];
287extern const char mkswap_usage[];
288extern const char mktemp_usage[];
289extern const char more_usage[];
290extern const char mount_usage[];
291extern const char mt_usage[];
292extern const char mv_usage[];
293extern const char nc_usage[];
294extern const char nslookup_usage[];
295extern const char ping_usage[];
296extern const char poweroff_usage[];
297extern const char printf_usage[];
298extern const char ps_usage[];
299extern const char pwd_usage[];
300extern const char rdate_usage[];
Mark Whitley872138d2000-10-09 18:56:47 +0000301extern const char readlink_usage[];
Eric Andersenf6b71392000-09-26 01:09:18 +0000302extern const char reboot_usage[];
303extern const char renice_usage[];
304extern const char reset_usage[];
305extern const char rm_usage[];
306extern const char rmdir_usage[];
307extern const char rmmod_usage[];
308extern const char sed_usage[];
309extern const char setkeycodes_usage[];
310extern const char shell_usage[];
311extern const char sleep_usage[];
312extern const char sort_usage[];
313extern const char swapoff_usage[];
314extern const char swapon_usage[];
315extern const char sync_usage[];
316extern const char syslogd_usage[];
317extern const char tail_usage[];
318extern const char tar_usage[];
319extern const char tee_usage[];
320extern const char telnet_usage[];
321extern const char test_usage[];
322extern const char touch_usage[];
323extern const char tr_usage[];
324extern const char true_usage[];
325extern const char tty_usage[];
326extern const char umount_usage[];
327extern const char uname_usage[];
328extern const char uniq_usage[];
329extern const char unix2dos_usage[];
330extern const char unrpm_usage[];
331extern const char update_usage[];
332extern const char uptime_usage[];
333extern const char usleep_usage[];
334extern const char uudecode_usage[];
335extern const char uuencode_usage[];
336extern const char wc_usage[];
337extern const char wget_usage[];
338extern const char which_usage[];
339extern const char whoami_usage[];
340extern const char xargs_usage[];
341extern const char yes_usage[];
342
343extern const char *applet_name;
344
345extern void usage(const char *usage) __attribute__ ((noreturn));
346extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
347extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
348
349const char *modeString(int mode);
350const char *timeString(time_t timeVal);
351int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
352int isDevice(const char *name);
353
354typedef struct ino_dev_hash_bucket_struct {
355 struct ino_dev_hash_bucket_struct *next;
356 ino_t ino;
357 dev_t dev;
358 char name[1];
359} ino_dev_hashtable_bucket_t;
360int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
361void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
362void reset_ino_dev_hashtable(void);
363
364int copyFile(const char *srcName, const char *destName,
365 int setModes, int followLinks, int forceFlag);
366int copySubFile(int srcFd, int dstFd, size_t remaining);
367char *buildName(const char *dirName, const char *fileName);
368int makeString(int argc, const char **argv, char *buf, int bufLen);
369char *getChunk(int size);
370char *chunkstrdup(const char *str);
371void freeChunks(void);
372int fullWrite(int fd, const char *buf, int len);
373int fullRead(int fd, char *buf, int len);
374int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
375 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
376 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
377 void* userData);
378
379extern int createPath (const char *name, int mode);
380extern int parse_mode( const char* s, mode_t* theMode);
381
382extern int get_kernel_revision(void);
383
384extern int get_console_fd(char* tty_name);
385extern struct mntent *findMountPoint(const char *name, const char *table);
386extern void write_mtab(char* blockDevice, char* directory,
387 char* filesystemType, long flags, char* string_flags);
388extern void erase_mtab(const char * name);
389extern void mtab_read(void);
390extern char *mtab_first(void **iter);
391extern char *mtab_next(void **iter);
392extern char *mtab_getinfo(const char *match, const char which);
393extern int check_wildcard_match(const char* text, const char* pattern);
394extern long getNum (const char *cp);
395extern pid_t* findPidByName( char* pidName);
396extern int find_real_root_device_name(char* name);
397extern char *get_line_from_file(FILE *file);
398extern void print_file(FILE *file);
399extern int print_file_by_name(char *filename);
400extern char process_escape_sequence(char **ptr);
401extern char *get_last_path_component(char *path);
402extern void xregcomp(regex_t *preg, const char *regex, int cflags);
Matt Kraaic0321f92000-09-27 04:09:22 +0000403extern FILE *wfopen(const char *path, const char *mode);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000404extern FILE *xfopen(const char *path, const char *mode);
Eric Andersenf6b71392000-09-26 01:09:18 +0000405
406#ifndef DMALLOC
407extern void *xmalloc (size_t size);
408extern void *xrealloc(void *old, size_t size);
409extern void *xcalloc(size_t nmemb, size_t size);
410extern char *xstrdup (const char *s);
411#endif
412extern char *xstrndup (const char *s, int n);
413
414
415/* These parse entries in /etc/passwd and /etc/group. This is desirable
416 * for BusyBox since we want to avoid using the glibc NSS stuff, which
417 * increases target size and is often not needed embedded systems. */
418extern long my_getpwnam(char *name);
419extern long my_getgrnam(char *name);
420extern void my_getpwuid(char *name, long uid);
421extern void my_getgrgid(char *group, long gid);
422extern long my_getpwnamegid(char *name);
423
424extern int device_open(char *device, int mode);
425
426#if defined BB_FEATURE_MOUNT_LOOP
427extern int del_loop(const char *device);
428extern int set_loop(const char *device, const char *file, int offset, int *loopro);
429extern char *find_unused_loop_device (void);
430#endif
431
432
433#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
434extern int vdprintf(int d, const char *format, va_list ap);
435#endif
436
437#if defined BB_NFSMOUNT
438int nfsmount(const char *spec, const char *node, int *flags,
439 char **extra_opts, char **mount_opts, int running_bg);
440#endif
441
442#ifndef RB_POWER_OFF
443/* Stop system and switch power off if possible. */
444#define RB_POWER_OFF 0x4321fedc
445#endif
446
447/* Include our own copy of struct sysinfo to avoid binary compatability
448 * problems with Linux 2.4, which changed things. Grumble, grumble. */
449struct sysinfo {
450 long uptime; /* Seconds since boot */
451 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
452 unsigned long totalram; /* Total usable main memory size */
453 unsigned long freeram; /* Available memory size */
454 unsigned long sharedram; /* Amount of shared memory */
455 unsigned long bufferram; /* Memory used by buffers */
456 unsigned long totalswap; /* Total swap space size */
457 unsigned long freeswap; /* swap space still available */
458 unsigned short procs; /* Number of current processes */
459 unsigned long totalhigh; /* Total high memory size */
460 unsigned long freehigh; /* Available high memory size */
461 unsigned int mem_unit; /* Memory unit size in bytes */
462 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
463};
464extern int sysinfo (struct sysinfo* info);
465
466/* Bit map related macros -- libc5 doens't provide these... sigh. */
467#ifndef setbit
468#define NBBY CHAR_BIT
469#define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
470#define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
471#define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
472#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
473#endif
474
475#endif /* _BB_INTERNAL_H_ */