blob: 859bee1c398d646fb6649448795d8996db382abb [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen2b69c401999-10-05 22:58:32 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Busybox main internal header file
Eric Andersen2b69c401999-10-05 22:58:32 +00004 *
Eric Andersen2b69c401999-10-05 22:58:32 +00005 *
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 */
Erik Andersenfac10d72000-02-07 05:29:42 +000024#ifndef _BB_INTERNAL_H_
25#define _BB_INTERNAL_H_ 1
Eric Andersencc8ed391999-10-05 16:24:54 +000026
27#include "busybox.def.h"
28
29#include <stdlib.h>
Erik Andersen5509af72000-01-23 18:19:02 +000030#include <stdarg.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000031#include <string.h>
32#include <unistd.h>
33#include <sys/stat.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +000034#include <sys/param.h>
Eric Andersen29d2e361999-11-06 06:07:27 +000035#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000036
37
38/* Some useful definitions */
Eric Andersenf811e071999-10-09 00:25:00 +000039#define FALSE ((int) 1)
40#define TRUE ((int) 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Erik Andersenfac10d72000-02-07 05:29:42 +000042/* for mtab.c */
43#define MTAB_GETMOUNTPT '1'
44#define MTAB_GETDEVICE '2'
45
Eric Andersencc8ed391999-10-05 16:24:54 +000046#define BUF_SIZE 8192
47#define EXPAND_ALLOC 1024
48
Erik Andersen298854f2000-03-23 01:09:18 +000049
Eric Andersencc8ed391999-10-05 16:24:54 +000050#define isBlank(ch) (((ch) == ' ') || ((ch) == '\t'))
51#define isDecimal(ch) (((ch) >= '0') && ((ch) <= '9'))
52#define isOctal(ch) (((ch) >= '0') && ((ch) <= '7'))
53#define isWildCard(ch) (((ch) == '*') || ((ch) == '?') || ((ch) == '['))
54
Erik Andersen4f3f7572000-04-28 00:18:56 +000055/* Macros for min/max. */
56#ifndef MIN
57#define MIN(a,b) (((a)<(b))?(a):(b))
58#endif
59
60#ifndef MAX
61#define MAX(a,b) (((a)>(b))?(a):(b))
62#endif
63
64
65/* I don't like nested includes, but the string and io functions are used
66 * too often
67 */
68#include <stdio.h>
69#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
70# include <string.h>
71# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
72# include <memory.h>
73# endif
74# define memzero(s, n) memset ((void *)(s), 0, (n))
75#else
76# include <strings.h>
77# define strchr index
78# define strrchr rindex
79# define memcpy(d, s, n) bcopy((s), (d), (n))
80# define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
81# define memzero(s, n) bzero((s), (n))
82#endif
83
84
Erik Andersend75af992000-03-16 08:09:09 +000085enum Location {
86 _BB_DIR_ROOT = 0,
87 _BB_DIR_BIN,
88 _BB_DIR_SBIN,
89 _BB_DIR_USR_BIN,
90 _BB_DIR_USR_SBIN
91};
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Eric Andersencc8ed391999-10-05 16:24:54 +000093struct Applet {
94 const char* name;
95 int (*main)(int argc, char** argv);
Erik Andersend75af992000-03-16 08:09:09 +000096 enum Location location;
Eric Andersencc8ed391999-10-05 16:24:54 +000097};
98
Erik Andersene2729152000-02-18 21:34:17 +000099extern int basename_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000100extern int busybox_main(int argc, char** argv);
101extern int block_device_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000102extern int cat_main(int argc, char** argv);
Erik Andersenfac10d72000-02-07 05:29:42 +0000103extern int cp_mv_main(int argc, char** argv);
Eric Andersen1b61f411999-10-13 18:56:42 +0000104extern int chmod_chown_chgrp_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105extern int chroot_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000106extern int chvt_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000107extern int clear_main(int argc, char** argv);
108extern int date_main(int argc, char** argv);
109extern int dd_main(int argc, char** argv);
Erik Andersenfb002d02000-03-05 08:07:00 +0000110extern int dirname_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000111extern int deallocvt_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000112extern int df_main(int argc, char** argv);
113extern int dmesg_main(int argc, char** argv);
John Beppu7a986061999-12-09 18:29:29 +0000114extern int du_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000115extern int dutmp_main(int argc, char** argv);
Erik Andersend75af992000-03-16 08:09:09 +0000116extern int echo_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000117extern int false_main(int argc, char** argv);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000118extern int fbset_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000119extern int fdisk_main(int argc, char** argv);
Eric Andersenf6be9441999-10-13 21:12:06 +0000120extern int fdflush_main(int argc, char **argv);
Eric Andersene494fdd1999-10-19 20:23:03 +0000121extern int fsck_minix_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000122extern int find_main(int argc, char** argv);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000123extern int free_main(int argc, char** argv);
Erik Andersen64371702000-03-04 22:23:27 +0000124extern int freeramdisk_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000125extern int grep_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000126extern int gunzip_main (int argc, char** argv);
127extern int gzip_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000128extern int halt_main(int argc, char** argv);
John Beppu3157b1f1999-12-10 07:42:50 +0000129extern int head_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000130extern int hostid_main(int argc, char** argv);
Eric Andersen485b9551999-12-07 23:14:59 +0000131extern int hostname_main(int argc, char** argv);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000132extern int id_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000133extern int init_main(int argc, char** argv);
Erik Andersen6da0ae81999-12-17 01:08:27 +0000134extern int insmod_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000135extern int kill_main(int argc, char** argv);
136extern int length_main(int argc, char** argv);
137extern int ln_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000138extern int loadacm_main(int argc, char** argv);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000139extern int loadfont_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000140extern int loadkmap_main(int argc, char** argv);
141extern int losetup_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000142extern int logger_main(int argc, char **argv);
143extern int logname_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000144extern int ls_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000145extern int lsmod_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000146extern int makedevs_main(int argc, char** argv);
147extern int math_main(int argc, char** argv);
Eric Andersenf6be9441999-10-13 21:12:06 +0000148extern int mkdir_main(int argc, char** argv);
Eric Andersen19db07b1999-12-11 08:41:28 +0000149extern int mkfifo_main(int argc, char **argv);
150extern int mkfs_minix_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000151extern int mknod_main(int argc, char** argv);
152extern int mkswap_main(int argc, char** argv);
Erik Andersen227a59b2000-04-25 23:24:55 +0000153extern int mktemp_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000154extern int mnc_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000155extern int more_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000156extern int mount_main(int argc, char** argv);
157extern int mt_main(int argc, char** argv);
Erik Andersenfac10d72000-02-07 05:29:42 +0000158extern int nslookup_main(int argc, char **argv);
Eric Andersen485b9551999-12-07 23:14:59 +0000159extern int ping_main(int argc, char **argv);
Eric Andersen2cb55071999-12-10 08:25:07 +0000160extern int poweroff_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000161extern int printf_main(int argc, char** argv);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000162extern int ps_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000163extern int pwd_main(int argc, char** argv);
164extern int reboot_main(int argc, char** argv);
165extern int rm_main(int argc, char** argv);
Erik Andersen3d7e3411999-12-16 23:04:20 +0000166extern int rmdir_main(int argc, char **argv);
167extern int rmmod_main(int argc, char** argv);
Eric Andersenc1525e81999-10-29 00:07:31 +0000168extern int sed_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000169extern int sfdisk_main(int argc, char** argv);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000170extern int setkeycodes_main(int argc, char** argv);
Erik Andersen3522eb12000-03-12 23:49:18 +0000171extern int shell_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000172extern int sleep_main(int argc, char** argv);
John Beppu019513a1999-12-22 17:57:31 +0000173extern int sort_main(int argc, char** argv);
Eric Andersen87590061999-10-18 21:22:59 +0000174extern int swap_on_off_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000175extern int sync_main(int argc, char** argv);
Eric Andersen3843e961999-11-25 07:30:46 +0000176extern int syslogd_main(int argc, char **argv);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000177extern int tail_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000178extern int tar_main(int argc, char** argv);
John Beppu059f1521999-12-10 05:27:16 +0000179extern int tee_main(int argc, char** argv);
Erik Andersend75af992000-03-16 08:09:09 +0000180extern int test_main(int argc, char** argv);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000181extern int telnet_main(int argc, char** argv);
Eric Andersen596e5461999-10-07 08:30:23 +0000182extern int touch_main(int argc, char** argv);
Erik Andersenfb002d02000-03-05 08:07:00 +0000183extern int tr_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000184extern int true_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000185extern int tput_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000186extern int tryopen_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000187extern int tty_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000188extern int umount_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000189extern int uname_main(int argc, char** argv);
Erik Andersen66be5e72000-03-05 07:52:05 +0000190extern int uptime_main(int argc, char** argv);
John Beppuabb47722000-01-06 00:48:21 +0000191extern int uniq_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000192extern int update_main(int argc, char** argv);
Erik Andersenb99ca132000-04-04 18:49:18 +0000193extern int usleep_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000194extern int wc_main(int argc, char** argv);
195extern int whoami_main(int argc, char** argv);
196extern int yes_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000197
Eric Andersen2b69c401999-10-05 22:58:32 +0000198
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000199extern void usage(const char *usage) __attribute__ ((noreturn));
Erik Andersen93d65132000-04-06 08:06:36 +0000200extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
201extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000202
Eric Andersen2b69c401999-10-05 22:58:32 +0000203const char *modeString(int mode);
204const char *timeString(time_t timeVal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000205int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000206int isDevice(const char *name);
Erik Andersen029011b2000-03-04 21:19:32 +0000207
208typedef struct ino_dev_hash_bucket_struct {
209 struct ino_dev_hash_bucket_struct *next;
210 ino_t ino;
211 dev_t dev;
212 char name[1];
213} ino_dev_hashtable_bucket_t;
214int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
215void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
216void reset_ino_dev_hashtable(void);
217
Eric Andersenf811e071999-10-09 00:25:00 +0000218int copyFile(const char *srcName, const char *destName, int setModes,
219 int followLinks);
220char *buildName(const char *dirName, const char *fileName);
221int makeString(int argc, const char **argv, char *buf, int bufLen);
Eric Andersen2b69c401999-10-05 22:58:32 +0000222char *getChunk(int size);
223char *chunkstrdup(const char *str);
224void freeChunks(void);
225int fullWrite(int fd, const char *buf, int len);
226int fullRead(int fd, char *buf, int len);
Eric Andersen63a0e531999-10-22 05:12:14 +0000227int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000228 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
229 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
230 void* userData);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000231const char* timeString(time_t timeVal);
Eric Andersencc8ed391999-10-05 16:24:54 +0000232
Erik Andersen06936df2000-01-23 02:14:20 +0000233extern int createPath (const char *name, int mode);
Eric Andersenf6be9441999-10-13 21:12:06 +0000234extern int parse_mode( const char* s, mode_t* theMode);
Eric Andersenb0e9a701999-10-18 22:28:26 +0000235
Erik Andersen1ad302a2000-03-24 00:54:46 +0000236extern int get_kernel_revision(void);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000237
Eric Andersen0460ff21999-10-25 23:32:44 +0000238extern int get_console_fd(char* tty_name);
Eric Andersen29d2e361999-11-06 06:07:27 +0000239extern struct mntent *findMountPoint(const char *name, const char *table);
Eric Andersend0246fb1999-11-04 21:18:07 +0000240extern void write_mtab(char* blockDevice, char* directory,
241 char* filesystemType, long flags, char* string_flags);
242extern void erase_mtab(const char * name);
Erik Andersenfac10d72000-02-07 05:29:42 +0000243extern void mtab_read(void);
Erik Andersenfac10d72000-02-07 05:29:42 +0000244extern char *mtab_first(void **iter);
245extern char *mtab_next(void **iter);
246extern char *mtab_getinfo(const char *match, const char which);
Eric Andersenb186d981999-12-03 09:19:54 +0000247extern int check_wildcard_match(const char* text, const char* pattern);
Eric Andersen1792f8c1999-12-09 06:11:36 +0000248extern long getNum (const char *cp);
Erik Andersen825aead2000-04-07 06:00:07 +0000249extern pid_t* findPidByName( char* pidName);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000250extern void *xmalloc (size_t size);
Erik Andersenec5bd902000-03-22 07:12:05 +0000251extern int find_real_root_device_name(char* name);
John Beppu5a728cf2000-04-17 04:22:09 +0000252extern char *cstring_lineFromFile(FILE *f);
253
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000254/* These parse entries in /etc/passwd and /etc/group. This is desirable
255 * for BusyBox since we want to avoid using the glibc NSS stuff, which
256 * increases target size and is often not needed embedded systems. */
257extern uid_t my_getpwnam(char *name);
258extern gid_t my_getgrnam(char *name);
259extern void my_getpwuid(char *name, uid_t uid);
260extern void my_getgrgid(char *group, gid_t gid);
261extern gid_t my_getpwnamegid(char *name);
262
Erik Andersenec5bd902000-03-22 07:12:05 +0000263
Erik Andersen31cf8e02000-02-07 20:31:19 +0000264#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersend0480c22000-02-07 20:35:14 +0000265extern int device_open(char *device, int mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +0000266#endif
Erik Andersen5cbdd712000-01-26 20:06:48 +0000267#if defined BB_FEATURE_MOUNT_LOOP
268extern int del_loop(const char *device);
Erik Andersende7965c2000-01-26 23:49:21 +0000269extern int set_loop(const char *device, const char *file, int offset, int *loopro);
270extern char *find_unused_loop_device (void);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000271#endif
272
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000273
Erik Andersen5509af72000-01-23 18:19:02 +0000274#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +0000275extern int vdprintf(int d, const char *format, va_list ap);
276#endif
Eric Andersene674eb71999-10-19 20:52:57 +0000277
Eric Andersena9c95ea1999-11-15 17:33:30 +0000278#if defined BB_NFSMOUNT
279int nfsmount(const char *spec, const char *node, unsigned long *flags,
280 char **extra_opts, char **mount_opts, int running_bg);
281#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000282
Eric Andersene674eb71999-10-19 20:52:57 +0000283#if defined (BB_FSCK_MINIX) || defined (BB_MKFS_MINIX)
Erik Andersen4f3f7572000-04-28 00:18:56 +0000284/* Bit map related macros. */
285#ifndef setbit
286#define CHAR_BITS 8 /* Number of bits in a `char'. */
287#define setbit(a,i) ((a)[(i)/CHAR_BITS] |= 1<<((i)%CHAR_BITS))
288#define clrbit(a,i) ((a)[(i)/CHAR_BITS] &= ~(1<<((i)%CHAR_BITS)))
289#define isset(a,i) ((a)[(i)/CHAR_BITS] & (1<<((i)%CHAR_BITS)))
290#define isclr(a,i) (((a)[(i)/CHAR_BITS] & (1<<((i)%CHAR_BITS))) == 0)
291#endif
292#endif
Eric Andersene674eb71999-10-19 20:52:57 +0000293
294
Erik Andersen31638212000-01-15 22:28:50 +0000295#ifndef RB_POWER_OFF
296/* Stop system and switch power off if possable. */
297#define RB_POWER_OFF 0x4321fedc
298#endif
299
Eric Andersenaa0765e1999-10-22 04:30:20 +0000300
Erik Andersenfac10d72000-02-07 05:29:42 +0000301#endif /* _BB_INTERNAL_H_ */