blob: cff4af41d8f05e5c01436f7cb29a8810f4bb87c8 [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
Eric Andersen8c725e62000-11-30 00:27:06 +0000111/* Automagically pull in all the applet function prototypes and
112 * applet usage strings.
113 * These are all of the form:
114 * extern int foo_main(int argc, char **argv);
115 * extern const char foo_usage[];
116 * These are all autogenerated from the set of currently defined applets.
117 */
118#define PROTOTYPES
119#include "applets.h"
120#undef PROTOTYPES
Eric Andersenf6b71392000-09-26 01:09:18 +0000121
122extern const char *applet_name;
Eric Andersen8c725e62000-11-30 00:27:06 +0000123extern int applet_name_compare(const void *x, const void *y);
Eric Andersenf6b71392000-09-26 01:09:18 +0000124
125extern void usage(const char *usage) __attribute__ ((noreturn));
126extern void errorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
127extern void fatalError(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Matt Kraaief5529b2000-10-25 17:00:36 +0000128extern void perrorMsg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
Matt Kraai324a7782000-10-25 15:10:08 +0000129extern void fatalPerror(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
Eric Andersenf6b71392000-09-26 01:09:18 +0000130
131const char *modeString(int mode);
132const char *timeString(time_t timeVal);
133int isDirectory(const char *name, const int followLinks, struct stat *statBuf);
134int isDevice(const char *name);
135
136typedef struct ino_dev_hash_bucket_struct {
137 struct ino_dev_hash_bucket_struct *next;
138 ino_t ino;
139 dev_t dev;
140 char name[1];
141} ino_dev_hashtable_bucket_t;
142int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
143void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
144void reset_ino_dev_hashtable(void);
145
146int copyFile(const char *srcName, const char *destName,
147 int setModes, int followLinks, int forceFlag);
148int copySubFile(int srcFd, int dstFd, size_t remaining);
149char *buildName(const char *dirName, const char *fileName);
150int makeString(int argc, const char **argv, char *buf, int bufLen);
151char *getChunk(int size);
152char *chunkstrdup(const char *str);
153void freeChunks(void);
154int fullWrite(int fd, const char *buf, int len);
155int fullRead(int fd, char *buf, int len);
156int recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
157 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
158 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
159 void* userData);
160
161extern int createPath (const char *name, int mode);
162extern int parse_mode( const char* s, mode_t* theMode);
163
164extern int get_kernel_revision(void);
165
166extern int get_console_fd(char* tty_name);
167extern struct mntent *findMountPoint(const char *name, const char *table);
168extern void write_mtab(char* blockDevice, char* directory,
169 char* filesystemType, long flags, char* string_flags);
170extern void erase_mtab(const char * name);
171extern void mtab_read(void);
172extern char *mtab_first(void **iter);
173extern char *mtab_next(void **iter);
174extern char *mtab_getinfo(const char *match, const char which);
175extern int check_wildcard_match(const char* text, const char* pattern);
176extern long getNum (const char *cp);
177extern pid_t* findPidByName( char* pidName);
178extern int find_real_root_device_name(char* name);
179extern char *get_line_from_file(FILE *file);
180extern void print_file(FILE *file);
181extern int print_file_by_name(char *filename);
182extern char process_escape_sequence(char **ptr);
183extern char *get_last_path_component(char *path);
184extern void xregcomp(regex_t *preg, const char *regex, int cflags);
Matt Kraaic0321f92000-09-27 04:09:22 +0000185extern FILE *wfopen(const char *path, const char *mode);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000186extern FILE *xfopen(const char *path, const char *mode);
Eric Andersenf6b71392000-09-26 01:09:18 +0000187
188#ifndef DMALLOC
189extern void *xmalloc (size_t size);
190extern void *xrealloc(void *old, size_t size);
191extern void *xcalloc(size_t nmemb, size_t size);
192extern char *xstrdup (const char *s);
193#endif
194extern char *xstrndup (const char *s, int n);
195
196
197/* These parse entries in /etc/passwd and /etc/group. This is desirable
198 * for BusyBox since we want to avoid using the glibc NSS stuff, which
199 * increases target size and is often not needed embedded systems. */
200extern long my_getpwnam(char *name);
201extern long my_getgrnam(char *name);
202extern void my_getpwuid(char *name, long uid);
203extern void my_getgrgid(char *group, long gid);
204extern long my_getpwnamegid(char *name);
205
206extern int device_open(char *device, int mode);
207
208#if defined BB_FEATURE_MOUNT_LOOP
209extern int del_loop(const char *device);
210extern int set_loop(const char *device, const char *file, int offset, int *loopro);
211extern char *find_unused_loop_device (void);
212#endif
213
214
215#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
216extern int vdprintf(int d, const char *format, va_list ap);
217#endif
218
219#if defined BB_NFSMOUNT
220int nfsmount(const char *spec, const char *node, int *flags,
221 char **extra_opts, char **mount_opts, int running_bg);
222#endif
223
224#ifndef RB_POWER_OFF
225/* Stop system and switch power off if possible. */
226#define RB_POWER_OFF 0x4321fedc
227#endif
228
229/* Include our own copy of struct sysinfo to avoid binary compatability
230 * problems with Linux 2.4, which changed things. Grumble, grumble. */
231struct sysinfo {
232 long uptime; /* Seconds since boot */
233 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
234 unsigned long totalram; /* Total usable main memory size */
235 unsigned long freeram; /* Available memory size */
236 unsigned long sharedram; /* Amount of shared memory */
237 unsigned long bufferram; /* Memory used by buffers */
238 unsigned long totalswap; /* Total swap space size */
239 unsigned long freeswap; /* swap space still available */
240 unsigned short procs; /* Number of current processes */
241 unsigned long totalhigh; /* Total high memory size */
242 unsigned long freehigh; /* Available high memory size */
243 unsigned int mem_unit; /* Memory unit size in bytes */
244 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
245};
246extern int sysinfo (struct sysinfo* info);
247
248/* Bit map related macros -- libc5 doens't provide these... sigh. */
249#ifndef setbit
250#define NBBY CHAR_BIT
251#define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
252#define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
253#define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
254#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
255#endif
256
257#endif /* _BB_INTERNAL_H_ */