blob: 63f39590fa3bd36b85441ac745790f94349c520f [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 */
Eric Andersen2187adc2000-12-04 20:31:45 +000048#define FALSE ((int) 0)
49#define TRUE ((int) 1)
Eric Andersenf6b71392000-09-26 01:09:18 +000050
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
Mark Whitleyab147f62000-12-05 20:42:48 +000058static inline int isDecimal(ch) { return ((ch >= '0') && (ch <= '9')); }
59static inline int isOctal(ch) { return ((ch >= '0') && (ch <= '7')); }
Eric Andersenf6b71392000-09-26 01:09:18 +000060
61/* Macros for min/max. */
62#ifndef MIN
63#define MIN(a,b) (((a)<(b))?(a):(b))
64#endif
65
66#ifndef MAX
67#define MAX(a,b) (((a)>(b))?(a):(b))
68#endif
69
70
71/* I don't like nested includes, but the string and io functions are used
72 * too often
73 */
74#include <stdio.h>
75#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
76# include <string.h>
77# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
78# include <memory.h>
79# endif
80# define memzero(s, n) memset ((void *)(s), 0, (n))
81#else
82# include <strings.h>
83# define strchr index
84# define strrchr rindex
85# define memcpy(d, s, n) bcopy((s), (d), (n))
86# define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
87# define memzero(s, n) bzero((s), (n))
88#endif
89
90
91enum Location {
92 _BB_DIR_ROOT = 0,
93 _BB_DIR_BIN,
94 _BB_DIR_SBIN,
95 _BB_DIR_USR_BIN,
96 _BB_DIR_USR_SBIN
97};
98
99struct BB_applet {
100 const char* name;
101 int (*main)(int argc, char** argv);
102 enum Location location;
103 const char* usage;
104};
105/* From busybox.c */
106extern const struct BB_applet applets[];
107
Eric Andersen8c725e62000-11-30 00:27:06 +0000108/* Automagically pull in all the applet function prototypes and
Eric Andersen87559822000-12-01 19:02:24 +0000109 * applet usage strings. These are all of the form:
Eric Andersen8c725e62000-11-30 00:27:06 +0000110 * extern int foo_main(int argc, char **argv);
111 * extern const char foo_usage[];
112 * These are all autogenerated from the set of currently defined applets.
113 */
114#define PROTOTYPES
115#include "applets.h"
116#undef PROTOTYPES
Eric Andersenf6b71392000-09-26 01:09:18 +0000117
118extern const char *applet_name;
Eric Andersen8c725e62000-11-30 00:27:06 +0000119extern int applet_name_compare(const void *x, const void *y);
Eric Andersenf6b71392000-09-26 01:09:18 +0000120
121extern void usage(const char *usage) __attribute__ ((noreturn));
122extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
123extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Matt Kraaief5529b2000-10-25 17:00:36 +0000124extern void perrorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
Matt Kraai324a7782000-10-25 15:10:08 +0000125extern void fatalPerror(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Eric Andersenf6b71392000-09-26 01:09:18 +0000126
127const char *modeString(int mode);
128const char *timeString(time_t timeVal);
129int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
130int isDevice(const char *name);
131
132typedef struct ino_dev_hash_bucket_struct {
133 struct ino_dev_hash_bucket_struct *next;
134 ino_t ino;
135 dev_t dev;
136 char name[1];
137} ino_dev_hashtable_bucket_t;
138int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
139void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
140void reset_ino_dev_hashtable(void);
141
142int copyFile(const char *srcName, const char *destName,
143 int setModes, int followLinks, int forceFlag);
Mark Whitley47583682000-12-05 20:03:17 +0000144int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
Eric Andersenf6b71392000-09-26 01:09:18 +0000145char *buildName(const char *dirName, const char *fileName);
146int makeString(int argc, const char **argv, char *buf, int bufLen);
147char *getChunk(int size);
148char *chunkstrdup(const char *str);
149void freeChunks(void);
150int fullWrite(int fd, const char *buf, int len);
151int fullRead(int fd, char *buf, int len);
152int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
153 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
154 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
155 void* userData);
156
157extern int createPath (const char *name, int mode);
158extern int parse_mode( const char* s, mode_t* theMode);
159
160extern int get_kernel_revision(void);
161
162extern int get_console_fd(char* tty_name);
163extern struct mntent *findMountPoint(const char *name, const char *table);
164extern void write_mtab(char* blockDevice, char* directory,
165 char* filesystemType, long flags, char* string_flags);
166extern void erase_mtab(const char * name);
167extern void mtab_read(void);
168extern char *mtab_first(void **iter);
169extern char *mtab_next(void **iter);
170extern char *mtab_getinfo(const char *match, const char which);
171extern int check_wildcard_match(const char* text, const char* pattern);
172extern long getNum (const char *cp);
173extern pid_t* findPidByName( char* pidName);
174extern int find_real_root_device_name(char* name);
175extern char *get_line_from_file(FILE *file);
176extern void print_file(FILE *file);
177extern int print_file_by_name(char *filename);
178extern char process_escape_sequence(char **ptr);
179extern char *get_last_path_component(char *path);
180extern void xregcomp(regex_t *preg, const char *regex, int cflags);
Matt Kraaic0321f92000-09-27 04:09:22 +0000181extern FILE *wfopen(const char *path, const char *mode);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000182extern FILE *xfopen(const char *path, const char *mode);
Eric Andersenf6b71392000-09-26 01:09:18 +0000183
184#ifndef DMALLOC
185extern void *xmalloc (size_t size);
186extern void *xrealloc(void *old, size_t size);
187extern void *xcalloc(size_t nmemb, size_t size);
188extern char *xstrdup (const char *s);
189#endif
190extern char *xstrndup (const char *s, int n);
191
192
193/* These parse entries in /etc/passwd and /etc/group. This is desirable
194 * for BusyBox since we want to avoid using the glibc NSS stuff, which
195 * increases target size and is often not needed embedded systems. */
196extern long my_getpwnam(char *name);
197extern long my_getgrnam(char *name);
198extern void my_getpwuid(char *name, long uid);
199extern void my_getgrgid(char *group, long gid);
200extern long my_getpwnamegid(char *name);
201
202extern int device_open(char *device, int mode);
203
204#if defined BB_FEATURE_MOUNT_LOOP
205extern int del_loop(const char *device);
206extern int set_loop(const char *device, const char *file, int offset, int *loopro);
207extern char *find_unused_loop_device (void);
208#endif
209
210
211#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
212extern int vdprintf(int d, const char *format, va_list ap);
213#endif
214
215#if defined BB_NFSMOUNT
216int nfsmount(const char *spec, const char *node, int *flags,
217 char **extra_opts, char **mount_opts, int running_bg);
218#endif
219
220#ifndef RB_POWER_OFF
221/* Stop system and switch power off if possible. */
222#define RB_POWER_OFF 0x4321fedc
223#endif
224
225/* Include our own copy of struct sysinfo to avoid binary compatability
226 * problems with Linux 2.4, which changed things. Grumble, grumble. */
227struct sysinfo {
228 long uptime; /* Seconds since boot */
229 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
230 unsigned long totalram; /* Total usable main memory size */
231 unsigned long freeram; /* Available memory size */
232 unsigned long sharedram; /* Amount of shared memory */
233 unsigned long bufferram; /* Memory used by buffers */
234 unsigned long totalswap; /* Total swap space size */
235 unsigned long freeswap; /* swap space still available */
236 unsigned short procs; /* Number of current processes */
237 unsigned long totalhigh; /* Total high memory size */
238 unsigned long freehigh; /* Available high memory size */
239 unsigned int mem_unit; /* Memory unit size in bytes */
240 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
241};
242extern int sysinfo (struct sysinfo* info);
243
244/* Bit map related macros -- libc5 doens't provide these... sigh. */
245#ifndef setbit
246#define NBBY CHAR_BIT
247#define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
248#define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
249#define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
250#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
251#endif
252
253#endif /* _BB_INTERNAL_H_ */