blob: e96eebba9bfd3b0494f6a3077086dd73aed8faef [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 Andersene132f4b2000-02-09 04:16:43 +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
49#define isBlank(ch) (((ch) == ' ') || ((ch) == '\t'))
50#define isDecimal(ch) (((ch) >= '0') && ((ch) <= '9'))
51#define isOctal(ch) (((ch) >= '0') && ((ch) <= '7'))
52#define isWildCard(ch) (((ch) == '*') || ((ch) == '?') || ((ch) == '['))
53
54
Eric Andersencc8ed391999-10-05 16:24:54 +000055struct Applet {
56 const char* name;
57 int (*main)(int argc, char** argv);
58};
59
Erik Andersene2729152000-02-18 21:34:17 +000060extern int basename_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000061extern int busybox_main(int argc, char** argv);
62extern int block_device_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +000063extern int cat_main(int argc, char** argv);
Erik Andersenfac10d72000-02-07 05:29:42 +000064extern int cp_mv_main(int argc, char** argv);
Eric Andersen1b61f411999-10-13 18:56:42 +000065extern int chmod_chown_chgrp_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000066extern int chroot_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +000067extern int chvt_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000068extern int clear_main(int argc, char** argv);
69extern int date_main(int argc, char** argv);
70extern int dd_main(int argc, char** argv);
Erik Andersenfb002d02000-03-05 08:07:00 +000071extern int dirname_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +000072extern int deallocvt_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000073extern int df_main(int argc, char** argv);
74extern int dmesg_main(int argc, char** argv);
John Beppu7a986061999-12-09 18:29:29 +000075extern int du_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +000076extern int dutmp_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000077extern int false_main(int argc, char** argv);
Erik Andersen1c5b2581999-12-16 20:59:36 +000078extern int fbset_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000079extern int fdisk_main(int argc, char** argv);
Eric Andersenf6be9441999-10-13 21:12:06 +000080extern int fdflush_main(int argc, char **argv);
Eric Andersene494fdd1999-10-19 20:23:03 +000081extern int fsck_minix_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000082extern int find_main(int argc, char** argv);
Eric Andersenabc0f4f1999-12-08 23:19:36 +000083extern int free_main(int argc, char** argv);
Erik Andersen64371702000-03-04 22:23:27 +000084extern int freeramdisk_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000085extern int grep_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +000086extern int gunzip_main (int argc, char** argv);
87extern int gzip_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000088extern int halt_main(int argc, char** argv);
John Beppu3157b1f1999-12-10 07:42:50 +000089extern int head_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +000090extern int hostid_main(int argc, char** argv);
Eric Andersen485b9551999-12-07 23:14:59 +000091extern int hostname_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000092extern int init_main(int argc, char** argv);
Erik Andersen6da0ae81999-12-17 01:08:27 +000093extern int insmod_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000094extern int kill_main(int argc, char** argv);
95extern int length_main(int argc, char** argv);
96extern int ln_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +000097extern int loadacm_main(int argc, char** argv);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000098extern int loadfont_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +000099extern int loadkmap_main(int argc, char** argv);
100extern int losetup_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000101extern int logger_main(int argc, char **argv);
102extern int logname_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000103extern int ls_main(int argc, char** argv);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000104extern int lsmod_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105extern int makedevs_main(int argc, char** argv);
106extern int math_main(int argc, char** argv);
Eric Andersenf6be9441999-10-13 21:12:06 +0000107extern int mkdir_main(int argc, char** argv);
Eric Andersen19db07b1999-12-11 08:41:28 +0000108extern int mkfifo_main(int argc, char **argv);
109extern int mkfs_minix_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000110extern int mknod_main(int argc, char** argv);
111extern int mkswap_main(int argc, char** argv);
112extern int mnc_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000113extern int more_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000114extern int mount_main(int argc, char** argv);
115extern int mt_main(int argc, char** argv);
Erik Andersenfac10d72000-02-07 05:29:42 +0000116extern int nslookup_main(int argc, char **argv);
Eric Andersen485b9551999-12-07 23:14:59 +0000117extern int ping_main(int argc, char **argv);
Eric Andersen2cb55071999-12-10 08:25:07 +0000118extern int poweroff_main(int argc, char **argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000119extern int printf_main(int argc, char** argv);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000120extern int ps_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000121extern int pwd_main(int argc, char** argv);
122extern int reboot_main(int argc, char** argv);
123extern int rm_main(int argc, char** argv);
Erik Andersen3d7e3411999-12-16 23:04:20 +0000124extern int rmdir_main(int argc, char **argv);
125extern int rmmod_main(int argc, char** argv);
Eric Andersenc1525e81999-10-29 00:07:31 +0000126extern int sed_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000127extern int sfdisk_main(int argc, char** argv);
Erik Andersen3522eb12000-03-12 23:49:18 +0000128extern int shell_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129extern int sleep_main(int argc, char** argv);
John Beppu019513a1999-12-22 17:57:31 +0000130extern int sort_main(int argc, char** argv);
Eric Andersen87590061999-10-18 21:22:59 +0000131extern int swap_on_off_main(int argc, char** argv);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000132extern int sync_main(int argc, char** argv);
Eric Andersen3843e961999-11-25 07:30:46 +0000133extern int syslogd_main(int argc, char **argv);
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000134extern int tail_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000135extern int tar_main(int argc, char** argv);
John Beppu059f1521999-12-10 05:27:16 +0000136extern int tee_main(int argc, char** argv);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000137extern int telnet_main(int argc, char** argv);
Eric Andersen596e5461999-10-07 08:30:23 +0000138extern int touch_main(int argc, char** argv);
Erik Andersenfb002d02000-03-05 08:07:00 +0000139extern int tr_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000140extern int true_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000141extern int tput_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000142extern int tryopen_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000143extern int tty_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000144extern int umount_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000145extern int uname_main(int argc, char** argv);
Erik Andersen66be5e72000-03-05 07:52:05 +0000146extern int uptime_main(int argc, char** argv);
John Beppuabb47722000-01-06 00:48:21 +0000147extern int uniq_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000148extern int update_main(int argc, char** argv);
Erik Andersen31638212000-01-15 22:28:50 +0000149extern int wc_main(int argc, char** argv);
150extern int whoami_main(int argc, char** argv);
151extern int yes_main(int argc, char** argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000152
Eric Andersen2b69c401999-10-05 22:58:32 +0000153
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000154extern void usage(const char *usage) __attribute__ ((noreturn));
155extern void errorMsg(char *s, ...);
156extern void fatalError(char *s, ...) __attribute__ ((noreturn));
157
Eric Andersen2b69c401999-10-05 22:58:32 +0000158const char *modeString(int mode);
159const char *timeString(time_t timeVal);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000160int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000161int isDevice(const char *name);
Erik Andersen029011b2000-03-04 21:19:32 +0000162
163typedef struct ino_dev_hash_bucket_struct {
164 struct ino_dev_hash_bucket_struct *next;
165 ino_t ino;
166 dev_t dev;
167 char name[1];
168} ino_dev_hashtable_bucket_t;
169int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
170void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
171void reset_ino_dev_hashtable(void);
172
Eric Andersenf811e071999-10-09 00:25:00 +0000173int copyFile(const char *srcName, const char *destName, int setModes,
174 int followLinks);
175char *buildName(const char *dirName, const char *fileName);
176int makeString(int argc, const char **argv, char *buf, int bufLen);
Eric Andersen2b69c401999-10-05 22:58:32 +0000177char *getChunk(int size);
178char *chunkstrdup(const char *str);
179void freeChunks(void);
180int fullWrite(int fd, const char *buf, int len);
181int fullRead(int fd, char *buf, int len);
Eric Andersen63a0e531999-10-22 05:12:14 +0000182int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersen9b587181999-10-17 05:43:39 +0000183 int (*fileAction) (const char *fileName, struct stat* statbuf),
184 int (*dirAction) (const char *fileName, struct stat* statbuf));
Eric Andersen17d49ef1999-10-06 20:25:32 +0000185const char* timeString(time_t timeVal);
Eric Andersencc8ed391999-10-05 16:24:54 +0000186
Erik Andersen06936df2000-01-23 02:14:20 +0000187extern int createPath (const char *name, int mode);
Eric Andersenf6be9441999-10-13 21:12:06 +0000188extern int parse_mode( const char* s, mode_t* theMode);
Eric Andersenb0e9a701999-10-18 22:28:26 +0000189
Eric Andersend23f9ba1999-10-20 19:18:15 +0000190extern uid_t my_getpwnam(char *name);
191extern gid_t my_getgrnam(char *name);
192extern void my_getpwuid(char* name, uid_t uid);
193extern void my_getgrgid(char* group, gid_t gid);
194extern int get_kernel_revision();
Eric Andersen0460ff21999-10-25 23:32:44 +0000195extern int get_console_fd(char* tty_name);
Eric Andersen29d2e361999-11-06 06:07:27 +0000196extern struct mntent *findMountPoint(const char *name, const char *table);
Eric Andersend0246fb1999-11-04 21:18:07 +0000197extern void write_mtab(char* blockDevice, char* directory,
198 char* filesystemType, long flags, char* string_flags);
199extern void erase_mtab(const char * name);
Erik Andersenfac10d72000-02-07 05:29:42 +0000200extern void mtab_read(void);
201extern void mtab_free(void);
202extern char *mtab_first(void **iter);
203extern char *mtab_next(void **iter);
204extern char *mtab_getinfo(const char *match, const char which);
Eric Andersenb186d981999-12-03 09:19:54 +0000205extern int check_wildcard_match(const char* text, const char* pattern);
Eric Andersen1792f8c1999-12-09 06:11:36 +0000206extern long getNum (const char *cp);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000207extern pid_t findPidByName( char* pidName);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000208extern void *xmalloc (size_t size);
Erik Andersen31cf8e02000-02-07 20:31:19 +0000209#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersend0480c22000-02-07 20:35:14 +0000210extern int device_open(char *device, int mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +0000211#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000212extern void whine_if_fstab_is_missing();
Eric Andersend0246fb1999-11-04 21:18:07 +0000213
Erik Andersen5cbdd712000-01-26 20:06:48 +0000214#if defined BB_FEATURE_MOUNT_LOOP
215extern int del_loop(const char *device);
Erik Andersende7965c2000-01-26 23:49:21 +0000216extern int set_loop(const char *device, const char *file, int offset, int *loopro);
217extern char *find_unused_loop_device (void);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000218#endif
219
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000220
Erik Andersen5509af72000-01-23 18:19:02 +0000221#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +0000222extern int vdprintf(int d, const char *format, va_list ap);
223#endif
Eric Andersene674eb71999-10-19 20:52:57 +0000224
Eric Andersena9c95ea1999-11-15 17:33:30 +0000225#if defined BB_NFSMOUNT
226int nfsmount(const char *spec, const char *node, unsigned long *flags,
227 char **extra_opts, char **mount_opts, int running_bg);
228#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000229
Eric Andersene674eb71999-10-19 20:52:57 +0000230#if defined (BB_FSCK_MINIX) || defined (BB_MKFS_MINIX)
231
232static inline int bit(char * addr,unsigned int nr)
233{
234 return (addr[nr >> 3] & (1<<(nr & 7))) != 0;
235}
236
237static inline int setbit(char * addr,unsigned int nr)
238{
239 int __res = bit(addr, nr);
240 addr[nr >> 3] |= (1<<(nr & 7));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 return __res != 0;
Eric Andersene674eb71999-10-19 20:52:57 +0000242}
243
244static inline int clrbit(char * addr,unsigned int nr)
245{
246 int __res = bit(addr, nr);
247 addr[nr >> 3] &= ~(1<<(nr & 7));
248 return __res != 0;
249}
250
Eric Andersenaa0765e1999-10-22 04:30:20 +0000251#endif /* inline bitops junk */
Eric Andersene674eb71999-10-19 20:52:57 +0000252
253
Erik Andersen31638212000-01-15 22:28:50 +0000254#ifndef RB_POWER_OFF
255/* Stop system and switch power off if possable. */
256#define RB_POWER_OFF 0x4321fedc
257#endif
258
Eric Andersenaa0765e1999-10-22 04:30:20 +0000259
Erik Andersenfac10d72000-02-07 05:29:42 +0000260#endif /* _BB_INTERNAL_H_ */