blob: 4a132abf17c73ceab7b46d5aa7da84f8ea712ff7 [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 Andersenbe5023a2000-06-19 17:30:07 +000036#if ! defined(__GLIBC__)
Eric Andersenb6106152000-06-19 17:25:40 +000037#include <asm/string.h>
Eric Andersenbe5023a2000-06-19 17:30:07 +000038#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000039
40
41/* Some useful definitions */
Eric Andersenf811e071999-10-09 00:25:00 +000042#define FALSE ((int) 1)
43#define TRUE ((int) 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000044
Erik Andersenfac10d72000-02-07 05:29:42 +000045/* for mtab.c */
46#define MTAB_GETMOUNTPT '1'
47#define MTAB_GETDEVICE '2'
48
Eric Andersencc8ed391999-10-05 16:24:54 +000049#define BUF_SIZE 8192
50#define EXPAND_ALLOC 1024
51
Erik Andersen298854f2000-03-23 01:09:18 +000052
Eric Andersencc8ed391999-10-05 16:24:54 +000053#define isBlank(ch) (((ch) == ' ') || ((ch) == '\t'))
54#define isDecimal(ch) (((ch) >= '0') && ((ch) <= '9'))
55#define isOctal(ch) (((ch) >= '0') && ((ch) <= '7'))
56#define isWildCard(ch) (((ch) == '*') || ((ch) == '?') || ((ch) == '['))
57
Erik Andersen4f3f7572000-04-28 00:18:56 +000058/* Macros for min/max. */
59#ifndef MIN
60#define MIN(a,b) (((a)<(b))?(a):(b))
61#endif
62
63#ifndef MAX
64#define MAX(a,b) (((a)>(b))?(a):(b))
65#endif
66
67
68/* I don't like nested includes, but the string and io functions are used
69 * too often
70 */
71#include <stdio.h>
72#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
73# include <string.h>
74# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
75# include <memory.h>
76# endif
77# define memzero(s, n) memset ((void *)(s), 0, (n))
78#else
79# include <strings.h>
80# define strchr index
81# define strrchr rindex
82# define memcpy(d, s, n) bcopy((s), (d), (n))
83# define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
84# define memzero(s, n) bzero((s), (n))
85#endif
86
87
Erik Andersend75af992000-03-16 08:09:09 +000088enum Location {
89 _BB_DIR_ROOT = 0,
90 _BB_DIR_BIN,
91 _BB_DIR_SBIN,
92 _BB_DIR_USR_BIN,
93 _BB_DIR_USR_SBIN
94};
Eric Andersencc8ed391999-10-05 16:24:54 +000095
Erik Andersenbcd61772000-05-13 06:33:19 +000096struct BB_applet {
Eric Andersencc8ed391999-10-05 16:24:54 +000097 const char* name;
98 int (*main)(int argc, char** argv);
Erik Andersend75af992000-03-16 08:09:09 +000099 enum Location location;
Eric Andersencc8ed391999-10-05 16:24:54 +0000100};
Erik Andersenbcd61772000-05-13 06:33:19 +0000101/* From busybox.c */
102extern const struct BB_applet applets[];
Eric Andersencc8ed391999-10-05 16:24:54 +0000103
Eric Andersen86ab8a32000-06-02 03:21:42 +0000104extern int ar_main(int argc, char **argv);
Erik Andersene2729152000-02-18 21:34:17 +0000105extern int basename_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000106extern int busybox_main(int argc, char** argv);
107extern int block_device_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000108extern int cat_main(int argc, char** argv);
Eric Andersen1b61f411999-10-13 18:56:42 +0000109extern int chmod_chown_chgrp_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000110extern int chroot_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000111extern int chvt_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000112extern int clear_main(int argc, char** argv);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000113extern int cp_mv_main(int argc, char** argv);
114extern int cut_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000115extern int date_main(int argc, char** argv);
116extern int dd_main(int argc, char** argv);
Erik Andersenfb002d02000-03-05 08:07:00 +0000117extern int dirname_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000118extern int deallocvt_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000119extern int df_main(int argc, char** argv);
120extern int dmesg_main(int argc, char** argv);
John Beppu7a986061999-12-09 18:29:29 +0000121extern int du_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000122extern int dutmp_main(int argc, char** argv);
Erik Andersend75af992000-03-16 08:09:09 +0000123extern int echo_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000124extern int false_main(int argc, char** argv);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000125extern int fbset_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000126extern int fdisk_main(int argc, char** argv);
Eric Andersenf6be9441999-10-13 21:12:06 +0000127extern int fdflush_main(int argc, char **argv);
Eric Andersene494fdd1999-10-19 20:23:03 +0000128extern int fsck_minix_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129extern int find_main(int argc, char** argv);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000130extern int free_main(int argc, char** argv);
Erik Andersen64371702000-03-04 22:23:27 +0000131extern int freeramdisk_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000132extern int grep_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000133extern int gunzip_main (int argc, char** argv);
134extern int gzip_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000135extern int halt_main(int argc, char** argv);
John Beppu3157b1f1999-12-10 07:42:50 +0000136extern int head_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000137extern int hostid_main(int argc, char** argv);
Eric Andersen485b9551999-12-07 23:14:59 +0000138extern int hostname_main(int argc, char** argv);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000139extern int id_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000140extern int init_main(int argc, char** argv);
Erik Andersen6da0ae81999-12-17 01:08:27 +0000141extern int insmod_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000142extern int kill_main(int argc, char** argv);
143extern int length_main(int argc, char** argv);
144extern int ln_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000145extern int loadacm_main(int argc, char** argv);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000146extern int loadfont_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000147extern int loadkmap_main(int argc, char** argv);
148extern int losetup_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000149extern int logger_main(int argc, char **argv);
150extern int logname_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000151extern int ls_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000152extern int lsmod_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000153extern int makedevs_main(int argc, char** argv);
154extern int math_main(int argc, char** argv);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000155extern int md5sum_main(int argc, char** argv);
Eric Andersenf6be9441999-10-13 21:12:06 +0000156extern int mkdir_main(int argc, char** argv);
Eric Andersen19db07b1999-12-11 08:41:28 +0000157extern int mkfifo_main(int argc, char **argv);
158extern int mkfs_minix_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000159extern int mknod_main(int argc, char** argv);
160extern int mkswap_main(int argc, char** argv);
Erik Andersen227a59b2000-04-25 23:24:55 +0000161extern int mktemp_main(int argc, char **argv);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000162extern int nc_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000163extern int more_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000164extern int mount_main(int argc, char** argv);
165extern int mt_main(int argc, char** argv);
Erik Andersenfac10d72000-02-07 05:29:42 +0000166extern int nslookup_main(int argc, char **argv);
Eric Andersen485b9551999-12-07 23:14:59 +0000167extern int ping_main(int argc, char **argv);
Eric Andersen2cb55071999-12-10 08:25:07 +0000168extern int poweroff_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169extern int printf_main(int argc, char** argv);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000170extern int ps_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000171extern int pwd_main(int argc, char** argv);
172extern int reboot_main(int argc, char** argv);
173extern int rm_main(int argc, char** argv);
Erik Andersen3d7e3411999-12-16 23:04:20 +0000174extern int rmdir_main(int argc, char **argv);
175extern int rmmod_main(int argc, char** argv);
Eric Andersenc1525e81999-10-29 00:07:31 +0000176extern int sed_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000177extern int sfdisk_main(int argc, char** argv);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000178extern int setkeycodes_main(int argc, char** argv);
Erik Andersen3522eb12000-03-12 23:49:18 +0000179extern int shell_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000180extern int sleep_main(int argc, char** argv);
John Beppu019513a1999-12-22 17:57:31 +0000181extern int sort_main(int argc, char** argv);
Eric Andersen87590061999-10-18 21:22:59 +0000182extern int swap_on_off_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000183extern int sync_main(int argc, char** argv);
Eric Andersen3843e961999-11-25 07:30:46 +0000184extern int syslogd_main(int argc, char **argv);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000185extern int tail_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000186extern int tar_main(int argc, char** argv);
John Beppu059f1521999-12-10 05:27:16 +0000187extern int tee_main(int argc, char** argv);
Erik Andersend75af992000-03-16 08:09:09 +0000188extern int test_main(int argc, char** argv);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000189extern int telnet_main(int argc, char** argv);
Eric Andersen596e5461999-10-07 08:30:23 +0000190extern int touch_main(int argc, char** argv);
Erik Andersenfb002d02000-03-05 08:07:00 +0000191extern int tr_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000192extern int true_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000193extern int tput_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000194extern int tryopen_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000195extern int tty_main(int argc, char** argv);
Eric Andersen2b6ab3c2000-06-13 06:54:53 +0000196extern int uuencode_main(int argc, char** argv);
197extern int uudecode_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000198extern int umount_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000199extern int uname_main(int argc, char** argv);
Erik Andersen66be5e72000-03-05 07:52:05 +0000200extern int uptime_main(int argc, char** argv);
John Beppuabb47722000-01-06 00:48:21 +0000201extern int uniq_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000202extern int update_main(int argc, char** argv);
Erik Andersenb99ca132000-04-04 18:49:18 +0000203extern int usleep_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000204extern int wc_main(int argc, char** argv);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000205extern int which_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000206extern int whoami_main(int argc, char** argv);
207extern int yes_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000208
Eric Andersen2b69c401999-10-05 22:58:32 +0000209
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000210extern void usage(const char *usage) __attribute__ ((noreturn));
Erik Andersen93d65132000-04-06 08:06:36 +0000211extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
212extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000213
Eric Andersen2b69c401999-10-05 22:58:32 +0000214const char *modeString(int mode);
215const char *timeString(time_t timeVal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000216int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000217int isDevice(const char *name);
Erik Andersen029011b2000-03-04 21:19:32 +0000218
219typedef struct ino_dev_hash_bucket_struct {
220 struct ino_dev_hash_bucket_struct *next;
221 ino_t ino;
222 dev_t dev;
223 char name[1];
224} ino_dev_hashtable_bucket_t;
225int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
226void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
227void reset_ino_dev_hashtable(void);
228
Erik Andersen59b9e872000-05-10 05:05:45 +0000229int copyFile(const char *srcName, const char *destName,
230 int setModes, int followLinks, int forceFlag);
Eric Andersenf811e071999-10-09 00:25:00 +0000231char *buildName(const char *dirName, const char *fileName);
232int makeString(int argc, const char **argv, char *buf, int bufLen);
Eric Andersen2b69c401999-10-05 22:58:32 +0000233char *getChunk(int size);
234char *chunkstrdup(const char *str);
235void freeChunks(void);
236int fullWrite(int fd, const char *buf, int len);
237int fullRead(int fd, char *buf, int len);
Eric Andersen63a0e531999-10-22 05:12:14 +0000238int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000239 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
240 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
241 void* userData);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000242const char* timeString(time_t timeVal);
Eric Andersencc8ed391999-10-05 16:24:54 +0000243
Erik Andersen06936df2000-01-23 02:14:20 +0000244extern int createPath (const char *name, int mode);
Eric Andersenf6be9441999-10-13 21:12:06 +0000245extern int parse_mode( const char* s, mode_t* theMode);
Eric Andersenb0e9a701999-10-18 22:28:26 +0000246
Erik Andersen1ad302a2000-03-24 00:54:46 +0000247extern int get_kernel_revision(void);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000248
Eric Andersen0460ff21999-10-25 23:32:44 +0000249extern int get_console_fd(char* tty_name);
Eric Andersen29d2e361999-11-06 06:07:27 +0000250extern struct mntent *findMountPoint(const char *name, const char *table);
Eric Andersend0246fb1999-11-04 21:18:07 +0000251extern void write_mtab(char* blockDevice, char* directory,
252 char* filesystemType, long flags, char* string_flags);
253extern void erase_mtab(const char * name);
Erik Andersenfac10d72000-02-07 05:29:42 +0000254extern void mtab_read(void);
Erik Andersenfac10d72000-02-07 05:29:42 +0000255extern char *mtab_first(void **iter);
256extern char *mtab_next(void **iter);
257extern char *mtab_getinfo(const char *match, const char which);
Eric Andersenb186d981999-12-03 09:19:54 +0000258extern int check_wildcard_match(const char* text, const char* pattern);
Eric Andersen1792f8c1999-12-09 06:11:36 +0000259extern long getNum (const char *cp);
Erik Andersen825aead2000-04-07 06:00:07 +0000260extern pid_t* findPidByName( char* pidName);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000261extern void *xmalloc (size_t size);
Erik Andersenec5bd902000-03-22 07:12:05 +0000262extern int find_real_root_device_name(char* name);
John Beppu5a728cf2000-04-17 04:22:09 +0000263extern char *cstring_lineFromFile(FILE *f);
264
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000265/* These parse entries in /etc/passwd and /etc/group. This is desirable
266 * for BusyBox since we want to avoid using the glibc NSS stuff, which
267 * increases target size and is often not needed embedded systems. */
Erik Andersen5b46d792000-05-17 05:24:24 +0000268extern unsigned long my_getpwnam(char *name);
269extern unsigned long my_getgrnam(char *name);
270extern void my_getpwuid(char *name, unsigned long uid);
271extern void my_getgrgid(char *group, unsigned long gid);
272extern unsigned long my_getpwnamegid(char *name);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000273
Erik Andersenec5bd902000-03-22 07:12:05 +0000274
Erik Andersen31cf8e02000-02-07 20:31:19 +0000275#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersend0480c22000-02-07 20:35:14 +0000276extern int device_open(char *device, int mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +0000277#endif
Erik Andersen5cbdd712000-01-26 20:06:48 +0000278#if defined BB_FEATURE_MOUNT_LOOP
279extern int del_loop(const char *device);
Erik Andersende7965c2000-01-26 23:49:21 +0000280extern int set_loop(const char *device, const char *file, int offset, int *loopro);
281extern char *find_unused_loop_device (void);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000282#endif
283
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000284
Erik Andersen5509af72000-01-23 18:19:02 +0000285#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +0000286extern int vdprintf(int d, const char *format, va_list ap);
287#endif
Eric Andersene674eb71999-10-19 20:52:57 +0000288
Eric Andersena9c95ea1999-11-15 17:33:30 +0000289#if defined BB_NFSMOUNT
290int nfsmount(const char *spec, const char *node, unsigned long *flags,
291 char **extra_opts, char **mount_opts, int running_bg);
292#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000293
Erik Andersen31638212000-01-15 22:28:50 +0000294#ifndef RB_POWER_OFF
Eric Andersen74c66ad2000-06-16 19:57:44 +0000295/* Stop system and switch power off if possible. */
Erik Andersen31638212000-01-15 22:28:50 +0000296#define RB_POWER_OFF 0x4321fedc
297#endif
298
Eric Andersenaa0765e1999-10-22 04:30:20 +0000299
Eric Andersenbd4a75f2000-06-19 20:03:48 +0000300#ifndef setbit
301/* Bit map related macros -- libc5 doens't provide these... sigh. */
302#define NBBY CHAR_BIT
303#define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
304#define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
305#define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
306#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
307#endif
308
Erik Andersenfac10d72000-02-07 05:29:42 +0000309#endif /* _BB_INTERNAL_H_ */