aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile36
-rw-r--r--applets.c92
-rw-r--r--applets/applets.c92
-rw-r--r--busybox.h193
-rw-r--r--include/busybox.h193
-rw-r--r--include/libbb.h209
-rw-r--r--libbb/ask_confirmation.c53
-rw-r--r--libbb/check_wildcard_match.c140
-rw-r--r--libbb/chomp.c51
-rw-r--r--libbb/copy_file.c180
-rw-r--r--libbb/copy_file_chunk.c60
-rw-r--r--libbb/create_path.c71
-rw-r--r--libbb/device_open.c59
-rw-r--r--libbb/error_msg.c100
-rw-r--r--libbb/find_mount_point.c80
-rw-r--r--libbb/find_pid_by_name.c168
-rw-r--r--libbb/find_root_device.c85
-rw-r--r--libbb/full_read.c70
-rw-r--r--libbb/full_write.c66
-rw-r--r--libbb/get_console.c129
-rw-r--r--libbb/get_last_path_component.c58
-rw-r--r--libbb/get_line_from_file.c72
-rw-r--r--libbb/human_readable.c62
-rw-r--r--libbb/inode_hash.c105
-rw-r--r--libbb/isdirectory.c71
-rw-r--r--libbb/kernel_version.c66
-rw-r--r--libbb/libbb.h209
-rw-r--r--libbb/loop.c128
-rw-r--r--libbb/mode_string.c82
-rw-r--r--libbb/parse_mode.c138
-rw-r--r--libbb/parse_number.c74
-rw-r--r--libbb/print_file.c59
-rw-r--r--libbb/process_escape_sequence.c83
-rw-r--r--libbb/pwd_grp_wrappers.c109
-rw-r--r--libbb/recursive_action.c160
-rw-r--r--libbb/safe_read.c54
-rw-r--r--libbb/safe_strncpy.c48
-rw-r--r--libbb/syscalls.c76
-rw-r--r--libbb/syslog_msg_with_name.c53
-rw-r--r--libbb/time_string.c68
-rw-r--r--libbb/trim.c52
-rw-r--r--libbb/vdprintf.c52
-rw-r--r--libbb/wfopen.c50
-rw-r--r--libbb/xfuncs.c107
-rw-r--r--libbb/xregcomp.c53
-rw-r--r--mtab_file.c52
-rw-r--r--utility.c1887
47 files changed, 3901 insertions, 2254 deletions
diff --git a/Makefile b/Makefile
index 8af079062..5f5c80e6d 100644
--- a/Makefile
+++ b/Makefile
@@ -200,7 +200,7 @@ endif
200# And option 4: 200# And option 4:
201-include applet_source_list 201-include applet_source_list
202 202
203OBJECTS = $(APPLET_SOURCES:.c=.o) busybox.o messages.o usage.o utility.o 203OBJECTS = $(APPLET_SOURCES:.c=.o) busybox.o messages.o usage.o applets.o mtab_file.o
204CFLAGS += $(CROSS_CFLAGS) 204CFLAGS += $(CROSS_CFLAGS)
205CFLAGS += -DBB_VER='"$(VERSION)"' 205CFLAGS += -DBB_VER='"$(VERSION)"'
206CFLAGS += -DBB_BT='"$(BUILDTIME)"' 206CFLAGS += -DBB_BT='"$(BUILDTIME)"'
@@ -220,6 +220,21 @@ ifneq ($(strip $(USE_SYSTEM_PWD_GRP)),true)
220else 220else
221 CFLAGS += -DUSE_SYSTEM_PWD_GRP 221 CFLAGS += -DUSE_SYSTEM_PWD_GRP
222endif 222endif
223
224LIBBB = libbb
225LIBBB_DIR = $(BB_SRC_DIR:=/)$(LIBBB)
226LIBBB_LIB = libbb.a
227LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c copy_file.c \
228copy_file_chunk.c create_path.c device_open.c error_msg.c \
229find_mount_point.c find_pid_by_name.c find_root_device.c full_read.c \
230full_write.c get_console.c get_last_path_component.c get_line_from_file.c \
231human_readable.c inode_hash.c isdirectory.c kernel_version.c loop.c \
232mode_string.c parse_mode.c parse_number.c print_file.c process_escape_sequence.c \
233pwd_grp_wrappers.c recursive_action.c safe_read.c safe_strncpy.c syscalls.c \
234syslog_msg_with_name.c time_string.c trim.c vdprintf.c wfopen.c xfuncs.c \
235xregcomp.c
236LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
237LIBBB_CFLAGS = -I$(LIBBB_DIR)
223 238
224# Put user-supplied flags at the end, where they 239# Put user-supplied flags at the end, where they
225# have a chance of winning. 240# have a chance of winning.
@@ -288,8 +303,8 @@ docs/busybox/busyboxdocumentation.html: docs/busybox.sgml
288 (cd docs/busybox.lineo.com; sgmltools -b html ../busybox.sgml) 303 (cd docs/busybox.lineo.com; sgmltools -b html ../busybox.sgml)
289 304
290 305
291busybox: $(PWD_LIB) $(OBJECTS) 306busybox: $(PWD_LIB) $(LIBBB_LIB) $(OBJECTS)
292 $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBRARIES) $(PWD_LIB) 307 $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBRARIES) $(PWD_LIB) $(LIBBB_LIB)
293 $(STRIP) 308 $(STRIP)
294 309
295# Without VPATH, rule expands to "/bin/sh busybox.mkll Config.h applets.h" 310# Without VPATH, rule expands to "/bin/sh busybox.mkll Config.h applets.h"
@@ -307,14 +322,21 @@ $(PWD_OBJS): %.o: %.c Config.h busybox.h applets.h Makefile
307 - mkdir -p $(PWD_GRP) 322 - mkdir -p $(PWD_GRP)
308 $(CC) $(CFLAGS) $(PWD_CFLAGS) -c $< -o $*.o 323 $(CC) $(CFLAGS) $(PWD_CFLAGS) -c $< -o $*.o
309 324
325$(LIBBB_OBJS): %.o: %.c Config.h busybox.h applets.h Makefile
326 - mkdir -p $(LIBBB)
327 $(CC) $(CFLAGS) $(LIBBB_CFLAGS) -c $< -o $*.o
328
310libpwd.a: $(PWD_OBJS) 329libpwd.a: $(PWD_OBJS)
311 $(AR) $(ARFLAGS) $@ $^ 330 $(AR) $(ARFLAGS) $@ $^
312 331
332libbb.a: $(LIBBB_OBJS)
333 $(AR) $(ARFLAGS) $@ $^
334
313usage.o: usage.h 335usage.o: usage.h
314 336
315utility.o: loop.h 337libbb/loop.o: libbb/loop.h
316 338
317loop.h: mk_loop_h.sh 339libbb/loop.h: mk_loop_h.sh
318 @ $(SHELL) $< > $@ 340 @ $(SHELL) $< > $@
319 341
320test tests: 342test tests:
@@ -330,8 +352,8 @@ clean:
330 - rm -f docs/busybox.txt docs/busybox.dvi docs/busybox.ps \ 352 - rm -f docs/busybox.txt docs/busybox.dvi docs/busybox.ps \
331 docs/busybox.pdf docs/busybox.lineo.com/busybox.html 353 docs/busybox.pdf docs/busybox.lineo.com/busybox.html
332 - rm -f multibuild.log Config.h.orig 354 - rm -f multibuild.log Config.h.orig
333 - rm -rf docs/busybox _install libpwd.a 355 - rm -rf docs/busybox _install libpwd.a libbb.a
334 - rm -f busybox.links loop.h *~ slist.mk core applet_source_list 356 - rm -f busybox.links libbb/loop.h *~ slist.mk core applet_source_list
335 - find -name \*.o -exec rm -f {} \; 357 - find -name \*.o -exec rm -f {} \;
336 358
337distclean: clean 359distclean: clean
diff --git a/applets.c b/applets.c
new file mode 100644
index 000000000..1e31e8925
--- /dev/null
+++ b/applets.c
@@ -0,0 +1,92 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include "busybox.h"
31
32#define bb_need_full_version
33#define BB_DECLARE_EXTERN
34#include "messages.c"
35
36struct BB_applet *applet_using;
37
38extern void show_usage(void)
39{
40 const char *format_string;
41 const char *usage_string = usage_messages;
42 int i;
43
44 for (i = applet_using - applets; i > 0; ) {
45 if (!*usage_string++) {
46 --i;
47 }
48 }
49 format_string = "%s\n\nUsage: %s %s\n\n";
50 if(*usage_string == 0)
51 format_string = "%s\n\nNo help available.\n\n";
52 fprintf(stderr, format_string,
53 full_version, applet_using->name, usage_string);
54 exit(EXIT_FAILURE);
55}
56
57static int applet_name_compare(const void *x, const void *y)
58{
59 const char *name = x;
60 const struct BB_applet *applet = y;
61
62 return strcmp(name, applet->name);
63}
64
65extern size_t NUM_APPLETS;
66
67struct BB_applet *find_applet_by_name(const char *name)
68{
69 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
70 applet_name_compare);
71}
72
73void run_applet_by_name(const char *name, int argc, char **argv)
74{
75 /* Do a binary search to find the applet entry given the name. */
76 if ((applet_using = find_applet_by_name(name)) != NULL) {
77 applet_name = applet_using->name;
78 if (argv[1] && strcmp(argv[1], "--help") == 0)
79 show_usage();
80 exit((*(applet_using->main)) (argc, argv));
81 }
82}
83
84
85/* END CODE */
86/*
87Local Variables:
88c-file-style: "linux"
89c-basic-offset: 4
90tab-width: 4
91End:
92*/
diff --git a/applets/applets.c b/applets/applets.c
new file mode 100644
index 000000000..1e31e8925
--- /dev/null
+++ b/applets/applets.c
@@ -0,0 +1,92 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include "busybox.h"
31
32#define bb_need_full_version
33#define BB_DECLARE_EXTERN
34#include "messages.c"
35
36struct BB_applet *applet_using;
37
38extern void show_usage(void)
39{
40 const char *format_string;
41 const char *usage_string = usage_messages;
42 int i;
43
44 for (i = applet_using - applets; i > 0; ) {
45 if (!*usage_string++) {
46 --i;
47 }
48 }
49 format_string = "%s\n\nUsage: %s %s\n\n";
50 if(*usage_string == 0)
51 format_string = "%s\n\nNo help available.\n\n";
52 fprintf(stderr, format_string,
53 full_version, applet_using->name, usage_string);
54 exit(EXIT_FAILURE);
55}
56
57static int applet_name_compare(const void *x, const void *y)
58{
59 const char *name = x;
60 const struct BB_applet *applet = y;
61
62 return strcmp(name, applet->name);
63}
64
65extern size_t NUM_APPLETS;
66
67struct BB_applet *find_applet_by_name(const char *name)
68{
69 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
70 applet_name_compare);
71}
72
73void run_applet_by_name(const char *name, int argc, char **argv)
74{
75 /* Do a binary search to find the applet entry given the name. */
76 if ((applet_using = find_applet_by_name(name)) != NULL) {
77 applet_name = applet_using->name;
78 if (argv[1] && strcmp(argv[1], "--help") == 0)
79 show_usage();
80 exit((*(applet_using->main)) (argc, argv));
81 }
82}
83
84
85/* END CODE */
86/*
87Local Variables:
88c-file-style: "linux"
89c-basic-offset: 4
90tab-width: 4
91End:
92*/
diff --git a/busybox.h b/busybox.h
index 194fd8220..9414234c7 100644
--- a/busybox.h
+++ b/busybox.h
@@ -44,31 +44,6 @@ typedef unsigned int socklen_t;
44#endif 44#endif
45 45
46 46
47/* Some useful definitions */
48#define FALSE ((int) 0)
49#define TRUE ((int) 1)
50#define SKIP ((int) 2)
51
52/* for mtab.c */
53#define MTAB_GETMOUNTPT '1'
54#define MTAB_GETDEVICE '2'
55
56#define BUF_SIZE 8192
57#define EXPAND_ALLOC 1024
58
59static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); }
60static inline int is_octal(ch) { return ((ch >= '0') && (ch <= '7')); }
61
62/* Macros for min/max. */
63#ifndef MIN
64#define MIN(a,b) (((a)<(b))?(a):(b))
65#endif
66
67#ifndef MAX
68#define MAX(a,b) (((a)>(b))?(a):(b))
69#endif
70
71
72enum Location { 47enum Location {
73 _BB_DIR_ROOT = 0, 48 _BB_DIR_ROOT = 0,
74 _BB_DIR_BIN, 49 _BB_DIR_BIN,
@@ -97,146 +72,14 @@ extern const struct BB_applet applets[];
97 72
98extern const char *applet_name; 73extern const char *applet_name;
99 74
100extern void show_usage(void) __attribute__ ((noreturn)); 75#ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
101extern void error_msg(const char *s, ...); 76#define RESERVE_BB_BUFFER(buffer,len) char buffer[len]
102extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); 77#define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
103extern void perror_msg(const char *s, ...); 78#else
104extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); 79#define RESERVE_BB_BUFFER(buffer,len) char *buffer=xmalloc(len)
105 80#define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
106const char *mode_string(int mode);
107const char *time_string(time_t timeVal);
108int is_directory(const char *name, const int followLinks, struct stat *statBuf);
109int isDevice(const char *name);
110
111typedef struct ino_dev_hash_bucket_struct {
112 struct ino_dev_hash_bucket_struct *next;
113 ino_t ino;
114 dev_t dev;
115 char name[1];
116} ino_dev_hashtable_bucket_t;
117int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
118void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
119void reset_ino_dev_hashtable(void);
120
121int copy_file(const char *srcName, const char *destName,
122 int setModes, int followLinks, int forceFlag);
123int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
124char *buildName(const char *dirName, const char *fileName);
125int makeString(int argc, const char **argv, char *buf, int bufLen);
126char *getChunk(int size);
127char *chunkstrdup(const char *str);
128void freeChunks(void);
129ssize_t safe_read(int fd, void *buf, size_t count);
130int full_write(int fd, const char *buf, int len);
131int full_read(int fd, char *buf, int len);
132int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
133 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
134 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
135 void* userData);
136
137extern int create_path (const char *name, int mode);
138extern int parse_mode( const char* s, mode_t* theMode);
139
140extern int get_kernel_revision(void);
141
142extern int get_console_fd(char* tty_name);
143extern struct mntent *find_mount_point(const char *name, const char *table);
144extern void write_mtab(char* blockDevice, char* directory,
145 char* filesystemType, long flags, char* string_flags);
146extern void erase_mtab(const char * name);
147extern void mtab_read(void);
148extern char *mtab_first(void **iter);
149extern char *mtab_next(void **iter);
150extern char *mtab_getinfo(const char *match, const char which);
151extern int check_wildcard_match(const char* text, const char* pattern);
152extern long atoi_w_units (const char *cp);
153extern pid_t* find_pid_by_name( char* pidName);
154extern int find_real_root_device_name(char* name);
155extern char *get_line_from_file(FILE *file);
156extern void print_file(FILE *file);
157extern int print_file_by_name(char *filename);
158extern char process_escape_sequence(char **ptr);
159extern char *get_last_path_component(char *path);
160extern FILE *wfopen(const char *path, const char *mode);
161extern FILE *xfopen(const char *path, const char *mode);
162extern void chomp(char *s);
163extern void trim(char *s);
164extern struct BB_applet *find_applet_by_name(const char *name);
165void run_applet_by_name(const char *name, int argc, char **argv);
166
167#ifndef DMALLOC
168extern void *xmalloc (size_t size);
169extern void *xrealloc(void *old, size_t size);
170extern void *xcalloc(size_t nmemb, size_t size);
171extern char *xstrdup (const char *s);
172#endif
173extern char *xstrndup (const char *s, int n);
174extern char * safe_strncpy(char *dst, const char *src, size_t size);
175
176struct suffix_mult {
177 char *suffix;
178 int mult;
179};
180
181extern unsigned long parse_number(const char *numstr,
182 const struct suffix_mult *suffixes);
183
184
185/* These parse entries in /etc/passwd and /etc/group. This is desirable
186 * for BusyBox since we want to avoid using the glibc NSS stuff, which
187 * increases target size and is often not needed embedded systems. */
188extern long my_getpwnam(const char *name);
189extern long my_getgrnam(const char *name);
190extern void my_getpwuid(char *name, long uid);
191extern void my_getgrgid(char *group, long gid);
192extern long my_getpwnamegid(const char *name);
193
194extern int device_open(char *device, int mode);
195
196#if defined BB_FEATURE_MOUNT_LOOP
197extern int del_loop(const char *device);
198extern int set_loop(const char *device, const char *file, int offset, int *loopro);
199extern char *find_unused_loop_device (void);
200#endif
201
202
203#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
204extern int vdprintf(int d, const char *format, va_list ap);
205#endif
206
207#if defined BB_NFSMOUNT
208int nfsmount(const char *spec, const char *node, int *flags,
209 char **extra_opts, char **mount_opts, int running_bg);
210#endif
211
212#ifndef RB_POWER_OFF
213/* Stop system and switch power off if possible. */
214#define RB_POWER_OFF 0x4321fedc
215#endif
216
217#if defined(BB_KLOGD) || defined(BB_LOGGER)
218void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
219void syslog_msg(int facility, int pri, const char *msg);
220#endif 81#endif
221 82
222/* Include our own copy of struct sysinfo to avoid binary compatability
223 * problems with Linux 2.4, which changed things. Grumble, grumble. */
224struct sysinfo {
225 long uptime; /* Seconds since boot */
226 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
227 unsigned long totalram; /* Total usable main memory size */
228 unsigned long freeram; /* Available memory size */
229 unsigned long sharedram; /* Amount of shared memory */
230 unsigned long bufferram; /* Memory used by buffers */
231 unsigned long totalswap; /* Total swap space size */
232 unsigned long freeswap; /* swap space still available */
233 unsigned short procs; /* Number of current processes */
234 unsigned long totalhigh; /* Total high memory size */
235 unsigned long freehigh; /* Available high memory size */
236 unsigned int mem_unit; /* Memory unit size in bytes */
237 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
238};
239extern int sysinfo (struct sysinfo* info);
240 83
241/* Bit map related macros -- libc5 doens't provide these... sigh. */ 84/* Bit map related macros -- libc5 doens't provide these... sigh. */
242#ifndef setbit 85#ifndef setbit
@@ -247,25 +90,15 @@ extern int sysinfo (struct sysinfo* info);
247#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) 90#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
248#endif 91#endif
249 92
250#ifdef BB_FEATURE_HUMAN_READABLE 93#ifndef RB_POWER_OFF
251const char *make_human_readable_str(unsigned long val, unsigned long hr); 94/* Stop system and switch power off if possible. */
95#define RB_POWER_OFF 0x4321fedc
252#endif 96#endif
253enum {
254 KILOBYTE = 1024,
255 MEGABYTE = (KILOBYTE*1024),
256 GIGABYTE = (MEGABYTE*1024)
257};
258 97
259#ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
260#define RESERVE_BB_BUFFER(buffer,len) char buffer[len]
261#define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
262#else
263#define RESERVE_BB_BUFFER(buffer,len) char *buffer=xmalloc(len)
264#define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
265#endif
266 98
267#if defined(BB_FEATURE_RM_INTERACTIVE) && defined(BB_RM) 99/* Pull in the utility routines from libbb */
268int ask_confirmation(void); 100#include "libbb/libbb.h"
269#endif 101
102
270 103
271#endif /* _BB_INTERNAL_H_ */ 104#endif /* _BB_INTERNAL_H_ */
diff --git a/include/busybox.h b/include/busybox.h
index 194fd8220..9414234c7 100644
--- a/include/busybox.h
+++ b/include/busybox.h
@@ -44,31 +44,6 @@ typedef unsigned int socklen_t;
44#endif 44#endif
45 45
46 46
47/* Some useful definitions */
48#define FALSE ((int) 0)
49#define TRUE ((int) 1)
50#define SKIP ((int) 2)
51
52/* for mtab.c */
53#define MTAB_GETMOUNTPT '1'
54#define MTAB_GETDEVICE '2'
55
56#define BUF_SIZE 8192
57#define EXPAND_ALLOC 1024
58
59static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); }
60static inline int is_octal(ch) { return ((ch >= '0') && (ch <= '7')); }
61
62/* Macros for min/max. */
63#ifndef MIN
64#define MIN(a,b) (((a)<(b))?(a):(b))
65#endif
66
67#ifndef MAX
68#define MAX(a,b) (((a)>(b))?(a):(b))
69#endif
70
71
72enum Location { 47enum Location {
73 _BB_DIR_ROOT = 0, 48 _BB_DIR_ROOT = 0,
74 _BB_DIR_BIN, 49 _BB_DIR_BIN,
@@ -97,146 +72,14 @@ extern const struct BB_applet applets[];
97 72
98extern const char *applet_name; 73extern const char *applet_name;
99 74
100extern void show_usage(void) __attribute__ ((noreturn)); 75#ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
101extern void error_msg(const char *s, ...); 76#define RESERVE_BB_BUFFER(buffer,len) char buffer[len]
102extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); 77#define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
103extern void perror_msg(const char *s, ...); 78#else
104extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); 79#define RESERVE_BB_BUFFER(buffer,len) char *buffer=xmalloc(len)
105 80#define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
106const char *mode_string(int mode);
107const char *time_string(time_t timeVal);
108int is_directory(const char *name, const int followLinks, struct stat *statBuf);
109int isDevice(const char *name);
110
111typedef struct ino_dev_hash_bucket_struct {
112 struct ino_dev_hash_bucket_struct *next;
113 ino_t ino;
114 dev_t dev;
115 char name[1];
116} ino_dev_hashtable_bucket_t;
117int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
118void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
119void reset_ino_dev_hashtable(void);
120
121int copy_file(const char *srcName, const char *destName,
122 int setModes, int followLinks, int forceFlag);
123int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
124char *buildName(const char *dirName, const char *fileName);
125int makeString(int argc, const char **argv, char *buf, int bufLen);
126char *getChunk(int size);
127char *chunkstrdup(const char *str);
128void freeChunks(void);
129ssize_t safe_read(int fd, void *buf, size_t count);
130int full_write(int fd, const char *buf, int len);
131int full_read(int fd, char *buf, int len);
132int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
133 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
134 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
135 void* userData);
136
137extern int create_path (const char *name, int mode);
138extern int parse_mode( const char* s, mode_t* theMode);
139
140extern int get_kernel_revision(void);
141
142extern int get_console_fd(char* tty_name);
143extern struct mntent *find_mount_point(const char *name, const char *table);
144extern void write_mtab(char* blockDevice, char* directory,
145 char* filesystemType, long flags, char* string_flags);
146extern void erase_mtab(const char * name);
147extern void mtab_read(void);
148extern char *mtab_first(void **iter);
149extern char *mtab_next(void **iter);
150extern char *mtab_getinfo(const char *match, const char which);
151extern int check_wildcard_match(const char* text, const char* pattern);
152extern long atoi_w_units (const char *cp);
153extern pid_t* find_pid_by_name( char* pidName);
154extern int find_real_root_device_name(char* name);
155extern char *get_line_from_file(FILE *file);
156extern void print_file(FILE *file);
157extern int print_file_by_name(char *filename);
158extern char process_escape_sequence(char **ptr);
159extern char *get_last_path_component(char *path);
160extern FILE *wfopen(const char *path, const char *mode);
161extern FILE *xfopen(const char *path, const char *mode);
162extern void chomp(char *s);
163extern void trim(char *s);
164extern struct BB_applet *find_applet_by_name(const char *name);
165void run_applet_by_name(const char *name, int argc, char **argv);
166
167#ifndef DMALLOC
168extern void *xmalloc (size_t size);
169extern void *xrealloc(void *old, size_t size);
170extern void *xcalloc(size_t nmemb, size_t size);
171extern char *xstrdup (const char *s);
172#endif
173extern char *xstrndup (const char *s, int n);
174extern char * safe_strncpy(char *dst, const char *src, size_t size);
175
176struct suffix_mult {
177 char *suffix;
178 int mult;
179};
180
181extern unsigned long parse_number(const char *numstr,
182 const struct suffix_mult *suffixes);
183
184
185/* These parse entries in /etc/passwd and /etc/group. This is desirable
186 * for BusyBox since we want to avoid using the glibc NSS stuff, which
187 * increases target size and is often not needed embedded systems. */
188extern long my_getpwnam(const char *name);
189extern long my_getgrnam(const char *name);
190extern void my_getpwuid(char *name, long uid);
191extern void my_getgrgid(char *group, long gid);
192extern long my_getpwnamegid(const char *name);
193
194extern int device_open(char *device, int mode);
195
196#if defined BB_FEATURE_MOUNT_LOOP
197extern int del_loop(const char *device);
198extern int set_loop(const char *device, const char *file, int offset, int *loopro);
199extern char *find_unused_loop_device (void);
200#endif
201
202
203#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
204extern int vdprintf(int d, const char *format, va_list ap);
205#endif
206
207#if defined BB_NFSMOUNT
208int nfsmount(const char *spec, const char *node, int *flags,
209 char **extra_opts, char **mount_opts, int running_bg);
210#endif
211
212#ifndef RB_POWER_OFF
213/* Stop system and switch power off if possible. */
214#define RB_POWER_OFF 0x4321fedc
215#endif
216
217#if defined(BB_KLOGD) || defined(BB_LOGGER)
218void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
219void syslog_msg(int facility, int pri, const char *msg);
220#endif 81#endif
221 82
222/* Include our own copy of struct sysinfo to avoid binary compatability
223 * problems with Linux 2.4, which changed things. Grumble, grumble. */
224struct sysinfo {
225 long uptime; /* Seconds since boot */
226 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
227 unsigned long totalram; /* Total usable main memory size */
228 unsigned long freeram; /* Available memory size */
229 unsigned long sharedram; /* Amount of shared memory */
230 unsigned long bufferram; /* Memory used by buffers */
231 unsigned long totalswap; /* Total swap space size */
232 unsigned long freeswap; /* swap space still available */
233 unsigned short procs; /* Number of current processes */
234 unsigned long totalhigh; /* Total high memory size */
235 unsigned long freehigh; /* Available high memory size */
236 unsigned int mem_unit; /* Memory unit size in bytes */
237 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
238};
239extern int sysinfo (struct sysinfo* info);
240 83
241/* Bit map related macros -- libc5 doens't provide these... sigh. */ 84/* Bit map related macros -- libc5 doens't provide these... sigh. */
242#ifndef setbit 85#ifndef setbit
@@ -247,25 +90,15 @@ extern int sysinfo (struct sysinfo* info);
247#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) 90#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
248#endif 91#endif
249 92
250#ifdef BB_FEATURE_HUMAN_READABLE 93#ifndef RB_POWER_OFF
251const char *make_human_readable_str(unsigned long val, unsigned long hr); 94/* Stop system and switch power off if possible. */
95#define RB_POWER_OFF 0x4321fedc
252#endif 96#endif
253enum {
254 KILOBYTE = 1024,
255 MEGABYTE = (KILOBYTE*1024),
256 GIGABYTE = (MEGABYTE*1024)
257};
258 97
259#ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
260#define RESERVE_BB_BUFFER(buffer,len) char buffer[len]
261#define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
262#else
263#define RESERVE_BB_BUFFER(buffer,len) char *buffer=xmalloc(len)
264#define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
265#endif
266 98
267#if defined(BB_FEATURE_RM_INTERACTIVE) && defined(BB_RM) 99/* Pull in the utility routines from libbb */
268int ask_confirmation(void); 100#include "libbb/libbb.h"
269#endif 101
102
270 103
271#endif /* _BB_INTERNAL_H_ */ 104#endif /* _BB_INTERNAL_H_ */
diff --git a/include/libbb.h b/include/libbb.h
new file mode 100644
index 000000000..a85987d9e
--- /dev/null
+++ b/include/libbb.h
@@ -0,0 +1,209 @@
1/* 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 __LIBBB_H__
25#define __LIBBB_H__ 1
26
27#include <stdio.h>
28#include <stdarg.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31
32#ifdef DMALLOC
33#include "dmalloc.h"
34#endif
35
36#include <features.h>
37/* Stupid libc doesn't have a reliable way for use to know
38 * that libc5 is being used. Assume this is good enough */
39#if ! defined __GLIBC__ && ! defined __UCLIBC__
40/* libc5 doesn't define socklen_t */
41typedef unsigned int socklen_t;
42#endif
43
44/* Some useful definitions */
45#define FALSE ((int) 0)
46#define TRUE ((int) 1)
47#define SKIP ((int) 2)
48
49/* for mtab.c */
50#define MTAB_GETMOUNTPT '1'
51#define MTAB_GETDEVICE '2'
52
53#define BUF_SIZE 8192
54#define EXPAND_ALLOC 1024
55
56static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); }
57static inline int is_octal(ch) { return ((ch >= '0') && (ch <= '7')); }
58
59/* Macros for min/max. */
60#ifndef MIN
61#define MIN(a,b) (((a)<(b))?(a):(b))
62#endif
63
64#ifndef MAX
65#define MAX(a,b) (((a)>(b))?(a):(b))
66#endif
67
68
69
70extern void show_usage(void) __attribute__ ((noreturn));
71extern void error_msg(const char *s, ...);
72extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
73extern void perror_msg(const char *s, ...);
74extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
75
76const char *mode_string(int mode);
77const char *time_string(time_t timeVal);
78int is_directory(const char *name, const int followLinks, struct stat *statBuf);
79int isDevice(const char *name);
80
81typedef struct ino_dev_hash_bucket_struct {
82 struct ino_dev_hash_bucket_struct *next;
83 ino_t ino;
84 dev_t dev;
85 char name[1];
86} ino_dev_hashtable_bucket_t;
87int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
88void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
89void reset_ino_dev_hashtable(void);
90
91int copy_file(const char *srcName, const char *destName,
92 int setModes, int followLinks, int forceFlag);
93int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
94char *buildName(const char *dirName, const char *fileName);
95int makeString(int argc, const char **argv, char *buf, int bufLen);
96char *getChunk(int size);
97char *chunkstrdup(const char *str);
98void freeChunks(void);
99ssize_t safe_read(int fd, void *buf, size_t count);
100int full_write(int fd, const char *buf, int len);
101int full_read(int fd, char *buf, int len);
102int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
103 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
104 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
105 void* userData);
106
107extern int create_path (const char *name, int mode);
108extern int parse_mode( const char* s, mode_t* theMode);
109
110extern int get_kernel_revision(void);
111
112extern int get_console_fd(char* tty_name);
113extern struct mntent *find_mount_point(const char *name, const char *table);
114extern void write_mtab(char* blockDevice, char* directory,
115 char* filesystemType, long flags, char* string_flags);
116extern void erase_mtab(const char * name);
117extern void mtab_read(void);
118extern char *mtab_first(void **iter);
119extern char *mtab_next(void **iter);
120extern char *mtab_getinfo(const char *match, const char which);
121extern int check_wildcard_match(const char* text, const char* pattern);
122extern long atoi_w_units (const char *cp);
123extern pid_t* find_pid_by_name( char* pidName);
124extern int find_real_root_device_name(char* name);
125extern char *get_line_from_file(FILE *file);
126extern void print_file(FILE *file);
127extern int print_file_by_name(char *filename);
128extern char process_escape_sequence(char **ptr);
129extern char *get_last_path_component(char *path);
130extern FILE *wfopen(const char *path, const char *mode);
131extern FILE *xfopen(const char *path, const char *mode);
132extern void chomp(char *s);
133extern void trim(char *s);
134extern struct BB_applet *find_applet_by_name(const char *name);
135void run_applet_by_name(const char *name, int argc, char **argv);
136
137#ifndef DMALLOC
138extern void *xmalloc (size_t size);
139extern void *xrealloc(void *old, size_t size);
140extern void *xcalloc(size_t nmemb, size_t size);
141extern char *xstrdup (const char *s);
142#endif
143extern char *xstrndup (const char *s, int n);
144extern char * safe_strncpy(char *dst, const char *src, size_t size);
145
146struct suffix_mult {
147 char *suffix;
148 int mult;
149};
150
151extern unsigned long parse_number(const char *numstr,
152 const struct suffix_mult *suffixes);
153
154
155/* These parse entries in /etc/passwd and /etc/group. This is desirable
156 * for BusyBox since we want to avoid using the glibc NSS stuff, which
157 * increases target size and is often not needed embedded systems. */
158extern long my_getpwnam(const char *name);
159extern long my_getgrnam(const char *name);
160extern void my_getpwuid(char *name, long uid);
161extern void my_getgrgid(char *group, long gid);
162extern long my_getpwnamegid(const char *name);
163
164extern int device_open(char *device, int mode);
165
166extern int del_loop(const char *device);
167extern int set_loop(const char *device, const char *file, int offset, int *loopro);
168extern char *find_unused_loop_device (void);
169
170
171#if (__GLIBC__ < 2)
172extern int vdprintf(int d, const char *format, va_list ap);
173#endif
174
175int nfsmount(const char *spec, const char *node, int *flags,
176 char **extra_opts, char **mount_opts, int running_bg);
177
178void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
179void syslog_msg(int facility, int pri, const char *msg);
180
181/* Include our own copy of struct sysinfo to avoid binary compatability
182 * problems with Linux 2.4, which changed things. Grumble, grumble. */
183struct sysinfo {
184 long uptime; /* Seconds since boot */
185 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
186 unsigned long totalram; /* Total usable main memory size */
187 unsigned long freeram; /* Available memory size */
188 unsigned long sharedram; /* Amount of shared memory */
189 unsigned long bufferram; /* Memory used by buffers */
190 unsigned long totalswap; /* Total swap space size */
191 unsigned long freeswap; /* swap space still available */
192 unsigned short procs; /* Number of current processes */
193 unsigned long totalhigh; /* Total high memory size */
194 unsigned long freehigh; /* Available high memory size */
195 unsigned int mem_unit; /* Memory unit size in bytes */
196 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
197};
198extern int sysinfo (struct sysinfo* info);
199
200const char *make_human_readable_str(unsigned long val, unsigned long hr);
201enum {
202 KILOBYTE = 1024,
203 MEGABYTE = (KILOBYTE*1024),
204 GIGABYTE = (MEGABYTE*1024)
205};
206
207int ask_confirmation(void);
208
209#endif /* __LIBBB_H__ */
diff --git a/libbb/ask_confirmation.c b/libbb/ask_confirmation.c
new file mode 100644
index 000000000..f2922379c
--- /dev/null
+++ b/libbb/ask_confirmation.c
@@ -0,0 +1,53 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32int ask_confirmation()
33{
34 int c = '\0';
35 int ret = 0;
36
37 while (c != '\n') {
38 c = getchar();
39 if ( c != '\n' ) {
40 ret = ((c=='y')||(c=='Y')) ? 1 : 0;
41 }
42 }
43 return ret;
44}
45
46/* END CODE */
47/*
48Local Variables:
49c-file-style: "linux"
50c-basic-offset: 4
51tab-width: 4
52End:
53*/
diff --git a/libbb/check_wildcard_match.c b/libbb/check_wildcard_match.c
new file mode 100644
index 000000000..7bd3c7998
--- /dev/null
+++ b/libbb/check_wildcard_match.c
@@ -0,0 +1,140 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32/*
33 * Routine to see if a text string is matched by a wildcard pattern.
34 * Returns TRUE if the text is matched, or FALSE if it is not matched
35 * or if the pattern is invalid.
36 * * matches zero or more characters
37 * ? matches a single character
38 * [abc] matches 'a', 'b' or 'c'
39 * \c quotes character c
40 * Adapted from code written by Ingo Wilken, and
41 * then taken from sash, Copyright (c) 1999 by David I. Bell
42 * Permission is granted to use, distribute, or modify this source,
43 * provided that this copyright notice remains intact.
44 * Permission to distribute this code under the GPL has been granted.
45 */
46extern int check_wildcard_match(const char *text, const char *pattern)
47{
48 const char *retryPat;
49 const char *retryText;
50 int ch;
51 int found;
52 int len;
53
54 retryPat = NULL;
55 retryText = NULL;
56
57 while (*text || *pattern) {
58 ch = *pattern++;
59
60 switch (ch) {
61 case '*':
62 retryPat = pattern;
63 retryText = text;
64 break;
65
66 case '[':
67 found = FALSE;
68
69 while ((ch = *pattern++) != ']') {
70 if (ch == '\\')
71 ch = *pattern++;
72
73 if (ch == '\0')
74 return FALSE;
75
76 if (*text == ch)
77 found = TRUE;
78 }
79 len=strlen(text);
80 if (found == FALSE && len!=0) {
81 return FALSE;
82 }
83 if (found == TRUE) {
84 if (strlen(pattern)==0 && len==1) {
85 return TRUE;
86 }
87 if (len!=0) {
88 text++;
89 continue;
90 }
91 }
92
93 /* fall into next case */
94
95 case '?':
96 if (*text++ == '\0')
97 return FALSE;
98
99 break;
100
101 case '\\':
102 ch = *pattern++;
103
104 if (ch == '\0')
105 return FALSE;
106
107 /* fall into next case */
108
109 default:
110 if (*text == ch) {
111 if (*text)
112 text++;
113 break;
114 }
115
116 if (*text) {
117 pattern = retryPat;
118 text = ++retryText;
119 break;
120 }
121
122 return FALSE;
123 }
124
125 if (pattern == NULL)
126 return FALSE;
127 }
128
129 return TRUE;
130}
131
132
133/* END CODE */
134/*
135Local Variables:
136c-file-style: "linux"
137c-basic-offset: 4
138tab-width: 4
139End:
140*/
diff --git a/libbb/chomp.c b/libbb/chomp.c
new file mode 100644
index 000000000..cbd3d04bc
--- /dev/null
+++ b/libbb/chomp.c
@@ -0,0 +1,51 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32void chomp(char *s)
33{
34 size_t len = strlen(s);
35
36 if (len == 0)
37 return;
38
39 if (s[len-1] == '\n')
40 s[len-1] = '\0';
41}
42
43
44/* END CODE */
45/*
46Local Variables:
47c-file-style: "linux"
48c-basic-offset: 4
49tab-width: 4
50End:
51*/
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
new file mode 100644
index 000000000..64b65b9fa
--- /dev/null
+++ b/libbb/copy_file.c
@@ -0,0 +1,180 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <utime.h>
31#include <unistd.h>
32#include <fcntl.h>
33#include <sys/stat.h>
34#include "libbb.h"
35
36
37/*
38 * Copy one file to another, while possibly preserving its modes, times, and
39 * modes. Returns TRUE if successful, or FALSE on a failure with an error
40 * message output. (Failure is not indicated if attributes cannot be set.)
41 * -Erik Andersen
42 */
43int
44copy_file(const char *srcName, const char *destName,
45 int setModes, int followLinks, int forceFlag)
46{
47 int rfd;
48 int wfd;
49 int status;
50 struct stat srcStatBuf;
51 struct stat dstStatBuf;
52 struct utimbuf times;
53
54 if (followLinks == TRUE)
55 status = stat(srcName, &srcStatBuf);
56 else
57 status = lstat(srcName, &srcStatBuf);
58
59 if (status < 0) {
60 perror_msg("%s", srcName);
61 return FALSE;
62 }
63
64 if (followLinks == TRUE)
65 status = stat(destName, &dstStatBuf);
66 else
67 status = lstat(destName, &dstStatBuf);
68
69 if (status < 0 || forceFlag==TRUE) {
70 unlink(destName);
71 dstStatBuf.st_ino = -1;
72 dstStatBuf.st_dev = -1;
73 }
74
75 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
76 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
77 error_msg("Copying file \"%s\" to itself", srcName);
78 return FALSE;
79 }
80
81 if (S_ISDIR(srcStatBuf.st_mode)) {
82 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
83 /* Make sure the directory is writable */
84 status = mkdir(destName, 0777777 ^ umask(0));
85 if (status < 0 && errno != EEXIST) {
86 perror_msg("%s", destName);
87 return FALSE;
88 }
89 } else if (S_ISLNK(srcStatBuf.st_mode)) {
90 char link_val[BUFSIZ + 1];
91 int link_size;
92
93 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
94 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
95 link_size = readlink(srcName, &link_val[0], BUFSIZ);
96 if (link_size < 0) {
97 perror_msg("%s", srcName);
98 return FALSE;
99 }
100 link_val[link_size] = '\0';
101 status = symlink(link_val, destName);
102 if (status < 0) {
103 perror_msg("%s", destName);
104 return FALSE;
105 }
106#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
107 if (setModes == TRUE) {
108 /* Try to set owner, but fail silently like GNU cp */
109 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
110 }
111#endif
112 return TRUE;
113 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
114 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
115 if (mkfifo(destName, 0644) < 0) {
116 perror_msg("%s", destName);
117 return FALSE;
118 }
119 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
120 || S_ISSOCK(srcStatBuf.st_mode)) {
121 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
122 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
123 perror_msg("%s", destName);
124 return FALSE;
125 }
126 } else if (S_ISREG(srcStatBuf.st_mode)) {
127 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
128 rfd = open(srcName, O_RDONLY);
129 if (rfd < 0) {
130 perror_msg("%s", srcName);
131 return FALSE;
132 }
133
134 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
135 srcStatBuf.st_mode);
136 if (wfd < 0) {
137 perror_msg("%s", destName);
138 close(rfd);
139 return FALSE;
140 }
141
142 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
143 goto error_exit;
144
145 close(rfd);
146 if (close(wfd) < 0) {
147 return FALSE;
148 }
149 }
150
151 if (setModes == TRUE) {
152 /* This is fine, since symlinks never get here */
153 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
154 perror_msg_and_die("%s", destName);
155 if (chmod(destName, srcStatBuf.st_mode) < 0)
156 perror_msg_and_die("%s", destName);
157 times.actime = srcStatBuf.st_atime;
158 times.modtime = srcStatBuf.st_mtime;
159 if (utime(destName, &times) < 0)
160 perror_msg_and_die("%s", destName);
161 }
162
163 return TRUE;
164
165 error_exit:
166 perror_msg("%s", destName);
167 close(rfd);
168 close(wfd);
169
170 return FALSE;
171}
172
173/* END CODE */
174/*
175Local Variables:
176c-file-style: "linux"
177c-basic-offset: 4
178tab-width: 4
179End:
180*/
diff --git a/libbb/copy_file_chunk.c b/libbb/copy_file_chunk.c
new file mode 100644
index 000000000..3c657dd06
--- /dev/null
+++ b/libbb/copy_file_chunk.c
@@ -0,0 +1,60 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <sys/stat.h>
30#include "libbb.h"
31
32/*
33 * Copy chunksize bytes between two file descriptors
34 */
35int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
36{
37 size_t size;
38 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
39
40 while (chunksize > 0) {
41 if (chunksize > BUFSIZ)
42 size = BUFSIZ;
43 else
44 size = chunksize;
45 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
46 return(FALSE);
47 chunksize -= size;
48 }
49 return (TRUE);
50}
51
52
53/* END CODE */
54/*
55Local Variables:
56c-file-style: "linux"
57c-basic-offset: 4
58tab-width: 4
59End:
60*/
diff --git a/libbb/create_path.c b/libbb/create_path.c
new file mode 100644
index 000000000..328afc351
--- /dev/null
+++ b/libbb/create_path.c
@@ -0,0 +1,71 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include "libbb.h"
32
33/*
34 * Attempt to create the directories along the specified path, except for
35 * the final component. The mode is given for the final directory only,
36 * while all previous ones get default protections. Errors are not reported
37 * here, as failures to restore files can be reported later.
38 */
39extern int create_path(const char *name, int mode)
40{
41 char *cp;
42 char *cpOld;
43 char buf[BUFSIZ + 1];
44 int retVal = 0;
45
46 strcpy(buf, name);
47 for (cp = buf; *cp == '/'; cp++);
48 cp = strchr(cp, '/');
49 while (cp) {
50 cpOld = cp;
51 cp = strchr(cp + 1, '/');
52 *cpOld = '\0';
53 retVal = mkdir(buf, cp ? 0777 : mode);
54 if (retVal != 0 && errno != EEXIST) {
55 perror_msg("%s", buf);
56 return FALSE;
57 }
58 *cpOld = '/';
59 }
60 return TRUE;
61}
62
63
64/* END CODE */
65/*
66Local Variables:
67c-file-style: "linux"
68c-basic-offset: 4
69tab-width: 4
70End:
71*/
diff --git a/libbb/device_open.c b/libbb/device_open.c
new file mode 100644
index 000000000..8e97ce6c5
--- /dev/null
+++ b/libbb/device_open.c
@@ -0,0 +1,59 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <fcntl.h>
30#include "libbb.h"
31
32
33/* try to open up the specified device */
34extern int device_open(char *device, int mode)
35{
36 int m, f, fd = -1;
37
38 m = mode | O_NONBLOCK;
39
40 /* Retry up to 5 times */
41 for (f = 0; f < 5; f++)
42 if ((fd = open(device, m, 0600)) >= 0)
43 break;
44 if (fd < 0)
45 return fd;
46 /* Reset original flags. */
47 if (m != mode)
48 fcntl(fd, F_SETFL, mode);
49 return fd;
50}
51
52/* END CODE */
53/*
54Local Variables:
55c-file-style: "linux"
56c-basic-offset: 4
57tab-width: 4
58End:
59*/
diff --git a/libbb/error_msg.c b/libbb/error_msg.c
new file mode 100644
index 000000000..7773d32a2
--- /dev/null
+++ b/libbb/error_msg.c
@@ -0,0 +1,100 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include <stdlib.h>
32#include "libbb.h"
33
34extern const char *applet_name;
35
36static void verror_msg(const char *s, va_list p)
37{
38 fflush(stdout);
39 fprintf(stderr, "%s: ", applet_name);
40 vfprintf(stderr, s, p);
41}
42
43extern void error_msg(const char *s, ...)
44{
45 va_list p;
46
47 va_start(p, s);
48 verror_msg(s, p);
49 va_end(p);
50 putc('\n', stderr);
51}
52
53extern void error_msg_and_die(const char *s, ...)
54{
55 va_list p;
56
57 va_start(p, s);
58 verror_msg(s, p);
59 va_end(p);
60 putc('\n', stderr);
61 exit(EXIT_FAILURE);
62}
63
64static void vperror_msg(const char *s, va_list p)
65{
66 int err=errno;
67 if(s == 0) s = "";
68 verror_msg(s, p);
69 if (*s) s = ": ";
70 fprintf(stderr, "%s%s\n", s, strerror(err));
71}
72
73extern void perror_msg(const char *s, ...)
74{
75 va_list p;
76
77 va_start(p, s);
78 vperror_msg(s, p);
79 va_end(p);
80}
81
82extern void perror_msg_and_die(const char *s, ...)
83{
84 va_list p;
85
86 va_start(p, s);
87 vperror_msg(s, p);
88 va_end(p);
89 exit(EXIT_FAILURE);
90}
91
92
93/* END CODE */
94/*
95Local Variables:
96c-file-style: "linux"
97c-basic-offset: 4
98tab-width: 4
99End:
100*/
diff --git a/libbb/find_mount_point.c b/libbb/find_mount_point.c
new file mode 100644
index 000000000..27322f7f0
--- /dev/null
+++ b/libbb/find_mount_point.c
@@ -0,0 +1,80 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32#include <mntent.h>
33/*
34 * Given a block device, find the mount table entry if that block device
35 * is mounted.
36 *
37 * Given any other file (or directory), find the mount table entry for its
38 * filesystem.
39 */
40extern struct mntent *find_mount_point(const char *name, const char *table)
41{
42 struct stat s;
43 dev_t mountDevice;
44 FILE *mountTable;
45 struct mntent *mountEntry;
46
47 if (stat(name, &s) != 0)
48 return 0;
49
50 if ((s.st_mode & S_IFMT) == S_IFBLK)
51 mountDevice = s.st_rdev;
52 else
53 mountDevice = s.st_dev;
54
55
56 if ((mountTable = setmntent(table, "r")) == 0)
57 return 0;
58
59 while ((mountEntry = getmntent(mountTable)) != 0) {
60 if (strcmp(name, mountEntry->mnt_dir) == 0
61 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
62 break;
63 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
64 break;
65 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
66 break;
67 }
68 endmntent(mountTable);
69 return mountEntry;
70}
71
72
73/* END CODE */
74/*
75Local Variables:
76c-file-style: "linux"
77c-basic-offset: 4
78tab-width: 4
79End:
80*/
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
new file mode 100644
index 000000000..a22ee1ff8
--- /dev/null
+++ b/libbb/find_pid_by_name.c
@@ -0,0 +1,168 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <ctype.h>
30#include <string.h>
31#include <dirent.h>
32#include <stdlib.h>
33#include "libbb.h"
34
35
36/* For Erik's nifty devps device driver */
37#ifdef BB_FEATURE_USE_DEVPS_PATCH
38#include <linux/devps.h>
39
40/* find_pid_by_name()
41 *
42 * This finds the pid of the specified process,
43 * by using the /dev/ps device driver.
44 *
45 * Returns a list of all matching PIDs
46 */
47extern pid_t* find_pid_by_name( char* pidName)
48{
49 int fd, i, j;
50 char device[] = "/dev/ps";
51 pid_t num_pids;
52 pid_t* pid_array = NULL;
53 pid_t* pidList=NULL;
54
55 /* open device */
56 fd = open(device, O_RDONLY);
57 if (fd < 0)
58 perror_msg_and_die("open failed for `%s'", device);
59
60 /* Find out how many processes there are */
61 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
62 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
63
64 /* Allocate some memory -- grab a few extras just in case
65 * some new processes start up while we wait. The kernel will
66 * just ignore any extras if we give it too many, and will trunc.
67 * the list if we give it too few. */
68 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
69 pid_array[0] = num_pids+10;
70
71 /* Now grab the pid list */
72 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
73 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
74
75 /* Now search for a match */
76 for (i=1, j=0; i<pid_array[0] ; i++) {
77 char* p;
78 struct pid_info info;
79
80 info.pid = pid_array[i];
81 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
82 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
83
84 /* Make sure we only match on the process name */
85 p=info.command_line+1;
86 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
87 (p)++;
88 }
89 if (isspace(*(p)))
90 *p='\0';
91
92 if ((strstr(info.command_line, pidName) != NULL)
93 && (strlen(pidName) == strlen(info.command_line))) {
94 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
95 pidList[j++]=info.pid;
96 }
97 }
98 if (pidList)
99 pidList[j]=0;
100
101 /* Free memory */
102 free( pid_array);
103
104 /* close device */
105 if (close (fd) != 0)
106 perror_msg_and_die("close failed for `%s'", device);
107
108 return pidList;
109}
110
111#else /* BB_FEATURE_USE_DEVPS_PATCH */
112
113/* find_pid_by_name()
114 *
115 * This finds the pid of the specified process.
116 * Currently, it's implemented by rummaging through
117 * the proc filesystem.
118 *
119 * Returns a list of all matching PIDs
120 */
121extern pid_t* find_pid_by_name( char* pidName)
122{
123 DIR *dir;
124 struct dirent *next;
125 pid_t* pidList=NULL;
126 int i=0;
127
128 dir = opendir("/proc");
129 if (!dir)
130 perror_msg_and_die("Cannot open /proc");
131
132 while ((next = readdir(dir)) != NULL) {
133 FILE *status;
134 char filename[256];
135 char buffer[256];
136
137 /* If it isn't a number, we don't want it */
138 if (!isdigit(*next->d_name))
139 continue;
140
141 sprintf(filename, "/proc/%s/cmdline", next->d_name);
142 status = fopen(filename, "r");
143 if (!status) {
144 continue;
145 }
146 fgets(buffer, 256, status);
147 fclose(status);
148
149 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
150 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
151 pidList[i++]=strtol(next->d_name, NULL, 0);
152 }
153 }
154
155 if (pidList)
156 pidList[i]=0;
157 return pidList;
158}
159#endif /* BB_FEATURE_USE_DEVPS_PATCH */
160
161/* END CODE */
162/*
163Local Variables:
164c-file-style: "linux"
165c-basic-offset: 4
166tab-width: 4
167End:
168*/
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c
new file mode 100644
index 000000000..de765ce44
--- /dev/null
+++ b/libbb/find_root_device.c
@@ -0,0 +1,85 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <dirent.h>
31#include "libbb.h"
32
33
34
35extern int find_real_root_device_name(char* name)
36{
37 DIR *dir;
38 struct dirent *entry;
39 struct stat statBuf, rootStat;
40 char fileName[BUFSIZ];
41
42 if (stat("/", &rootStat) != 0) {
43 error_msg("could not stat '/'");
44 return( FALSE);
45 }
46
47 dir = opendir("/dev");
48 if (!dir) {
49 error_msg("could not open '/dev'");
50 return( FALSE);
51 }
52
53 while((entry = readdir(dir)) != NULL) {
54
55 /* Must skip ".." since that is "/", and so we
56 * would get a false positive on ".." */
57 if (strcmp(entry->d_name, "..") == 0)
58 continue;
59
60 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
61
62 if (stat(fileName, &statBuf) != 0)
63 continue;
64 /* Some char devices have the same dev_t as block
65 * devices, so make sure this is a block device */
66 if (! S_ISBLK(statBuf.st_mode))
67 continue;
68 if (statBuf.st_rdev == rootStat.st_rdev) {
69 strcpy(name, fileName);
70 return ( TRUE);
71 }
72 }
73
74 return( FALSE);
75}
76
77
78/* END CODE */
79/*
80Local Variables:
81c-file-style: "linux"
82c-basic-offset: 4
83tab-width: 4
84End:
85*/
diff --git a/libbb/full_read.c b/libbb/full_read.c
new file mode 100644
index 000000000..e9c4bbfc6
--- /dev/null
+++ b/libbb/full_read.c
@@ -0,0 +1,70 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <unistd.h>
30#include "libbb.h"
31
32
33/*
34 * Read all of the supplied buffer from a file.
35 * This does multiple reads as necessary.
36 * Returns the amount read, or -1 on an error.
37 * A short read is returned on an end of file.
38 */
39int full_read(int fd, char *buf, int len)
40{
41 int cc;
42 int total;
43
44 total = 0;
45
46 while (len > 0) {
47 cc = read(fd, buf, len);
48
49 if (cc < 0)
50 return -1;
51
52 if (cc == 0)
53 break;
54
55 buf += cc;
56 total += cc;
57 len -= cc;
58 }
59
60 return total;
61}
62
63/* END CODE */
64/*
65Local Variables:
66c-file-style: "linux"
67c-basic-offset: 4
68tab-width: 4
69End:
70*/
diff --git a/libbb/full_write.c b/libbb/full_write.c
new file mode 100644
index 000000000..dc9937fa3
--- /dev/null
+++ b/libbb/full_write.c
@@ -0,0 +1,66 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <unistd.h>
30#include "libbb.h"
31
32/*
33 * Write all of the supplied buffer out to a file.
34 * This does multiple writes as necessary.
35 * Returns the amount written, or -1 on an error.
36 */
37int full_write(int fd, const char *buf, int len)
38{
39 int cc;
40 int total;
41
42 total = 0;
43
44 while (len > 0) {
45 cc = write(fd, buf, len);
46
47 if (cc < 0)
48 return -1;
49
50 buf += cc;
51 total += cc;
52 len -= cc;
53 }
54
55 return total;
56}
57
58
59/* END CODE */
60/*
61Local Variables:
62c-file-style: "linux"
63c-basic-offset: 4
64tab-width: 4
65End:
66*/
diff --git a/libbb/get_console.c b/libbb/get_console.c
new file mode 100644
index 000000000..4be9adc84
--- /dev/null
+++ b/libbb/get_console.c
@@ -0,0 +1,129 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <sys/ioctl.h>
33#include "libbb.h"
34
35
36
37
38
39/* From <linux/kd.h> */
40static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
41static const int KB_84 = 0x01;
42static const int KB_101 = 0x02; /* this is what we always answer */
43
44int is_a_console(int fd)
45{
46 char arg;
47
48 arg = 0;
49 return (ioctl(fd, KDGKBTYPE, &arg) == 0
50 && ((arg == KB_101) || (arg == KB_84)));
51}
52
53static int open_a_console(char *fnam)
54{
55 int fd;
56
57 /* try read-only */
58 fd = open(fnam, O_RDWR);
59
60 /* if failed, try read-only */
61 if (fd < 0 && errno == EACCES)
62 fd = open(fnam, O_RDONLY);
63
64 /* if failed, try write-only */
65 if (fd < 0 && errno == EACCES)
66 fd = open(fnam, O_WRONLY);
67
68 /* if failed, fail */
69 if (fd < 0)
70 return -1;
71
72 /* if not a console, fail */
73 if (!is_a_console(fd)) {
74 close(fd);
75 return -1;
76 }
77
78 /* success */
79 return fd;
80}
81
82/*
83 * Get an fd for use with kbd/console ioctls.
84 * We try several things because opening /dev/console will fail
85 * if someone else used X (which does a chown on /dev/console).
86 *
87 * if tty_name is non-NULL, try this one instead.
88 */
89
90int get_console_fd(char *tty_name)
91{
92 int fd;
93
94 if (tty_name) {
95 if (-1 == (fd = open_a_console(tty_name)))
96 return -1;
97 else
98 return fd;
99 }
100
101 fd = open_a_console("/dev/tty");
102 if (fd >= 0)
103 return fd;
104
105 fd = open_a_console("/dev/tty0");
106 if (fd >= 0)
107 return fd;
108
109 fd = open_a_console("/dev/console");
110 if (fd >= 0)
111 return fd;
112
113 for (fd = 0; fd < 3; fd++)
114 if (is_a_console(fd))
115 return fd;
116
117 error_msg("Couldnt get a file descriptor referring to the console");
118 return -1; /* total failure */
119}
120
121
122/* END CODE */
123/*
124Local Variables:
125c-file-style: "linux"
126c-basic-offset: 4
127tab-width: 4
128End:
129*/
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c
new file mode 100644
index 000000000..a322288a6
--- /dev/null
+++ b/libbb/get_last_path_component.c
@@ -0,0 +1,58 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include "libbb.h"
31
32
33char *get_last_path_component(char *path)
34{
35 char *s=path+strlen(path)-1;
36
37 /* strip trailing slashes */
38 while (s != path && *s == '/') {
39 *s-- = '\0';
40 }
41
42 /* find last component */
43 s = strrchr(path, '/');
44 if (s == NULL || s[1] == '\0')
45 return path;
46 else
47 return s+1;
48}
49
50
51/* END CODE */
52/*
53Local Variables:
54c-file-style: "linux"
55c-basic-offset: 4
56tab-width: 4
57End:
58*/
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
new file mode 100644
index 000000000..759481731
--- /dev/null
+++ b/libbb/get_line_from_file.c
@@ -0,0 +1,72 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32
33/* get_line_from_file() - This function reads an entire line from a text file
34 * up to a newline. It returns a malloc'ed char * which must be stored and
35 * free'ed by the caller. */
36extern char *get_line_from_file(FILE *file)
37{
38 static const int GROWBY = 80; /* how large we will grow strings by */
39
40 int ch;
41 int idx = 0;
42 char *linebuf = NULL;
43 int linebufsz = 0;
44
45 while (1) {
46 ch = fgetc(file);
47 if (ch == EOF)
48 break;
49 /* grow the line buffer as necessary */
50 while (idx > linebufsz-2)
51 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
52 linebuf[idx++] = (char)ch;
53 if ((char)ch == '\n')
54 break;
55 }
56
57 if (idx == 0)
58 return NULL;
59
60 linebuf[idx] = 0;
61 return linebuf;
62}
63
64
65/* END CODE */
66/*
67Local Variables:
68c-file-style: "linux"
69c-basic-offset: 4
70tab-width: 4
71End:
72*/
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
new file mode 100644
index 000000000..ff2175175
--- /dev/null
+++ b/libbb/human_readable.c
@@ -0,0 +1,62 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32
33const char *make_human_readable_str(unsigned long val, unsigned long hr)
34{
35 int i=0;
36 static char str[10] = "\0";
37 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
38 unsigned long divisor = 1;
39
40 if(val == 0)
41 return("0");
42 if(hr)
43 snprintf(str, 9, "%ld", val/hr);
44 else {
45 while(val >= divisor && i <= 4) {
46 divisor=divisor<<10, i++;
47 }
48 divisor=divisor>>10, i--;
49 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
50 }
51 return(str);
52}
53
54
55/* END CODE */
56/*
57Local Variables:
58c-file-style: "linux"
59c-basic-offset: 4
60tab-width: 4
61End:
62*/
diff --git a/libbb/inode_hash.c b/libbb/inode_hash.c
new file mode 100644
index 000000000..db68025dd
--- /dev/null
+++ b/libbb/inode_hash.c
@@ -0,0 +1,105 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include "libbb.h"
31
32#define HASH_SIZE 311 /* Should be prime */
33#define hash_inode(i) ((i) % HASH_SIZE)
34
35static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
36
37/*
38 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
39 * `ino_dev_hashtable', else return 0
40 *
41 * If NAME is a non-NULL pointer to a character pointer, and there is
42 * a match, then set *NAME to the value of the name slot in that
43 * bucket.
44 */
45int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
46{
47 ino_dev_hashtable_bucket_t *bucket;
48
49 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
50 while (bucket != NULL) {
51 if ((bucket->ino == statbuf->st_ino) &&
52 (bucket->dev == statbuf->st_dev))
53 {
54 if (name) *name = bucket->name;
55 return 1;
56 }
57 bucket = bucket->next;
58 }
59 return 0;
60}
61
62/* Add statbuf to statbuf hash table */
63void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
64{
65 int i;
66 size_t s;
67 ino_dev_hashtable_bucket_t *bucket;
68
69 i = hash_inode(statbuf->st_ino);
70 s = name ? strlen(name) : 0;
71 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
72 bucket->ino = statbuf->st_ino;
73 bucket->dev = statbuf->st_dev;
74 if (name)
75 strcpy(bucket->name, name);
76 else
77 bucket->name[0] = '\0';
78 bucket->next = ino_dev_hashtable[i];
79 ino_dev_hashtable[i] = bucket;
80}
81
82/* Clear statbuf hash table */
83void reset_ino_dev_hashtable(void)
84{
85 int i;
86 ino_dev_hashtable_bucket_t *bucket;
87
88 for (i = 0; i < HASH_SIZE; i++) {
89 while (ino_dev_hashtable[i] != NULL) {
90 bucket = ino_dev_hashtable[i]->next;
91 free(ino_dev_hashtable[i]);
92 ino_dev_hashtable[i] = bucket;
93 }
94 }
95}
96
97
98/* END CODE */
99/*
100Local Variables:
101c-file-style: "linux"
102c-basic-offset: 4
103tab-width: 4
104End:
105*/
diff --git a/libbb/isdirectory.c b/libbb/isdirectory.c
new file mode 100644
index 000000000..65f4fee00
--- /dev/null
+++ b/libbb/isdirectory.c
@@ -0,0 +1,71 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <sys/stat.h>
31#include "libbb.h"
32
33/*
34 * Return TRUE if a fileName is a directory.
35 * Nonexistant files return FALSE.
36 */
37int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
38{
39 int status;
40 int didMalloc = 0;
41
42 if (statBuf == NULL) {
43 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
44 ++didMalloc;
45 }
46
47 if (followLinks == TRUE)
48 status = stat(fileName, statBuf);
49 else
50 status = lstat(fileName, statBuf);
51
52 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
53 status = FALSE;
54 }
55 else status = TRUE;
56
57 if (didMalloc) {
58 free(statBuf);
59 statBuf = NULL;
60 }
61 return status;
62}
63
64/* END CODE */
65/*
66Local Variables:
67c-file-style: "linux"
68c-basic-offset: 4
69tab-width: 4
70End:
71*/
diff --git a/libbb/kernel_version.c b/libbb/kernel_version.c
new file mode 100644
index 000000000..09cd582c4
--- /dev/null
+++ b/libbb/kernel_version.c
@@ -0,0 +1,66 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <sys/utsname.h> /* for uname(2) */
32
33#include "libbb.h"
34
35/* Returns kernel version encoded as major*65536 + minor*256 + patch,
36 * so, for example, to check if the kernel is greater than 2.2.11:
37 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
38 */
39extern int get_kernel_revision(void)
40{
41 struct utsname name;
42 char *s;
43 int i, r;
44
45 if (uname(&name) == -1) {
46 perror_msg("cannot get system information");
47 return (0);
48 }
49
50 s = name.release;
51 r = 0;
52 for (i=0 ; i<3 ; i++) {
53 r = r * 256 + atoi(strtok(s, "."));
54 s = NULL;
55 }
56 return r;
57}
58
59/* END CODE */
60/*
61Local Variables:
62c-file-style: "linux"
63c-basic-offset: 4
64tab-width: 4
65End:
66*/
diff --git a/libbb/libbb.h b/libbb/libbb.h
new file mode 100644
index 000000000..a85987d9e
--- /dev/null
+++ b/libbb/libbb.h
@@ -0,0 +1,209 @@
1/* 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 __LIBBB_H__
25#define __LIBBB_H__ 1
26
27#include <stdio.h>
28#include <stdarg.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31
32#ifdef DMALLOC
33#include "dmalloc.h"
34#endif
35
36#include <features.h>
37/* Stupid libc doesn't have a reliable way for use to know
38 * that libc5 is being used. Assume this is good enough */
39#if ! defined __GLIBC__ && ! defined __UCLIBC__
40/* libc5 doesn't define socklen_t */
41typedef unsigned int socklen_t;
42#endif
43
44/* Some useful definitions */
45#define FALSE ((int) 0)
46#define TRUE ((int) 1)
47#define SKIP ((int) 2)
48
49/* for mtab.c */
50#define MTAB_GETMOUNTPT '1'
51#define MTAB_GETDEVICE '2'
52
53#define BUF_SIZE 8192
54#define EXPAND_ALLOC 1024
55
56static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); }
57static inline int is_octal(ch) { return ((ch >= '0') && (ch <= '7')); }
58
59/* Macros for min/max. */
60#ifndef MIN
61#define MIN(a,b) (((a)<(b))?(a):(b))
62#endif
63
64#ifndef MAX
65#define MAX(a,b) (((a)>(b))?(a):(b))
66#endif
67
68
69
70extern void show_usage(void) __attribute__ ((noreturn));
71extern void error_msg(const char *s, ...);
72extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
73extern void perror_msg(const char *s, ...);
74extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
75
76const char *mode_string(int mode);
77const char *time_string(time_t timeVal);
78int is_directory(const char *name, const int followLinks, struct stat *statBuf);
79int isDevice(const char *name);
80
81typedef struct ino_dev_hash_bucket_struct {
82 struct ino_dev_hash_bucket_struct *next;
83 ino_t ino;
84 dev_t dev;
85 char name[1];
86} ino_dev_hashtable_bucket_t;
87int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
88void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
89void reset_ino_dev_hashtable(void);
90
91int copy_file(const char *srcName, const char *destName,
92 int setModes, int followLinks, int forceFlag);
93int copy_file_chunk(int srcFd, int dstFd, size_t remaining);
94char *buildName(const char *dirName, const char *fileName);
95int makeString(int argc, const char **argv, char *buf, int bufLen);
96char *getChunk(int size);
97char *chunkstrdup(const char *str);
98void freeChunks(void);
99ssize_t safe_read(int fd, void *buf, size_t count);
100int full_write(int fd, const char *buf, int len);
101int full_read(int fd, char *buf, int len);
102int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
103 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
104 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
105 void* userData);
106
107extern int create_path (const char *name, int mode);
108extern int parse_mode( const char* s, mode_t* theMode);
109
110extern int get_kernel_revision(void);
111
112extern int get_console_fd(char* tty_name);
113extern struct mntent *find_mount_point(const char *name, const char *table);
114extern void write_mtab(char* blockDevice, char* directory,
115 char* filesystemType, long flags, char* string_flags);
116extern void erase_mtab(const char * name);
117extern void mtab_read(void);
118extern char *mtab_first(void **iter);
119extern char *mtab_next(void **iter);
120extern char *mtab_getinfo(const char *match, const char which);
121extern int check_wildcard_match(const char* text, const char* pattern);
122extern long atoi_w_units (const char *cp);
123extern pid_t* find_pid_by_name( char* pidName);
124extern int find_real_root_device_name(char* name);
125extern char *get_line_from_file(FILE *file);
126extern void print_file(FILE *file);
127extern int print_file_by_name(char *filename);
128extern char process_escape_sequence(char **ptr);
129extern char *get_last_path_component(char *path);
130extern FILE *wfopen(const char *path, const char *mode);
131extern FILE *xfopen(const char *path, const char *mode);
132extern void chomp(char *s);
133extern void trim(char *s);
134extern struct BB_applet *find_applet_by_name(const char *name);
135void run_applet_by_name(const char *name, int argc, char **argv);
136
137#ifndef DMALLOC
138extern void *xmalloc (size_t size);
139extern void *xrealloc(void *old, size_t size);
140extern void *xcalloc(size_t nmemb, size_t size);
141extern char *xstrdup (const char *s);
142#endif
143extern char *xstrndup (const char *s, int n);
144extern char * safe_strncpy(char *dst, const char *src, size_t size);
145
146struct suffix_mult {
147 char *suffix;
148 int mult;
149};
150
151extern unsigned long parse_number(const char *numstr,
152 const struct suffix_mult *suffixes);
153
154
155/* These parse entries in /etc/passwd and /etc/group. This is desirable
156 * for BusyBox since we want to avoid using the glibc NSS stuff, which
157 * increases target size and is often not needed embedded systems. */
158extern long my_getpwnam(const char *name);
159extern long my_getgrnam(const char *name);
160extern void my_getpwuid(char *name, long uid);
161extern void my_getgrgid(char *group, long gid);
162extern long my_getpwnamegid(const char *name);
163
164extern int device_open(char *device, int mode);
165
166extern int del_loop(const char *device);
167extern int set_loop(const char *device, const char *file, int offset, int *loopro);
168extern char *find_unused_loop_device (void);
169
170
171#if (__GLIBC__ < 2)
172extern int vdprintf(int d, const char *format, va_list ap);
173#endif
174
175int nfsmount(const char *spec, const char *node, int *flags,
176 char **extra_opts, char **mount_opts, int running_bg);
177
178void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
179void syslog_msg(int facility, int pri, const char *msg);
180
181/* Include our own copy of struct sysinfo to avoid binary compatability
182 * problems with Linux 2.4, which changed things. Grumble, grumble. */
183struct sysinfo {
184 long uptime; /* Seconds since boot */
185 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
186 unsigned long totalram; /* Total usable main memory size */
187 unsigned long freeram; /* Available memory size */
188 unsigned long sharedram; /* Amount of shared memory */
189 unsigned long bufferram; /* Memory used by buffers */
190 unsigned long totalswap; /* Total swap space size */
191 unsigned long freeswap; /* swap space still available */
192 unsigned short procs; /* Number of current processes */
193 unsigned long totalhigh; /* Total high memory size */
194 unsigned long freehigh; /* Available high memory size */
195 unsigned int mem_unit; /* Memory unit size in bytes */
196 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
197};
198extern int sysinfo (struct sysinfo* info);
199
200const char *make_human_readable_str(unsigned long val, unsigned long hr);
201enum {
202 KILOBYTE = 1024,
203 MEGABYTE = (KILOBYTE*1024),
204 GIGABYTE = (MEGABYTE*1024)
205};
206
207int ask_confirmation(void);
208
209#endif /* __LIBBB_H__ */
diff --git a/libbb/loop.c b/libbb/loop.c
new file mode 100644
index 000000000..4754b8da1
--- /dev/null
+++ b/libbb/loop.c
@@ -0,0 +1,128 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <string.h>
32#include <unistd.h>
33#include <sys/ioctl.h>
34#include "libbb.h"
35#include "loop.h" /* Pull in loop device support */
36
37extern int del_loop(const char *device)
38{
39 int fd;
40
41 if ((fd = open(device, O_RDONLY)) < 0) {
42 perror_msg("%s", device);
43 return (FALSE);
44 }
45 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
46 perror_msg("ioctl: LOOP_CLR_FD");
47 return (FALSE);
48 }
49 close(fd);
50 return (TRUE);
51}
52
53extern int set_loop(const char *device, const char *file, int offset,
54 int *loopro)
55{
56 struct loop_info loopinfo;
57 int fd, ffd, mode;
58
59 mode = *loopro ? O_RDONLY : O_RDWR;
60 if ((ffd = open(file, mode)) < 0 && !*loopro
61 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
62 perror_msg("%s", file);
63 return 1;
64 }
65 if ((fd = open(device, mode)) < 0) {
66 close(ffd);
67 perror_msg("%s", device);
68 return 1;
69 }
70 *loopro = (mode == O_RDONLY);
71
72 memset(&loopinfo, 0, sizeof(loopinfo));
73 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
74
75 loopinfo.lo_offset = offset;
76
77 loopinfo.lo_encrypt_key_size = 0;
78 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
79 perror_msg("ioctl: LOOP_SET_FD");
80 close(fd);
81 close(ffd);
82 return 1;
83 }
84 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
85 (void) ioctl(fd, LOOP_CLR_FD, 0);
86 perror_msg("ioctl: LOOP_SET_STATUS");
87 close(fd);
88 close(ffd);
89 return 1;
90 }
91 close(fd);
92 close(ffd);
93 return 0;
94}
95
96extern char *find_unused_loop_device(void)
97{
98 char dev[20];
99 int i, fd;
100 struct stat statbuf;
101 struct loop_info loopinfo;
102
103 for (i = 0; i <= 7; i++) {
104 sprintf(dev, "/dev/loop%d", i);
105 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
106 if ((fd = open(dev, O_RDONLY)) >= 0) {
107 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
108 if (errno == ENXIO) { /* probably free */
109 close(fd);
110 return strdup(dev);
111 }
112 }
113 close(fd);
114 }
115 }
116 }
117 return NULL;
118}
119
120
121/* END CODE */
122/*
123Local Variables:
124c-file-style: "linux"
125c-basic-offset: 4
126tab-width: 4
127End:
128*/
diff --git a/libbb/mode_string.c b/libbb/mode_string.c
new file mode 100644
index 000000000..0a3d6e6f0
--- /dev/null
+++ b/libbb/mode_string.c
@@ -0,0 +1,82 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32
33#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
34#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
35
36/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
37static const mode_t SBIT[] = {
38 0, 0, S_ISUID,
39 0, 0, S_ISGID,
40 0, 0, S_ISVTX
41};
42
43/* The 9 mode bits to test */
44static const mode_t MBIT[] = {
45 S_IRUSR, S_IWUSR, S_IXUSR,
46 S_IRGRP, S_IWGRP, S_IXGRP,
47 S_IROTH, S_IWOTH, S_IXOTH
48};
49
50static const char MODE1[] = "rwxrwxrwx";
51static const char MODE0[] = "---------";
52static const char SMODE1[] = "..s..s..t";
53static const char SMODE0[] = "..S..S..T";
54
55/*
56 * Return the standard ls-like mode string from a file mode.
57 * This is static and so is overwritten on each call.
58 */
59const char *mode_string(int mode)
60{
61 static char buf[12];
62
63 int i;
64
65 buf[0] = TYPECHAR(mode);
66 for (i = 0; i < 9; i++) {
67 if (mode & SBIT[i])
68 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
69 else
70 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
71 }
72 return buf;
73}
74
75/* END CODE */
76/*
77Local Variables:
78c-file-style: "linux"
79c-basic-offset: 4
80tab-width: 4
81End:
82*/
diff --git a/libbb/parse_mode.c b/libbb/parse_mode.c
new file mode 100644
index 000000000..33a878122
--- /dev/null
+++ b/libbb/parse_mode.c
@@ -0,0 +1,138 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include "libbb.h"
31
32
33/* This function parses the sort of string you might pass
34 * to chmod (i.e. [ugoa]{+|-|=}[rwxst] ) and returns the
35 * correct mode described by the string. */
36extern int parse_mode(const char *s, mode_t * theMode)
37{
38 static const mode_t group_set[] = {
39 S_ISUID | S_IRWXU, /* u */
40 S_ISGID | S_IRWXG, /* g */
41 S_IRWXO, /* o */
42 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
43 };
44
45 static const mode_t mode_set[] = {
46 S_IRUSR | S_IRGRP | S_IROTH, /* r */
47 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
48 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
49 S_ISUID | S_ISGID, /* s */
50 S_ISVTX /* t */
51 };
52
53 static const char group_string[] = "ugoa";
54 static const char mode_string[] = "rwxst";
55
56 const char *p;
57
58 mode_t andMode =
59 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
60 mode_t orMode = 0;
61 mode_t mode;
62 mode_t groups;
63 char type;
64 char c;
65
66 if (s==NULL) {
67 return (FALSE);
68 }
69
70 do {
71 mode = 0;
72 groups = 0;
73 NEXT_GROUP:
74 if ((c = *s++) == '\0') {
75 return -1;
76 }
77 for (p=group_string ; *p ; p++) {
78 if (*p == c) {
79 groups |= group_set[(int)(p-group_string)];
80 goto NEXT_GROUP;
81 }
82 }
83 switch (c) {
84 case '=':
85 case '+':
86 case '-':
87 type = c;
88 if (groups == 0) { /* The default is "all" */
89 groups |= S_ISUID | S_ISGID | S_ISVTX
90 | S_IRWXU | S_IRWXG | S_IRWXO;
91 }
92 break;
93 default:
94 if ((c < '0') || (c > '7') || (mode | groups)) {
95 return (FALSE);
96 } else {
97 *theMode = strtol(--s, NULL, 8);
98 return (TRUE);
99 }
100 }
101
102 NEXT_MODE:
103 if (((c = *s++) != '\0') && (c != ',')) {
104 for (p=mode_string ; *p ; p++) {
105 if (*p == c) {
106 mode |= mode_set[(int)(p-mode_string)];
107 goto NEXT_MODE;
108 }
109 }
110 break; /* We're done so break out of loop.*/
111 }
112 switch (type) {
113 case '=':
114 andMode &= ~(groups); /* Now fall through. */
115 case '+':
116 orMode |= mode & groups;
117 break;
118 case '-':
119 andMode &= ~(mode & groups);
120 orMode &= ~(mode & groups);
121 break;
122 }
123 } while (c == ',');
124
125 *theMode &= andMode;
126 *theMode |= orMode;
127
128 return TRUE;
129}
130
131/* END CODE */
132/*
133Local Variables:
134c-file-style: "linux"
135c-basic-offset: 4
136tab-width: 4
137End:
138*/
diff --git a/libbb/parse_number.c b/libbb/parse_number.c
new file mode 100644
index 000000000..c90511dca
--- /dev/null
+++ b/libbb/parse_number.c
@@ -0,0 +1,74 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include "libbb.h"
32
33
34unsigned long parse_number(const char *numstr,
35 const struct suffix_mult *suffixes)
36{
37 const struct suffix_mult *sm;
38 unsigned long int ret;
39 int len;
40 char *end;
41
42 ret = strtoul(numstr, &end, 10);
43 if (numstr == end)
44 error_msg_and_die("invalid number `%s'", numstr);
45 while (end[0] != '\0') {
46 sm = suffixes;
47 while ( sm != 0 ) {
48 if(sm->suffix) {
49 len = strlen(sm->suffix);
50 if (strncmp(sm->suffix, end, len) == 0) {
51 ret *= sm->mult;
52 end += len;
53 break;
54 }
55 sm++;
56
57 } else
58 sm = 0;
59 }
60 if (sm == 0)
61 error_msg_and_die("invalid number `%s'", numstr);
62 }
63 return ret;
64}
65
66
67/* END CODE */
68/*
69Local Variables:
70c-file-style: "linux"
71c-basic-offset: 4
72tab-width: 4
73End:
74*/
diff --git a/libbb/print_file.c b/libbb/print_file.c
new file mode 100644
index 000000000..52a39774f
--- /dev/null
+++ b/libbb/print_file.c
@@ -0,0 +1,59 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32extern void print_file(FILE *file)
33{
34 int c;
35
36 while ((c = getc(file)) != EOF)
37 putc(c, stdout);
38 fclose(file);
39 fflush(stdout);
40}
41
42extern int print_file_by_name(char *filename)
43{
44 FILE *file;
45 if ((file = wfopen(filename, "r")) == NULL)
46 return FALSE;
47 print_file(file);
48 return TRUE;
49}
50
51
52/* END CODE */
53/*
54Local Variables:
55c-file-style: "linux"
56c-basic-offset: 4
57tab-width: 4
58End:
59*/
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c
new file mode 100644
index 000000000..ad2be94ee
--- /dev/null
+++ b/libbb/process_escape_sequence.c
@@ -0,0 +1,83 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <limits.h>
30#include "libbb.h"
31
32
33
34char process_escape_sequence(char **ptr)
35{
36 static const char charmap[] = {
37 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
38 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
39
40 const char *p;
41 char *q;
42 int num_digits;
43 unsigned int n;
44
45 n = 0;
46 q = *ptr;
47
48 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
49 if ((*q < '0') || (*q > '7')) { /* not a digit? */
50 break;
51 }
52 n = n * 8 + (*q++ - '0');
53 }
54
55 if (num_digits == 0) { /* mnemonic escape sequence? */
56 for (p=charmap ; *p ; p++) {
57 if (*p == *q) {
58 q++;
59 break;
60 }
61 }
62 n = *(p+(sizeof(charmap)/2));
63 }
64
65 /* doesn't hurt to fall through to here from mnemonic case */
66 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
67 n /= 8; /* adjust value and */
68 --q; /* back up one char */
69 }
70
71 *ptr = q;
72 return (char) n;
73}
74
75
76/* END CODE */
77/*
78Local Variables:
79c-file-style: "linux"
80c-basic-offset: 4
81tab-width: 4
82End:
83*/
diff --git a/libbb/pwd_grp_wrappers.c b/libbb/pwd_grp_wrappers.c
new file mode 100644
index 000000000..e7b7e3820
--- /dev/null
+++ b/libbb/pwd_grp_wrappers.c
@@ -0,0 +1,109 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include "../pwd_grp/pwd.h"
31#include "../pwd_grp/grp.h"
32#include "libbb.h"
33
34
35
36/* returns a uid given a username */
37long my_getpwnam(const char *name)
38{
39 struct passwd *myuser;
40
41 myuser = getpwnam(name);
42 if (myuser==NULL)
43 error_msg_and_die("unknown user name: %s", name);
44
45 return myuser->pw_uid;
46}
47
48/* returns a gid given a group name */
49long my_getgrnam(const char *name)
50{
51 struct group *mygroup;
52
53 mygroup = getgrnam(name);
54 if (mygroup==NULL)
55 error_msg_and_die("unknown group name: %s", name);
56
57 return (mygroup->gr_gid);
58}
59
60/* gets a username given a uid */
61void my_getpwuid(char *name, long uid)
62{
63 struct passwd *myuser;
64
65 myuser = getpwuid(uid);
66 if (myuser==NULL)
67 sprintf(name, "%-8ld ", (long)uid);
68 else
69 strcpy(name, myuser->pw_name);
70}
71
72/* gets a groupname given a gid */
73void my_getgrgid(char *group, long gid)
74{
75 struct group *mygroup;
76
77 mygroup = getgrgid(gid);
78 if (mygroup==NULL)
79 sprintf(group, "%-8ld ", (long)gid);
80 else
81 strcpy(group, mygroup->gr_name);
82}
83
84/* gets a gid given a user name */
85long my_getpwnamegid(const char *name)
86{
87 struct group *mygroup;
88 struct passwd *myuser;
89
90 myuser=getpwnam(name);
91 if (myuser==NULL)
92 error_msg_and_die("unknown user name: %s", name);
93
94 mygroup = getgrgid(myuser->pw_gid);
95 if (mygroup==NULL)
96 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
97
98 return mygroup->gr_gid;
99}
100
101
102/* END CODE */
103/*
104Local Variables:
105c-file-style: "linux"
106c-basic-offset: 4
107tab-width: 4
108End:
109*/
diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c
new file mode 100644
index 000000000..6b93340be
--- /dev/null
+++ b/libbb/recursive_action.c
@@ -0,0 +1,160 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <dirent.h>
30#include <sys/stat.h>
31#include "libbb.h"
32
33/* same conditions as recursive_action */
34#define bb_need_name_too_long
35#define BB_DECLARE_EXTERN
36#include "../messages.c"
37
38#undef DEBUG_RECURS_ACTION
39
40
41/*
42 * Walk down all the directories under the specified
43 * location, and do something (something specified
44 * by the fileAction and dirAction function pointers).
45 *
46 * Unfortunately, while nftw(3) could replace this and reduce
47 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
48 * and so isn't sufficiently portable to take over since glibc2.1
49 * is so stinking huge.
50 */
51int recursive_action(const char *fileName,
52 int recurse, int followLinks, int depthFirst,
53 int (*fileAction) (const char *fileName,
54 struct stat * statbuf,
55 void* userData),
56 int (*dirAction) (const char *fileName,
57 struct stat * statbuf,
58 void* userData),
59 void* userData)
60{
61 int status;
62 struct stat statbuf;
63 struct dirent *next;
64
65 if (followLinks == TRUE)
66 status = stat(fileName, &statbuf);
67 else
68 status = lstat(fileName, &statbuf);
69
70 if (status < 0) {
71#ifdef DEBUG_RECURS_ACTION
72 fprintf(stderr,
73 "status=%d followLinks=%d TRUE=%d\n",
74 status, followLinks, TRUE);
75#endif
76 perror_msg("%s", fileName);
77 return FALSE;
78 }
79
80 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
81 if (fileAction == NULL)
82 return TRUE;
83 else
84 return fileAction(fileName, &statbuf, userData);
85 }
86
87 if (recurse == FALSE) {
88 if (S_ISDIR(statbuf.st_mode)) {
89 if (dirAction != NULL)
90 return (dirAction(fileName, &statbuf, userData));
91 else
92 return TRUE;
93 }
94 }
95
96 if (S_ISDIR(statbuf.st_mode)) {
97 DIR *dir;
98
99 if (dirAction != NULL && depthFirst == FALSE) {
100 status = dirAction(fileName, &statbuf, userData);
101 if (status == FALSE) {
102 perror_msg("%s", fileName);
103 return FALSE;
104 } else if (status == SKIP)
105 return TRUE;
106 }
107 dir = opendir(fileName);
108 if (!dir) {
109 perror_msg("%s", fileName);
110 return FALSE;
111 }
112 status = TRUE;
113 while ((next = readdir(dir)) != NULL) {
114 char nextFile[PATH_MAX];
115
116 if ((strcmp(next->d_name, "..") == 0)
117 || (strcmp(next->d_name, ".") == 0)) {
118 continue;
119 }
120 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
121 error_msg(name_too_long);
122 return FALSE;
123 }
124 memset(nextFile, 0, sizeof(nextFile));
125 if (fileName[strlen(fileName)-1] == '/')
126 sprintf(nextFile, "%s%s", fileName, next->d_name);
127 else
128 sprintf(nextFile, "%s/%s", fileName, next->d_name);
129 if (recursive_action(nextFile, TRUE, followLinks, depthFirst,
130 fileAction, dirAction, userData) == FALSE) {
131 status = FALSE;
132 }
133 }
134 closedir(dir);
135 if (dirAction != NULL && depthFirst == TRUE) {
136 if (dirAction(fileName, &statbuf, userData) == FALSE) {
137 perror_msg("%s", fileName);
138 return FALSE;
139 }
140 }
141 if (status == FALSE)
142 return FALSE;
143 } else {
144 if (fileAction == NULL)
145 return TRUE;
146 else
147 return fileAction(fileName, &statbuf, userData);
148 }
149 return TRUE;
150}
151
152
153/* END CODE */
154/*
155Local Variables:
156c-file-style: "linux"
157c-basic-offset: 4
158tab-width: 4
159End:
160*/
diff --git a/libbb/safe_read.c b/libbb/safe_read.c
new file mode 100644
index 000000000..dbf4aa7e4
--- /dev/null
+++ b/libbb/safe_read.c
@@ -0,0 +1,54 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <unistd.h>
31#include "libbb.h"
32
33
34
35ssize_t safe_read(int fd, void *buf, size_t count)
36{
37 ssize_t n;
38
39 do {
40 n = read(fd, buf, count);
41 } while (n < 0 && errno == EINTR);
42
43 return n;
44}
45
46
47/* END CODE */
48/*
49Local Variables:
50c-file-style: "linux"
51c-basic-offset: 4
52tab-width: 4
53End:
54*/
diff --git a/libbb/safe_strncpy.c b/libbb/safe_strncpy.c
new file mode 100644
index 000000000..55ec79802
--- /dev/null
+++ b/libbb/safe_strncpy.c
@@ -0,0 +1,48 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <string.h>
29#include "libbb.h"
30
31
32
33/* Like strncpy but make sure the resulting string is always 0 terminated. */
34extern char * safe_strncpy(char *dst, const char *src, size_t size)
35{
36 dst[size-1] = '\0';
37 return strncpy(dst, src, size-1);
38}
39
40
41/* END CODE */
42/*
43Local Variables:
44c-file-style: "linux"
45c-basic-offset: 4
46tab-width: 4
47End:
48*/
diff --git a/libbb/syscalls.c b/libbb/syscalls.c
new file mode 100644
index 000000000..021154602
--- /dev/null
+++ b/libbb/syscalls.c
@@ -0,0 +1,76 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <sys/syscall.h>
31#include <linux/unistd.h>
32
33#include "libbb.h"
34
35
36_syscall1(int, sysinfo, struct sysinfo *, info);
37
38/* Include our own version of <sys/mount.h>, since libc5 doesn't
39 * know about umount2 */
40extern _syscall1(int, umount, const char *, special_file);
41extern _syscall5(int, mount, const char *, special_file, const char *, dir,
42 const char *, fstype, unsigned long int, rwflag, const void *, data);
43
44#ifndef __NR_umount2
45# warning This kernel does not support the umount2 syscall
46# warning The umount2 system call is being stubbed out...
47int umount2(const char * special_file, int flags)
48{
49 /* BusyBox was compiled against a kernel that did not support
50 * the umount2 system call. To make this application work,
51 * you will need to recompile with a kernel supporting the
52 * umount2 system call.
53 */
54 fprintf(stderr, "\n\nTo make this application work, you will need to recompile\n");
55 fprintf(stderr, "with a kernel supporting the umount2 system call. -Erik\n\n");
56 errno=ENOSYS;
57 return -1;
58}
59# else
60extern _syscall2(int, umount2, const char *, special_file, int, flags);
61#endif
62
63#ifndef __NR_query_module
64static const int __NR_query_module = 167;
65#endif
66_syscall5(int, query_module, const char *, name, int, which,
67 void *, buf, size_t, bufsize, size_t*, ret);
68
69/* END CODE */
70/*
71Local Variables:
72c-file-style: "linux"
73c-basic-offset: 4
74tab-width: 4
75End:
76*/
diff --git a/libbb/syslog_msg_with_name.c b/libbb/syslog_msg_with_name.c
new file mode 100644
index 000000000..d72755a37
--- /dev/null
+++ b/libbb/syslog_msg_with_name.c
@@ -0,0 +1,53 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <sys/syslog.h>
30#include "libbb.h"
31
32extern const char *applet_name;
33
34void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg)
35{
36 openlog(name, 0, facility);
37 syslog(pri, "%s", msg);
38 closelog();
39}
40
41void syslog_msg(int facility, int pri, const char *msg)
42{
43 syslog_msg_with_name(applet_name, facility, pri, msg);
44}
45
46/* END CODE */
47/*
48Local Variables:
49c-file-style: "linux"
50c-basic-offset: 4
51tab-width: 4
52End:
53*/
diff --git a/libbb/time_string.c b/libbb/time_string.c
new file mode 100644
index 000000000..076529006
--- /dev/null
+++ b/libbb/time_string.c
@@ -0,0 +1,68 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <time.h>
31#include <utime.h>
32#include "libbb.h"
33
34
35/*
36 * Return the standard ls-like time string from a time_t
37 * This is static and so is overwritten on each call.
38 */
39const char *time_string(time_t timeVal)
40{
41 time_t now;
42 char *str;
43 static char buf[26];
44
45 time(&now);
46
47 str = ctime(&timeVal);
48
49 strcpy(buf, &str[4]);
50 buf[12] = '\0';
51
52 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
53 strcpy(&buf[7], &str[20]);
54 buf[11] = '\0';
55 }
56
57 return buf;
58}
59
60
61/* END CODE */
62/*
63Local Variables:
64c-file-style: "linux"
65c-basic-offset: 4
66tab-width: 4
67End:
68*/
diff --git a/libbb/trim.c b/libbb/trim.c
new file mode 100644
index 000000000..8a9b07a14
--- /dev/null
+++ b/libbb/trim.c
@@ -0,0 +1,52 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <ctype.h>
31#include "libbb.h"
32
33
34void trim(char *s)
35{
36 /* trim trailing whitespace */
37 while (isspace(s[strlen(s)-1]))
38 s[strlen(s)-1]='\0';
39
40 /* trim leading whitespace */
41 memmove(s, &s[strspn(s, " \n\r\t\v")], strlen(s));
42
43}
44
45/* END CODE */
46/*
47Local Variables:
48c-file-style: "linux"
49c-basic-offset: 4
50tab-width: 4
51End:
52*/
diff --git a/libbb/vdprintf.c b/libbb/vdprintf.c
new file mode 100644
index 000000000..a18291b3b
--- /dev/null
+++ b/libbb/vdprintf.c
@@ -0,0 +1,52 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30
31
32
33#if (__GLIBC__ < 2)
34extern int vdprintf(int d, const char *format, va_list ap)
35{
36 char buf[BUF_SIZE];
37 int len;
38
39 len = vsprintf(buf, format, ap);
40 return write(d, buf, len);
41}
42#endif
43
44
45/* END CODE */
46/*
47Local Variables:
48c-file-style: "linux"
49c-basic-offset: 4
50tab-width: 4
51End:
52*/
diff --git a/libbb/wfopen.c b/libbb/wfopen.c
new file mode 100644
index 000000000..8b074d2f7
--- /dev/null
+++ b/libbb/wfopen.c
@@ -0,0 +1,50 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include "libbb.h"
31
32FILE *wfopen(const char *path, const char *mode)
33{
34 FILE *fp;
35 if ((fp = fopen(path, mode)) == NULL) {
36 perror_msg("%s", path);
37 errno = 0;
38 }
39 return fp;
40}
41
42
43/* END CODE */
44/*
45Local Variables:
46c-file-style: "linux"
47c-basic-offset: 4
48tab-width: 4
49End:
50*/
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
new file mode 100644
index 000000000..e7f41aae2
--- /dev/null
+++ b/libbb/xfuncs.c
@@ -0,0 +1,107 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include "libbb.h"
33
34/* same conditions as recursive_action */
35#define bb_need_memory_exhausted
36#define BB_DECLARE_EXTERN
37#include "../messages.c"
38
39
40#ifndef DMALLOC
41extern void *xmalloc(size_t size)
42{
43 void *ptr = malloc(size);
44
45 if (!ptr)
46 error_msg_and_die(memory_exhausted);
47 return ptr;
48}
49
50extern void *xrealloc(void *old, size_t size)
51{
52 void *ptr = realloc(old, size);
53 if (!ptr)
54 error_msg_and_die(memory_exhausted);
55 return ptr;
56}
57
58extern void *xcalloc(size_t nmemb, size_t size)
59{
60 void *ptr = calloc(nmemb, size);
61 if (!ptr)
62 error_msg_and_die(memory_exhausted);
63 return ptr;
64}
65
66extern char * xstrdup (const char *s) {
67 char *t;
68
69 if (s == NULL)
70 return NULL;
71
72 t = strdup (s);
73
74 if (t == NULL)
75 error_msg_and_die(memory_exhausted);
76
77 return t;
78}
79#endif
80
81extern char * xstrndup (const char *s, int n) {
82 char *t;
83
84 if (s == NULL)
85 error_msg_and_die("xstrndup bug");
86
87 t = xmalloc(++n);
88
89 return safe_strncpy(t,s,n);
90}
91
92FILE *xfopen(const char *path, const char *mode)
93{
94 FILE *fp;
95 if ((fp = fopen(path, mode)) == NULL)
96 perror_msg_and_die("%s", path);
97 return fp;
98}
99
100/* END CODE */
101/*
102Local Variables:
103c-file-style: "linux"
104c-basic-offset: 4
105tab-width: 4
106End:
107*/
diff --git a/libbb/xregcomp.c b/libbb/xregcomp.c
new file mode 100644
index 000000000..6f5e2f0cb
--- /dev/null
+++ b/libbb/xregcomp.c
@@ -0,0 +1,53 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "libbb.h"
30#include <regex.h>
31
32
33
34void xregcomp(regex_t *preg, const char *regex, int cflags)
35{
36 int ret;
37 if ((ret = regcomp(preg, regex, cflags)) != 0) {
38 int errmsgsz = regerror(ret, preg, NULL, 0);
39 char *errmsg = xmalloc(errmsgsz);
40 regerror(ret, preg, errmsg, errmsgsz);
41 error_msg_and_die("xregcomp: %s", errmsg);
42 }
43}
44
45
46/* END CODE */
47/*
48Local Variables:
49c-file-style: "linux"
50c-basic-offset: 4
51tab-width: 4
52End:
53*/
diff --git a/mtab_file.c b/mtab_file.c
new file mode 100644
index 000000000..3235e35d6
--- /dev/null
+++ b/mtab_file.c
@@ -0,0 +1,52 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include "busybox.h"
30
31
32/* Busybox mount uses either /proc/mounts or /dev/mtab to
33 * get the list of currently mounted filesystems */
34#if defined BB_MTAB
35const char mtab_file[] = "/etc/mtab";
36#else
37# if defined BB_FEATURE_USE_DEVPS_PATCH
38 const char mtab_file[] = "/dev/mtab";
39# else
40 const char mtab_file[] = "/proc/mounts";
41# endif
42#endif
43
44
45/* END CODE */
46/*
47Local Variables:
48c-file-style: "linux"
49c-basic-offset: 4
50tab-width: 4
51End:
52*/
diff --git a/utility.c b/utility.c
deleted file mode 100644
index 8408a6cfa..000000000
--- a/utility.c
+++ /dev/null
@@ -1,1887 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <dirent.h>
33#include <time.h>
34#include <utime.h>
35#include <unistd.h>
36#include <ctype.h>
37#include <stdlib.h>
38#include <limits.h>
39#include <stdarg.h>
40#include <sys/ioctl.h>
41#include <sys/utsname.h> /* for uname(2) */
42
43#include "busybox.h"
44#if defined (BB_CHMOD_CHOWN_CHGRP) \
45 || defined (BB_CP_MV) \
46 || defined (BB_FIND) \
47 || defined (BB_INSMOD) \
48 || defined (BB_LS) \
49 || defined (BB_RM) \
50 || defined (BB_TAR)
51/* same conditions as recursive_action */
52#define bb_need_name_too_long
53#endif
54#define bb_need_memory_exhausted
55#define bb_need_full_version
56#define BB_DECLARE_EXTERN
57#include "messages.c"
58
59#include "pwd_grp/pwd.h"
60#include "pwd_grp/grp.h"
61
62/* for the _syscall() macros */
63#include <sys/syscall.h>
64#include <linux/unistd.h>
65
66/* Busybox mount uses either /proc/mounts or /dev/mtab to
67 * get the list of currently mounted filesystems */
68#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
69# if defined BB_MTAB
70const char mtab_file[] = "/etc/mtab";
71# else
72# if defined BB_FEATURE_USE_DEVPS_PATCH
73const char mtab_file[] = "/dev/mtab";
74# else
75const char mtab_file[] = "/proc/mounts";
76# endif
77# endif
78#endif
79
80#if defined(BB_KLOGD) || defined(BB_LOGGER)
81#include <syslog.h>
82#endif
83
84static struct BB_applet *applet_using;
85
86extern void show_usage(void)
87{
88 const char *format_string;
89 const char *usage_string = usage_messages;
90 int i;
91
92 for (i = applet_using - applets; i > 0; ) {
93 if (!*usage_string++) {
94 --i;
95 }
96 }
97 format_string = "%s\n\nUsage: %s %s\n\n";
98 if(*usage_string == 0)
99 format_string = "%s\n\nNo help available.\n\n";
100 fprintf(stderr, format_string,
101 full_version, applet_using->name, usage_string);
102 exit(EXIT_FAILURE);
103}
104
105
106static void verror_msg(const char *s, va_list p)
107{
108 fflush(stdout);
109 fprintf(stderr, "%s: ", applet_name);
110 vfprintf(stderr, s, p);
111}
112
113extern void error_msg(const char *s, ...)
114{
115 va_list p;
116
117 va_start(p, s);
118 verror_msg(s, p);
119 va_end(p);
120 putc('\n', stderr);
121}
122
123extern void error_msg_and_die(const char *s, ...)
124{
125 va_list p;
126
127 va_start(p, s);
128 verror_msg(s, p);
129 va_end(p);
130 putc('\n', stderr);
131 exit(EXIT_FAILURE);
132}
133
134static void vperror_msg(const char *s, va_list p)
135{
136 int err=errno;
137 if(s == 0) s = "";
138 verror_msg(s, p);
139 if (*s) s = ": ";
140 fprintf(stderr, "%s%s\n", s, strerror(err));
141}
142
143extern void perror_msg(const char *s, ...)
144{
145 va_list p;
146
147 va_start(p, s);
148 vperror_msg(s, p);
149 va_end(p);
150}
151
152extern void perror_msg_and_die(const char *s, ...)
153{
154 va_list p;
155
156 va_start(p, s);
157 vperror_msg(s, p);
158 va_end(p);
159 exit(EXIT_FAILURE);
160}
161
162#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT || defined BB_FEATURE_IFCONFIG_STATUS
163/* Returns kernel version encoded as major*65536 + minor*256 + patch,
164 * so, for example, to check if the kernel is greater than 2.2.11:
165 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
166 */
167extern int get_kernel_revision(void)
168{
169 struct utsname name;
170 char *s;
171 int i, r;
172
173 if (uname(&name) == -1) {
174 perror_msg("cannot get system information");
175 return (0);
176 }
177
178 s = name.release;
179 r = 0;
180 for (i=0 ; i<3 ; i++) {
181 r = r * 256 + atoi(strtok(s, "."));
182 s = NULL;
183 }
184 return r;
185}
186#endif /* BB_INIT */
187
188
189
190#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
191_syscall1(int, sysinfo, struct sysinfo *, info);
192#endif /* BB_INIT */
193
194#if defined BB_MOUNT || defined BB_UMOUNT
195
196/* Include our own version of <sys/mount.h>, since libc5 doesn't
197 * know about umount2 */
198extern _syscall1(int, umount, const char *, special_file);
199extern _syscall5(int, mount, const char *, special_file, const char *, dir,
200 const char *, fstype, unsigned long int, rwflag, const void *, data);
201#ifndef __NR_umount2
202# warning This kernel does not support the umount2 syscall
203# warning The umount2 system call is being stubbed out...
204 int umount2(const char * special_file, int flags)
205 {
206 /* BusyBox was compiled against a kernel that did not support
207 * the umount2 system call. To make this application work,
208 * you will need to recompile with a kernel supporting the
209 * umount2 system call.
210 */
211 fprintf(stderr, "\n\nTo make this application work, you will need to recompile\n");
212 fprintf(stderr, "with a kernel supporting the umount2 system call. -Erik\n\n");
213 errno=ENOSYS;
214 return -1;
215 }
216# else
217 extern _syscall2(int, umount2, const char *, special_file, int, flags);
218# endif
219#endif
220
221
222
223#if defined BB_FEATURE_NEW_MODULE_INTERFACE && ( defined BB_INSMOD || defined BB_LSMOD )
224#ifndef __NR_query_module
225static const int __NR_query_module = 167;
226#endif
227_syscall5(int, query_module, const char *, name, int, which,
228 void *, buf, size_t, bufsize, size_t*, ret);
229#endif
230
231
232#if defined (BB_CP_MV) || defined (BB_DU)
233
234#define HASH_SIZE 311 /* Should be prime */
235#define hash_inode(i) ((i) % HASH_SIZE)
236
237static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
238
239/*
240 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
241 * `ino_dev_hashtable', else return 0
242 *
243 * If NAME is a non-NULL pointer to a character pointer, and there is
244 * a match, then set *NAME to the value of the name slot in that
245 * bucket.
246 */
247int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
248{
249 ino_dev_hashtable_bucket_t *bucket;
250
251 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
252 while (bucket != NULL) {
253 if ((bucket->ino == statbuf->st_ino) &&
254 (bucket->dev == statbuf->st_dev))
255 {
256 if (name) *name = bucket->name;
257 return 1;
258 }
259 bucket = bucket->next;
260 }
261 return 0;
262}
263
264/* Add statbuf to statbuf hash table */
265void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
266{
267 int i;
268 size_t s;
269 ino_dev_hashtable_bucket_t *bucket;
270
271 i = hash_inode(statbuf->st_ino);
272 s = name ? strlen(name) : 0;
273 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
274 bucket->ino = statbuf->st_ino;
275 bucket->dev = statbuf->st_dev;
276 if (name)
277 strcpy(bucket->name, name);
278 else
279 bucket->name[0] = '\0';
280 bucket->next = ino_dev_hashtable[i];
281 ino_dev_hashtable[i] = bucket;
282}
283
284/* Clear statbuf hash table */
285void reset_ino_dev_hashtable(void)
286{
287 int i;
288 ino_dev_hashtable_bucket_t *bucket;
289
290 for (i = 0; i < HASH_SIZE; i++) {
291 while (ino_dev_hashtable[i] != NULL) {
292 bucket = ino_dev_hashtable[i]->next;
293 free(ino_dev_hashtable[i]);
294 ino_dev_hashtable[i] = bucket;
295 }
296 }
297}
298
299#endif /* BB_CP_MV || BB_DU */
300
301#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
302/*
303 * Return TRUE if a fileName is a directory.
304 * Nonexistant files return FALSE.
305 */
306int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
307{
308 int status;
309 int didMalloc = 0;
310
311 if (statBuf == NULL) {
312 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
313 ++didMalloc;
314 }
315
316 if (followLinks == TRUE)
317 status = stat(fileName, statBuf);
318 else
319 status = lstat(fileName, statBuf);
320
321 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
322 status = FALSE;
323 }
324 else status = TRUE;
325
326 if (didMalloc) {
327 free(statBuf);
328 statBuf = NULL;
329 }
330 return status;
331}
332#endif
333
334#if defined BB_AR || defined BB_CP_MV
335/*
336 * Copy chunksize bytes between two file descriptors
337 */
338int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
339{
340 size_t size;
341 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
342
343 while (chunksize > 0) {
344 if (chunksize > BUFSIZ)
345 size = BUFSIZ;
346 else
347 size = chunksize;
348 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
349 return(FALSE);
350 chunksize -= size;
351 }
352 return (TRUE);
353}
354#endif
355
356
357#if defined (BB_CP_MV) || defined BB_DPKG
358/*
359 * Copy one file to another, while possibly preserving its modes, times, and
360 * modes. Returns TRUE if successful, or FALSE on a failure with an error
361 * message output. (Failure is not indicated if attributes cannot be set.)
362 * -Erik Andersen
363 */
364int
365copy_file(const char *srcName, const char *destName,
366 int setModes, int followLinks, int forceFlag)
367{
368 int rfd;
369 int wfd;
370 int status;
371 struct stat srcStatBuf;
372 struct stat dstStatBuf;
373 struct utimbuf times;
374
375 if (followLinks == TRUE)
376 status = stat(srcName, &srcStatBuf);
377 else
378 status = lstat(srcName, &srcStatBuf);
379
380 if (status < 0) {
381 perror_msg("%s", srcName);
382 return FALSE;
383 }
384
385 if (followLinks == TRUE)
386 status = stat(destName, &dstStatBuf);
387 else
388 status = lstat(destName, &dstStatBuf);
389
390 if (status < 0 || forceFlag==TRUE) {
391 unlink(destName);
392 dstStatBuf.st_ino = -1;
393 dstStatBuf.st_dev = -1;
394 }
395
396 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
397 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
398 error_msg("Copying file \"%s\" to itself", srcName);
399 return FALSE;
400 }
401
402 if (S_ISDIR(srcStatBuf.st_mode)) {
403 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
404 /* Make sure the directory is writable */
405 status = mkdir(destName, 0777777 ^ umask(0));
406 if (status < 0 && errno != EEXIST) {
407 perror_msg("%s", destName);
408 return FALSE;
409 }
410 } else if (S_ISLNK(srcStatBuf.st_mode)) {
411 char link_val[BUFSIZ + 1];
412 int link_size;
413
414 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
415 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
416 link_size = readlink(srcName, &link_val[0], BUFSIZ);
417 if (link_size < 0) {
418 perror_msg("%s", srcName);
419 return FALSE;
420 }
421 link_val[link_size] = '\0';
422 status = symlink(link_val, destName);
423 if (status < 0) {
424 perror_msg("%s", destName);
425 return FALSE;
426 }
427#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
428 if (setModes == TRUE) {
429 /* Try to set owner, but fail silently like GNU cp */
430 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
431 }
432#endif
433 return TRUE;
434 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
435 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
436 if (mkfifo(destName, 0644) < 0) {
437 perror_msg("%s", destName);
438 return FALSE;
439 }
440 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
441 || S_ISSOCK(srcStatBuf.st_mode)) {
442 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
443 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
444 perror_msg("%s", destName);
445 return FALSE;
446 }
447 } else if (S_ISREG(srcStatBuf.st_mode)) {
448 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
449 rfd = open(srcName, O_RDONLY);
450 if (rfd < 0) {
451 perror_msg("%s", srcName);
452 return FALSE;
453 }
454
455 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
456 srcStatBuf.st_mode);
457 if (wfd < 0) {
458 perror_msg("%s", destName);
459 close(rfd);
460 return FALSE;
461 }
462
463 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
464 goto error_exit;
465
466 close(rfd);
467 if (close(wfd) < 0) {
468 return FALSE;
469 }
470 }
471
472 if (setModes == TRUE) {
473 /* This is fine, since symlinks never get here */
474 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
475 perror_msg_and_die("%s", destName);
476 if (chmod(destName, srcStatBuf.st_mode) < 0)
477 perror_msg_and_die("%s", destName);
478 times.actime = srcStatBuf.st_atime;
479 times.modtime = srcStatBuf.st_mtime;
480 if (utime(destName, &times) < 0)
481 perror_msg_and_die("%s", destName);
482 }
483
484 return TRUE;
485
486 error_exit:
487 perror_msg("%s", destName);
488 close(rfd);
489 close(wfd);
490
491 return FALSE;
492}
493#endif /* BB_CP_MV */
494
495
496
497#if defined BB_TAR || defined BB_LS ||defined BB_AR
498
499#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
500#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
501
502/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
503static const mode_t SBIT[] = {
504 0, 0, S_ISUID,
505 0, 0, S_ISGID,
506 0, 0, S_ISVTX
507};
508
509/* The 9 mode bits to test */
510static const mode_t MBIT[] = {
511 S_IRUSR, S_IWUSR, S_IXUSR,
512 S_IRGRP, S_IWGRP, S_IXGRP,
513 S_IROTH, S_IWOTH, S_IXOTH
514};
515
516static const char MODE1[] = "rwxrwxrwx";
517static const char MODE0[] = "---------";
518static const char SMODE1[] = "..s..s..t";
519static const char SMODE0[] = "..S..S..T";
520
521/*
522 * Return the standard ls-like mode string from a file mode.
523 * This is static and so is overwritten on each call.
524 */
525const char *mode_string(int mode)
526{
527 static char buf[12];
528
529 int i;
530
531 buf[0] = TYPECHAR(mode);
532 for (i = 0; i < 9; i++) {
533 if (mode & SBIT[i])
534 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
535 else
536 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
537 }
538 return buf;
539}
540#endif /* BB_TAR || BB_LS */
541
542
543#if defined BB_TAR || defined BB_AR
544/*
545 * Return the standard ls-like time string from a time_t
546 * This is static and so is overwritten on each call.
547 */
548const char *time_string(time_t timeVal)
549{
550 time_t now;
551 char *str;
552 static char buf[26];
553
554 time(&now);
555
556 str = ctime(&timeVal);
557
558 strcpy(buf, &str[4]);
559 buf[12] = '\0';
560
561 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
562 strcpy(&buf[7], &str[20]);
563 buf[11] = '\0';
564 }
565
566 return buf;
567}
568#endif /* BB_TAR || BB_AR */
569
570#if defined BB_DD || defined BB_NC || defined BB_TAIL || defined BB_TAR || defined BB_AR || defined BB_CP_MV
571/*
572 * Write all of the supplied buffer out to a file.
573 * This does multiple writes as necessary.
574 * Returns the amount written, or -1 on an error.
575 */
576int full_write(int fd, const char *buf, int len)
577{
578 int cc;
579 int total;
580
581 total = 0;
582
583 while (len > 0) {
584 cc = write(fd, buf, len);
585
586 if (cc < 0)
587 return -1;
588
589 buf += cc;
590 total += cc;
591 len -= cc;
592 }
593
594 return total;
595}
596#endif
597
598#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
599/*
600 * Read all of the supplied buffer from a file.
601 * This does multiple reads as necessary.
602 * Returns the amount read, or -1 on an error.
603 * A short read is returned on an end of file.
604 */
605int full_read(int fd, char *buf, int len)
606{
607 int cc;
608 int total;
609
610 total = 0;
611
612 while (len > 0) {
613 cc = read(fd, buf, len);
614
615 if (cc < 0)
616 return -1;
617
618 if (cc == 0)
619 break;
620
621 buf += cc;
622 total += cc;
623 len -= cc;
624 }
625
626 return total;
627}
628#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
629
630
631#if defined (BB_CHMOD_CHOWN_CHGRP) \
632 || defined (BB_CP_MV) \
633 || defined (BB_FIND) \
634 || defined (BB_INSMOD) \
635 || defined (BB_LS) \
636 || defined (BB_RM) \
637 || defined (BB_TAR)
638
639/*
640 * Walk down all the directories under the specified
641 * location, and do something (something specified
642 * by the fileAction and dirAction function pointers).
643 *
644 * Unfortunately, while nftw(3) could replace this and reduce
645 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
646 * and so isn't sufficiently portable to take over since glibc2.1
647 * is so stinking huge.
648 */
649int recursive_action(const char *fileName,
650 int recurse, int followLinks, int depthFirst,
651 int (*fileAction) (const char *fileName,
652 struct stat * statbuf,
653 void* userData),
654 int (*dirAction) (const char *fileName,
655 struct stat * statbuf,
656 void* userData),
657 void* userData)
658{
659 int status;
660 struct stat statbuf;
661 struct dirent *next;
662
663 if (followLinks == TRUE)
664 status = stat(fileName, &statbuf);
665 else
666 status = lstat(fileName, &statbuf);
667
668 if (status < 0) {
669#ifdef BB_DEBUG_PRINT_SCAFFOLD
670 fprintf(stderr,
671 "status=%d followLinks=%d TRUE=%d\n",
672 status, followLinks, TRUE);
673#endif
674 perror_msg("%s", fileName);
675 return FALSE;
676 }
677
678 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
679 if (fileAction == NULL)
680 return TRUE;
681 else
682 return fileAction(fileName, &statbuf, userData);
683 }
684
685 if (recurse == FALSE) {
686 if (S_ISDIR(statbuf.st_mode)) {
687 if (dirAction != NULL)
688 return (dirAction(fileName, &statbuf, userData));
689 else
690 return TRUE;
691 }
692 }
693
694 if (S_ISDIR(statbuf.st_mode)) {
695 DIR *dir;
696
697 if (dirAction != NULL && depthFirst == FALSE) {
698 status = dirAction(fileName, &statbuf, userData);
699 if (status == FALSE) {
700 perror_msg("%s", fileName);
701 return FALSE;
702 } else if (status == SKIP)
703 return TRUE;
704 }
705 dir = opendir(fileName);
706 if (!dir) {
707 perror_msg("%s", fileName);
708 return FALSE;
709 }
710 status = TRUE;
711 while ((next = readdir(dir)) != NULL) {
712 char nextFile[PATH_MAX];
713
714 if ((strcmp(next->d_name, "..") == 0)
715 || (strcmp(next->d_name, ".") == 0)) {
716 continue;
717 }
718 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
719 error_msg(name_too_long);
720 return FALSE;
721 }
722 memset(nextFile, 0, sizeof(nextFile));
723 if (fileName[strlen(fileName)-1] == '/')
724 sprintf(nextFile, "%s%s", fileName, next->d_name);
725 else
726 sprintf(nextFile, "%s/%s", fileName, next->d_name);
727 if (recursive_action(nextFile, TRUE, followLinks, depthFirst,
728 fileAction, dirAction, userData) == FALSE) {
729 status = FALSE;
730 }
731 }
732 closedir(dir);
733 if (dirAction != NULL && depthFirst == TRUE) {
734 if (dirAction(fileName, &statbuf, userData) == FALSE) {
735 perror_msg("%s", fileName);
736 return FALSE;
737 }
738 }
739 if (status == FALSE)
740 return FALSE;
741 } else {
742 if (fileAction == NULL)
743 return TRUE;
744 else
745 return fileAction(fileName, &statbuf, userData);
746 }
747 return TRUE;
748}
749
750#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
751
752
753
754#if defined (BB_TAR) || defined (BB_MKDIR)
755/*
756 * Attempt to create the directories along the specified path, except for
757 * the final component. The mode is given for the final directory only,
758 * while all previous ones get default protections. Errors are not reported
759 * here, as failures to restore files can be reported later.
760 */
761extern int create_path(const char *name, int mode)
762{
763 char *cp;
764 char *cpOld;
765 char buf[BUFSIZ + 1];
766 int retVal = 0;
767
768 strcpy(buf, name);
769 for (cp = buf; *cp == '/'; cp++);
770 cp = strchr(cp, '/');
771 while (cp) {
772 cpOld = cp;
773 cp = strchr(cp + 1, '/');
774 *cpOld = '\0';
775 retVal = mkdir(buf, cp ? 0777 : mode);
776 if (retVal != 0 && errno != EEXIST) {
777 perror_msg("%s", buf);
778 return FALSE;
779 }
780 *cpOld = '/';
781 }
782 return TRUE;
783}
784#endif /* BB_TAR || BB_MKDIR */
785
786
787
788#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
789 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
790/* [ugoa]{+|-|=}[rwxst] */
791
792extern int parse_mode(const char *s, mode_t * theMode)
793{
794 static const mode_t group_set[] = {
795 S_ISUID | S_IRWXU, /* u */
796 S_ISGID | S_IRWXG, /* g */
797 S_IRWXO, /* o */
798 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
799 };
800
801 static const mode_t mode_set[] = {
802 S_IRUSR | S_IRGRP | S_IROTH, /* r */
803 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
804 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
805 S_ISUID | S_ISGID, /* s */
806 S_ISVTX /* t */
807 };
808
809 static const char group_string[] = "ugoa";
810 static const char mode_string[] = "rwxst";
811
812 const char *p;
813
814 mode_t andMode =
815 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
816 mode_t orMode = 0;
817 mode_t mode;
818 mode_t groups;
819 char type;
820 char c;
821
822 if (s==NULL) {
823 return (FALSE);
824 }
825
826 do {
827 mode = 0;
828 groups = 0;
829 NEXT_GROUP:
830 if ((c = *s++) == '\0') {
831 return -1;
832 }
833 for (p=group_string ; *p ; p++) {
834 if (*p == c) {
835 groups |= group_set[(int)(p-group_string)];
836 goto NEXT_GROUP;
837 }
838 }
839 switch (c) {
840 case '=':
841 case '+':
842 case '-':
843 type = c;
844 if (groups == 0) { /* The default is "all" */
845 groups |= S_ISUID | S_ISGID | S_ISVTX
846 | S_IRWXU | S_IRWXG | S_IRWXO;
847 }
848 break;
849 default:
850 if ((c < '0') || (c > '7') || (mode | groups)) {
851 return (FALSE);
852 } else {
853 *theMode = strtol(--s, NULL, 8);
854 return (TRUE);
855 }
856 }
857
858 NEXT_MODE:
859 if (((c = *s++) != '\0') && (c != ',')) {
860 for (p=mode_string ; *p ; p++) {
861 if (*p == c) {
862 mode |= mode_set[(int)(p-mode_string)];
863 goto NEXT_MODE;
864 }
865 }
866 break; /* We're done so break out of loop.*/
867 }
868 switch (type) {
869 case '=':
870 andMode &= ~(groups); /* Now fall through. */
871 case '+':
872 orMode |= mode & groups;
873 break;
874 case '-':
875 andMode &= ~(mode & groups);
876 orMode &= ~(mode & groups);
877 break;
878 }
879 } while (c == ',');
880
881 *theMode &= andMode;
882 *theMode |= orMode;
883
884 return TRUE;
885}
886
887#endif
888/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
889
890
891
892
893
894#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
895 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
896 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
897
898#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_ID
899/* returns a uid given a username */
900long my_getpwnam(const char *name)
901{
902 struct passwd *myuser;
903
904 myuser = getpwnam(name);
905 if (myuser==NULL)
906 error_msg_and_die("unknown user name: %s", name);
907
908 return myuser->pw_uid;
909}
910
911/* returns a gid given a group name */
912long my_getgrnam(const char *name)
913{
914 struct group *mygroup;
915
916 mygroup = getgrnam(name);
917 if (mygroup==NULL)
918 error_msg_and_die("unknown group name: %s", name);
919
920 return (mygroup->gr_gid);
921}
922#endif
923
924/* gets a username given a uid */
925void my_getpwuid(char *name, long uid)
926{
927 struct passwd *myuser;
928
929 myuser = getpwuid(uid);
930 if (myuser==NULL)
931 sprintf(name, "%-8ld ", (long)uid);
932 else
933 strcpy(name, myuser->pw_name);
934}
935
936/* gets a groupname given a gid */
937void my_getgrgid(char *group, long gid)
938{
939 struct group *mygroup;
940
941 mygroup = getgrgid(gid);
942 if (mygroup==NULL)
943 sprintf(group, "%-8ld ", (long)gid);
944 else
945 strcpy(group, mygroup->gr_name);
946}
947
948#if defined BB_ID
949/* gets a gid given a user name */
950long my_getpwnamegid(const char *name)
951{
952 struct group *mygroup;
953 struct passwd *myuser;
954
955 myuser=getpwnam(name);
956 if (myuser==NULL)
957 error_msg_and_die("unknown user name: %s", name);
958
959 mygroup = getgrgid(myuser->pw_gid);
960 if (mygroup==NULL)
961 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
962
963 return mygroup->gr_gid;
964}
965#endif /* BB_ID */
966#endif
967 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
968 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
969
970
971#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
972
973/* From <linux/kd.h> */
974static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
975static const int KB_84 = 0x01;
976static const int KB_101 = 0x02; /* this is what we always answer */
977
978int is_a_console(int fd)
979{
980 char arg;
981
982 arg = 0;
983 return (ioctl(fd, KDGKBTYPE, &arg) == 0
984 && ((arg == KB_101) || (arg == KB_84)));
985}
986
987static int open_a_console(char *fnam)
988{
989 int fd;
990
991 /* try read-only */
992 fd = open(fnam, O_RDWR);
993
994 /* if failed, try read-only */
995 if (fd < 0 && errno == EACCES)
996 fd = open(fnam, O_RDONLY);
997
998 /* if failed, try write-only */
999 if (fd < 0 && errno == EACCES)
1000 fd = open(fnam, O_WRONLY);
1001
1002 /* if failed, fail */
1003 if (fd < 0)
1004 return -1;
1005
1006 /* if not a console, fail */
1007 if (!is_a_console(fd)) {
1008 close(fd);
1009 return -1;
1010 }
1011
1012 /* success */
1013 return fd;
1014}
1015
1016/*
1017 * Get an fd for use with kbd/console ioctls.
1018 * We try several things because opening /dev/console will fail
1019 * if someone else used X (which does a chown on /dev/console).
1020 *
1021 * if tty_name is non-NULL, try this one instead.
1022 */
1023
1024int get_console_fd(char *tty_name)
1025{
1026 int fd;
1027
1028 if (tty_name) {
1029 if (-1 == (fd = open_a_console(tty_name)))
1030 return -1;
1031 else
1032 return fd;
1033 }
1034
1035 fd = open_a_console("/dev/tty");
1036 if (fd >= 0)
1037 return fd;
1038
1039 fd = open_a_console("/dev/tty0");
1040 if (fd >= 0)
1041 return fd;
1042
1043 fd = open_a_console("/dev/console");
1044 if (fd >= 0)
1045 return fd;
1046
1047 for (fd = 0; fd < 3; fd++)
1048 if (is_a_console(fd))
1049 return fd;
1050
1051 error_msg("Couldnt get a file descriptor referring to the console");
1052 return -1; /* total failure */
1053}
1054
1055
1056#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
1057
1058
1059#if defined BB_FIND || defined BB_INSMOD
1060/*
1061 * Routine to see if a text string is matched by a wildcard pattern.
1062 * Returns TRUE if the text is matched, or FALSE if it is not matched
1063 * or if the pattern is invalid.
1064 * * matches zero or more characters
1065 * ? matches a single character
1066 * [abc] matches 'a', 'b' or 'c'
1067 * \c quotes character c
1068 * Adapted from code written by Ingo Wilken, and
1069 * then taken from sash, Copyright (c) 1999 by David I. Bell
1070 * Permission is granted to use, distribute, or modify this source,
1071 * provided that this copyright notice remains intact.
1072 * Permission to distribute this code under the GPL has been granted.
1073 */
1074extern int check_wildcard_match(const char *text, const char *pattern)
1075{
1076 const char *retryPat;
1077 const char *retryText;
1078 int ch;
1079 int found;
1080 int len;
1081
1082 retryPat = NULL;
1083 retryText = NULL;
1084
1085 while (*text || *pattern) {
1086 ch = *pattern++;
1087
1088 switch (ch) {
1089 case '*':
1090 retryPat = pattern;
1091 retryText = text;
1092 break;
1093
1094 case '[':
1095 found = FALSE;
1096
1097 while ((ch = *pattern++) != ']') {
1098 if (ch == '\\')
1099 ch = *pattern++;
1100
1101 if (ch == '\0')
1102 return FALSE;
1103
1104 if (*text == ch)
1105 found = TRUE;
1106 }
1107 len=strlen(text);
1108 if (found == FALSE && len!=0) {
1109 return FALSE;
1110 }
1111 if (found == TRUE) {
1112 if (strlen(pattern)==0 && len==1) {
1113 return TRUE;
1114 }
1115 if (len!=0) {
1116 text++;
1117 continue;
1118 }
1119 }
1120
1121 /* fall into next case */
1122
1123 case '?':
1124 if (*text++ == '\0')
1125 return FALSE;
1126
1127 break;
1128
1129 case '\\':
1130 ch = *pattern++;
1131
1132 if (ch == '\0')
1133 return FALSE;
1134
1135 /* fall into next case */
1136
1137 default:
1138 if (*text == ch) {
1139 if (*text)
1140 text++;
1141 break;
1142 }
1143
1144 if (*text) {
1145 pattern = retryPat;
1146 text = ++retryText;
1147 break;
1148 }
1149
1150 return FALSE;
1151 }
1152
1153 if (pattern == NULL)
1154 return FALSE;
1155 }
1156
1157 return TRUE;
1158}
1159#endif /* BB_FIND || BB_INSMOD */
1160
1161
1162
1163
1164#if defined BB_DF || defined BB_MTAB
1165#include <mntent.h>
1166/*
1167 * Given a block device, find the mount table entry if that block device
1168 * is mounted.
1169 *
1170 * Given any other file (or directory), find the mount table entry for its
1171 * filesystem.
1172 */
1173extern struct mntent *find_mount_point(const char *name, const char *table)
1174{
1175 struct stat s;
1176 dev_t mountDevice;
1177 FILE *mountTable;
1178 struct mntent *mountEntry;
1179
1180 if (stat(name, &s) != 0)
1181 return 0;
1182
1183 if ((s.st_mode & S_IFMT) == S_IFBLK)
1184 mountDevice = s.st_rdev;
1185 else
1186 mountDevice = s.st_dev;
1187
1188
1189 if ((mountTable = setmntent(table, "r")) == 0)
1190 return 0;
1191
1192 while ((mountEntry = getmntent(mountTable)) != 0) {
1193 if (strcmp(name, mountEntry->mnt_dir) == 0
1194 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1195 break;
1196 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1197 break;
1198 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1199 break;
1200 }
1201 endmntent(mountTable);
1202 return mountEntry;
1203}
1204#endif /* BB_DF || BB_MTAB */
1205
1206#if defined BB_INIT || defined BB_SYSLOGD
1207/* try to open up the specified device */
1208extern int device_open(char *device, int mode)
1209{
1210 int m, f, fd = -1;
1211
1212 m = mode | O_NONBLOCK;
1213
1214 /* Retry up to 5 times */
1215 for (f = 0; f < 5; f++)
1216 if ((fd = open(device, m, 0600)) >= 0)
1217 break;
1218 if (fd < 0)
1219 return fd;
1220 /* Reset original flags. */
1221 if (m != mode)
1222 fcntl(fd, F_SETFL, mode);
1223 return fd;
1224}
1225#endif /* BB_INIT BB_SYSLOGD */
1226
1227
1228#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
1229#ifdef BB_FEATURE_USE_DEVPS_PATCH
1230#include <linux/devps.h> /* For Erik's nifty devps device driver */
1231#endif
1232
1233#if defined BB_FEATURE_USE_DEVPS_PATCH
1234/* find_pid_by_name()
1235 *
1236 * This finds the pid of the specified process,
1237 * by using the /dev/ps device driver.
1238 *
1239 * Returns a list of all matching PIDs
1240 */
1241extern pid_t* find_pid_by_name( char* pidName)
1242{
1243 int fd, i, j;
1244 char device[] = "/dev/ps";
1245 pid_t num_pids;
1246 pid_t* pid_array = NULL;
1247 pid_t* pidList=NULL;
1248
1249 /* open device */
1250 fd = open(device, O_RDONLY);
1251 if (fd < 0)
1252 perror_msg_and_die("open failed for `%s'", device);
1253
1254 /* Find out how many processes there are */
1255 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1256 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
1257
1258 /* Allocate some memory -- grab a few extras just in case
1259 * some new processes start up while we wait. The kernel will
1260 * just ignore any extras if we give it too many, and will trunc.
1261 * the list if we give it too few. */
1262 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
1263 pid_array[0] = num_pids+10;
1264
1265 /* Now grab the pid list */
1266 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1267 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
1268
1269 /* Now search for a match */
1270 for (i=1, j=0; i<pid_array[0] ; i++) {
1271 char* p;
1272 struct pid_info info;
1273
1274 info.pid = pid_array[i];
1275 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1276 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
1277
1278 /* Make sure we only match on the process name */
1279 p=info.command_line+1;
1280 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1281 (p)++;
1282 }
1283 if (isspace(*(p)))
1284 *p='\0';
1285
1286 if ((strstr(info.command_line, pidName) != NULL)
1287 && (strlen(pidName) == strlen(info.command_line))) {
1288 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
1289 pidList[j++]=info.pid;
1290 }
1291 }
1292 if (pidList)
1293 pidList[j]=0;
1294
1295 /* Free memory */
1296 free( pid_array);
1297
1298 /* close device */
1299 if (close (fd) != 0)
1300 perror_msg_and_die("close failed for `%s'", device);
1301
1302 return pidList;
1303}
1304#else /* BB_FEATURE_USE_DEVPS_PATCH */
1305
1306/* find_pid_by_name()
1307 *
1308 * This finds the pid of the specified process.
1309 * Currently, it's implemented by rummaging through
1310 * the proc filesystem.
1311 *
1312 * Returns a list of all matching PIDs
1313 */
1314extern pid_t* find_pid_by_name( char* pidName)
1315{
1316 DIR *dir;
1317 struct dirent *next;
1318 pid_t* pidList=NULL;
1319 int i=0;
1320
1321 dir = opendir("/proc");
1322 if (!dir)
1323 perror_msg_and_die("Cannot open /proc");
1324
1325 while ((next = readdir(dir)) != NULL) {
1326 FILE *status;
1327 char filename[256];
1328 char buffer[256];
1329
1330 /* If it isn't a number, we don't want it */
1331 if (!isdigit(*next->d_name))
1332 continue;
1333
1334 sprintf(filename, "/proc/%s/cmdline", next->d_name);
1335 status = fopen(filename, "r");
1336 if (!status) {
1337 continue;
1338 }
1339 fgets(buffer, 256, status);
1340 fclose(status);
1341
1342 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
1343 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
1344 pidList[i++]=strtol(next->d_name, NULL, 0);
1345 }
1346 }
1347
1348 if (pidList)
1349 pidList[i]=0;
1350 return pidList;
1351}
1352#endif /* BB_FEATURE_USE_DEVPS_PATCH */
1353#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
1354
1355#ifndef DMALLOC
1356/* this should really be farmed out to libbusybox.a */
1357extern void *xmalloc(size_t size)
1358{
1359 void *ptr = malloc(size);
1360
1361 if (!ptr)
1362 error_msg_and_die(memory_exhausted);
1363 return ptr;
1364}
1365
1366extern void *xrealloc(void *old, size_t size)
1367{
1368 void *ptr = realloc(old, size);
1369 if (!ptr)
1370 error_msg_and_die(memory_exhausted);
1371 return ptr;
1372}
1373
1374extern void *xcalloc(size_t nmemb, size_t size)
1375{
1376 void *ptr = calloc(nmemb, size);
1377 if (!ptr)
1378 error_msg_and_die(memory_exhausted);
1379 return ptr;
1380}
1381#endif
1382
1383#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || \
1384 defined BB_WGET || defined BB_DPKG_DEB || defined BB_TAR || \
1385 defined BB_LN
1386# ifndef DMALLOC
1387extern char * xstrdup (const char *s) {
1388 char *t;
1389
1390 if (s == NULL)
1391 return NULL;
1392
1393 t = strdup (s);
1394
1395 if (t == NULL)
1396 error_msg_and_die(memory_exhausted);
1397
1398 return t;
1399}
1400# endif
1401#endif
1402
1403#if defined BB_NFSMOUNT
1404extern char * xstrndup (const char *s, int n) {
1405 char *t;
1406
1407 if (s == NULL)
1408 error_msg_and_die("xstrndup bug");
1409
1410 t = xmalloc(++n);
1411
1412 return safe_strncpy(t,s,n);
1413}
1414#endif
1415
1416#if defined BB_IFCONFIG || defined BB_ROUTE || defined BB_NFSMOUNT || \
1417 defined BB_FEATURE_MOUNT_LOOP
1418/* Like strncpy but make sure the resulting string is always 0 terminated. */
1419extern char * safe_strncpy(char *dst, const char *src, size_t size)
1420{
1421 dst[size-1] = '\0';
1422 return strncpy(dst, src, size-1);
1423}
1424#endif
1425
1426#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1427extern int vdprintf(int d, const char *format, va_list ap)
1428{
1429 char buf[BUF_SIZE];
1430 int len;
1431
1432 len = vsprintf(buf, format, ap);
1433 return write(d, buf, len);
1434}
1435#endif /* BB_SYSLOGD */
1436
1437
1438#if defined BB_FEATURE_MOUNT_LOOP
1439#include <fcntl.h>
1440#include "loop.h" /* Pull in loop device support */
1441
1442extern int del_loop(const char *device)
1443{
1444 int fd;
1445
1446 if ((fd = open(device, O_RDONLY)) < 0) {
1447 perror_msg("%s", device);
1448 return (FALSE);
1449 }
1450 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1451 perror_msg("ioctl: LOOP_CLR_FD");
1452 return (FALSE);
1453 }
1454 close(fd);
1455 return (TRUE);
1456}
1457
1458extern int set_loop(const char *device, const char *file, int offset,
1459 int *loopro)
1460{
1461 struct loop_info loopinfo;
1462 int fd, ffd, mode;
1463
1464 mode = *loopro ? O_RDONLY : O_RDWR;
1465 if ((ffd = open(file, mode)) < 0 && !*loopro
1466 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1467 perror_msg("%s", file);
1468 return 1;
1469 }
1470 if ((fd = open(device, mode)) < 0) {
1471 close(ffd);
1472 perror_msg("%s", device);
1473 return 1;
1474 }
1475 *loopro = (mode == O_RDONLY);
1476
1477 memset(&loopinfo, 0, sizeof(loopinfo));
1478 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1479
1480 loopinfo.lo_offset = offset;
1481
1482 loopinfo.lo_encrypt_key_size = 0;
1483 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1484 perror_msg("ioctl: LOOP_SET_FD");
1485 close(fd);
1486 close(ffd);
1487 return 1;
1488 }
1489 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1490 (void) ioctl(fd, LOOP_CLR_FD, 0);
1491 perror_msg("ioctl: LOOP_SET_STATUS");
1492 close(fd);
1493 close(ffd);
1494 return 1;
1495 }
1496 close(fd);
1497 close(ffd);
1498 return 0;
1499}
1500
1501extern char *find_unused_loop_device(void)
1502{
1503 char dev[20];
1504 int i, fd;
1505 struct stat statbuf;
1506 struct loop_info loopinfo;
1507
1508 for (i = 0; i <= 7; i++) {
1509 sprintf(dev, "/dev/loop%d", i);
1510 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1511 if ((fd = open(dev, O_RDONLY)) >= 0) {
1512 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
1513 if (errno == ENXIO) { /* probably free */
1514 close(fd);
1515 return strdup(dev);
1516 }
1517 }
1518 close(fd);
1519 }
1520 }
1521 }
1522 return NULL;
1523}
1524#endif /* BB_FEATURE_MOUNT_LOOP */
1525
1526#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
1527extern int find_real_root_device_name(char* name)
1528{
1529 DIR *dir;
1530 struct dirent *entry;
1531 struct stat statBuf, rootStat;
1532 char fileName[BUFSIZ];
1533
1534 if (stat("/", &rootStat) != 0) {
1535 error_msg("could not stat '/'");
1536 return( FALSE);
1537 }
1538
1539 dir = opendir("/dev");
1540 if (!dir) {
1541 error_msg("could not open '/dev'");
1542 return( FALSE);
1543 }
1544
1545 while((entry = readdir(dir)) != NULL) {
1546
1547 /* Must skip ".." since that is "/", and so we
1548 * would get a false positive on ".." */
1549 if (strcmp(entry->d_name, "..") == 0)
1550 continue;
1551
1552 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
1553
1554 if (stat(fileName, &statBuf) != 0)
1555 continue;
1556 /* Some char devices have the same dev_t as block
1557 * devices, so make sure this is a block device */
1558 if (! S_ISBLK(statBuf.st_mode))
1559 continue;
1560 if (statBuf.st_rdev == rootStat.st_rdev) {
1561 strcpy(name, fileName);
1562 return ( TRUE);
1563 }
1564 }
1565
1566 return( FALSE);
1567}
1568#endif
1569
1570
1571/* get_line_from_file() - This function reads an entire line from a text file
1572 * up to a newline. It returns a malloc'ed char * which must be stored and
1573 * free'ed by the caller. */
1574extern char *get_line_from_file(FILE *file)
1575{
1576 static const int GROWBY = 80; /* how large we will grow strings by */
1577
1578 int ch;
1579 int idx = 0;
1580 char *linebuf = NULL;
1581 int linebufsz = 0;
1582
1583 while (1) {
1584 ch = fgetc(file);
1585 if (ch == EOF)
1586 break;
1587 /* grow the line buffer as necessary */
1588 while (idx > linebufsz-2)
1589 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
1590 linebuf[idx++] = (char)ch;
1591 if ((char)ch == '\n')
1592 break;
1593 }
1594
1595 if (idx == 0)
1596 return NULL;
1597
1598 linebuf[idx] = 0;
1599 return linebuf;
1600}
1601
1602#if defined BB_CAT
1603extern void print_file(FILE *file)
1604{
1605 int c;
1606
1607 while ((c = getc(file)) != EOF)
1608 putc(c, stdout);
1609 fclose(file);
1610 fflush(stdout);
1611}
1612
1613extern int print_file_by_name(char *filename)
1614{
1615 FILE *file;
1616 if ((file = wfopen(filename, "r")) == NULL)
1617 return FALSE;
1618 print_file(file);
1619 return TRUE;
1620}
1621#endif /* BB_CAT */
1622
1623#if defined BB_ECHO || defined BB_SH || defined BB_TR
1624char process_escape_sequence(char **ptr)
1625{
1626 static const char charmap[] = {
1627 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1628 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
1629
1630 const char *p;
1631 char *q;
1632 int num_digits;
1633 unsigned int n;
1634
1635 n = 0;
1636 q = *ptr;
1637
1638 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1639 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1640 break;
1641 }
1642 n = n * 8 + (*q++ - '0');
1643 }
1644
1645 if (num_digits == 0) { /* mnemonic escape sequence? */
1646 for (p=charmap ; *p ; p++) {
1647 if (*p == *q) {
1648 q++;
1649 break;
1650 }
1651 }
1652 n = *(p+(sizeof(charmap)/2));
1653 }
1654
1655 /* doesn't hurt to fall through to here from mnemonic case */
1656 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1657 n /= 8; /* adjust value and */
1658 --q; /* back up one char */
1659 }
1660
1661 *ptr = q;
1662 return (char) n;
1663}
1664#endif
1665
1666#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
1667 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
1668char *get_last_path_component(char *path)
1669{
1670 char *s=path+strlen(path)-1;
1671
1672 /* strip trailing slashes */
1673 while (s != path && *s == '/') {
1674 *s-- = '\0';
1675 }
1676
1677 /* find last component */
1678 s = strrchr(path, '/');
1679 if (s == NULL || s[1] == '\0')
1680 return path;
1681 else
1682 return s+1;
1683}
1684#endif
1685
1686#if defined BB_GREP || defined BB_SED
1687#include <regex.h>
1688void xregcomp(regex_t *preg, const char *regex, int cflags)
1689{
1690 int ret;
1691 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1692 int errmsgsz = regerror(ret, preg, NULL, 0);
1693 char *errmsg = xmalloc(errmsgsz);
1694 regerror(ret, preg, errmsg, errmsgsz);
1695 error_msg_and_die("xregcomp: %s", errmsg);
1696 }
1697}
1698#endif
1699
1700#if defined BB_CAT || defined BB_HEAD || defined BB_WC
1701FILE *wfopen(const char *path, const char *mode)
1702{
1703 FILE *fp;
1704 if ((fp = fopen(path, mode)) == NULL) {
1705 perror_msg("%s", path);
1706 errno = 0;
1707 }
1708 return fp;
1709}
1710#endif
1711
1712#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
1713 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
1714 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
1715 || defined BB_MOUNT || defined BB_ROUTE || defined BB_MKFS_MINIX
1716FILE *xfopen(const char *path, const char *mode)
1717{
1718 FILE *fp;
1719 if ((fp = fopen(path, mode)) == NULL)
1720 perror_msg_and_die("%s", path);
1721 return fp;
1722}
1723#endif
1724
1725static int applet_name_compare(const void *x, const void *y)
1726{
1727 const char *name = x;
1728 const struct BB_applet *applet = y;
1729
1730 return strcmp(name, applet->name);
1731}
1732
1733extern size_t NUM_APPLETS;
1734
1735struct BB_applet *find_applet_by_name(const char *name)
1736{
1737 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1738 applet_name_compare);
1739}
1740
1741void run_applet_by_name(const char *name, int argc, char **argv)
1742{
1743 /* Do a binary search to find the applet entry given the name. */
1744 if ((applet_using = find_applet_by_name(name)) != NULL) {
1745 applet_name = applet_using->name;
1746 if (argv[1] && strcmp(argv[1], "--help") == 0)
1747 show_usage();
1748 exit((*(applet_using->main)) (argc, argv));
1749 }
1750}
1751
1752#if defined BB_DD || defined BB_TAIL || defined BB_STTY
1753unsigned long parse_number(const char *numstr,
1754 const struct suffix_mult *suffixes)
1755{
1756 const struct suffix_mult *sm;
1757 unsigned long int ret;
1758 int len;
1759 char *end;
1760
1761 ret = strtoul(numstr, &end, 10);
1762 if (numstr == end)
1763 error_msg_and_die("invalid number `%s'", numstr);
1764 while (end[0] != '\0') {
1765 sm = suffixes;
1766 while ( sm != 0 ) {
1767 if(sm->suffix) {
1768 len = strlen(sm->suffix);
1769 if (strncmp(sm->suffix, end, len) == 0) {
1770 ret *= sm->mult;
1771 end += len;
1772 break;
1773 }
1774 sm++;
1775
1776 } else
1777 sm = 0;
1778 }
1779 if (sm == 0)
1780 error_msg_and_die("invalid number `%s'", numstr);
1781 }
1782 return ret;
1783}
1784#endif
1785
1786#if defined BB_DD || defined BB_NC || defined BB_TAIL
1787ssize_t safe_read(int fd, void *buf, size_t count)
1788{
1789 ssize_t n;
1790
1791 do {
1792 n = read(fd, buf, count);
1793 } while (n < 0 && errno == EINTR);
1794
1795 return n;
1796}
1797#endif
1798
1799#ifdef BB_FEATURE_HUMAN_READABLE
1800const char *make_human_readable_str(unsigned long val, unsigned long hr)
1801{
1802 int i=0;
1803 static char str[10] = "\0";
1804 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
1805 unsigned long divisor = 1;
1806
1807 if(val == 0)
1808 return("0");
1809 if(hr)
1810 snprintf(str, 9, "%ld", val/hr);
1811 else {
1812 while(val >= divisor && i <= 4) {
1813 divisor=divisor<<10, i++;
1814 }
1815 divisor=divisor>>10, i--;
1816 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
1817 }
1818 return(str);
1819}
1820#endif
1821
1822#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS) || defined(BB_SH)
1823void chomp(char *s)
1824{
1825 size_t len = strlen(s);
1826
1827 if (len == 0)
1828 return;
1829
1830 if (s[len-1] == '\n')
1831 s[len-1] = '\0';
1832}
1833#endif
1834
1835#if defined(BB_KLOGD) || defined(BB_LOGGER)
1836void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg)
1837{
1838 openlog(name, 0, facility);
1839 syslog(pri, "%s", msg);
1840 closelog();
1841}
1842
1843void syslog_msg(int facility, int pri, const char *msg)
1844{
1845 syslog_msg_with_name(applet_using->name, facility, pri, msg);
1846}
1847#endif
1848
1849#if defined(BB_SH)
1850void trim(char *s)
1851{
1852 /* trim trailing whitespace */
1853 while (isspace(s[strlen(s)-1]))
1854 s[strlen(s)-1]='\0';
1855
1856 /* trim leading whitespace */
1857 memmove(s, &s[strspn(s, " \n\r\t\v")], strlen(s));
1858
1859}
1860#endif
1861
1862#ifdef BB_FEATURE_RM_INTERACTIVE
1863 #if defined (BB_CP_MV) || defined (BB_RM)
1864int ask_confirmation()
1865{
1866 int c = '\0';
1867 int ret = 0;
1868
1869 while (c != '\n') {
1870 c = getchar();
1871 if ( c != '\n' ) {
1872 ret = ((c=='y')||(c=='Y')) ? 1 : 0;
1873 }
1874 }
1875 return ret;
1876}
1877 #endif
1878#endif
1879
1880/* END CODE */
1881/*
1882Local Variables:
1883c-file-style: "linux"
1884c-basic-offset: 4
1885tab-width: 4
1886End:
1887*/