blob: 64598fab6a6a965db206cbac167075b01ed61a1f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
3 * Utility routines.
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * 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.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 *
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
Eric Andersen2b69c401999-10-05 22:58:32 +000028#include "internal.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000029#if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
32 || defined (BB_LS) \
33 || defined (BB_INSMOD)
34/* same conditions as recursiveAction */
35#define bb_need_name_too_long
36#endif
37#define BB_DECLARE_EXTERN
38#include "messages.c"
39
Eric Andersen2b69c401999-10-05 22:58:32 +000040#include <stdio.h>
41#include <string.h>
42#include <errno.h>
43#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <dirent.h>
45#include <time.h>
46#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000047#include <sys/stat.h>
48#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000049#include <ctype.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +000050#include <sys/param.h> /* for PATH_MAX */
Erik Andersend07ee462000-02-21 21:26:32 +000051#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Erik Andersen5cbdd712000-01-26 20:06:48 +000053#if defined BB_FEATURE_MOUNT_LOOP
54#include <fcntl.h>
55#include <sys/ioctl.h>
56#include <linux/loop.h>
57#endif
58
59
Eric Andersend0246fb1999-11-04 21:18:07 +000060#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000061# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000062const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000063# else
64# if defined BB_MTAB
65const char mtab_file[] = "/etc/mtab";
66# else
67# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
68# endif
69# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000070#endif
71
72
Eric Andersend73dc5b1999-11-10 23:13:02 +000073extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000074{
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
76 BB_VER, BB_BT);
77 fprintf(stderr, "Usage: %s\n", usage);
78 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000079}
80
Erik Andersen9ffdaa62000-02-11 21:55:04 +000081extern void errorMsg(char *s, ...)
82{
83 va_list p;
84
85 va_start(p, s);
86 fflush(stdout);
87 fprintf(stderr, "\n");
88 vfprintf(stderr, s, p);
89 fprintf(stderr, "\n");
90 va_end(p);
91}
92
93extern void fatalError(char *s, ...)
94{
95 va_list p;
96
97 va_start(p, s);
98 fflush(stdout);
99 fprintf(stderr, "\n");
100 vfprintf(stderr, s, p);
101 fprintf(stderr, "\n");
102 va_end(p);
103 exit( FALSE);
104}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000105
Eric Andersend23f9ba1999-10-20 19:18:15 +0000106#if defined (BB_INIT) || defined (BB_PS)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000107/* Returns kernel version encoded as major*65536 + minor*256 + patch,
108 * so, for example, to check if the kernel is greater than 2.2.11:
109 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
110 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111int get_kernel_revision()
Eric Andersend23f9ba1999-10-20 19:18:15 +0000112{
Erik Andersend07ee462000-02-21 21:26:32 +0000113 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000115
Erik Andersend07ee462000-02-21 21:26:32 +0000116 if (uname(&name) == -1) {
117 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 return (0);
119 }
Erik Andersend07ee462000-02-21 21:26:32 +0000120 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000122}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123#endif /* BB_INIT || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000124
125
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000126
Erik Andersenfac10d72000-02-07 05:29:42 +0000127#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000128/*
129 * Return TRUE if a fileName is a directory.
130 * Nonexistant files return FALSE.
131 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000132int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000133{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000135 int didMalloc = 0;
136
137 if (statBuf == NULL) {
138 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
139 ++didMalloc;
140 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000141
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000143 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000145 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000146
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000147 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
148 status = FALSE;
149 }
150 else status = TRUE;
151
152 if (didMalloc) {
153 free(statBuf);
154 statBuf = NULL;
155 }
156 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000157}
Erik Andersenfac10d72000-02-07 05:29:42 +0000158#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000159
Erik Andersenfac10d72000-02-07 05:29:42 +0000160#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000161/*
162 * Copy one file to another, while possibly preserving its modes, times,
163 * and modes. Returns TRUE if successful, or FALSE on a failure with an
164 * error message output. (Failure is not indicted if the attributes cannot
165 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000166 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000167 */
168int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169copyFile(const char *srcName, const char *destName,
170 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000171{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 int rfd;
173 int wfd;
174 int rcc;
175 int status;
176 char buf[BUF_SIZE];
177 struct stat srcStatBuf;
178 struct stat dstStatBuf;
179 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000180
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 if (followLinks == TRUE)
182 status = stat(srcName, &srcStatBuf);
183 else
184 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000185
Erik Andersenfac10d72000-02-07 05:29:42 +0000186 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 perror(srcName);
188 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000189 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190
191 if (followLinks == TRUE)
192 status = stat(destName, &dstStatBuf);
193 else
194 status = lstat(destName, &dstStatBuf);
195
196 if (status < 0) {
197 dstStatBuf.st_ino = -1;
198 dstStatBuf.st_dev = -1;
199 }
200
201 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
202 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
203 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
204 return FALSE;
205 }
206
207 if (S_ISDIR(srcStatBuf.st_mode)) {
208 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
209 /* Make sure the directory is writable */
210 status = mkdir(destName, 0777777 ^ umask(0));
211 if (status < 0 && errno != EEXIST) {
212 perror(destName);
213 return FALSE;
214 }
215 } else if (S_ISLNK(srcStatBuf.st_mode)) {
216 char link_val[PATH_MAX + 1];
217 int link_size;
218
219 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
220 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
221 link_size = readlink(srcName, &link_val[0], PATH_MAX);
222 if (link_size < 0) {
223 perror(srcName);
224 return FALSE;
225 }
226 link_val[link_size] = '\0';
227 status = symlink(link_val, destName);
228 if (status < 0) {
229 perror(destName);
230 return FALSE;
231 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000232#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 if (setModes == TRUE) {
234 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
235 perror(destName);
236 return FALSE;
237 }
238 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000239#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 return TRUE;
241 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
242 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
243 if (mkfifo(destName, 0644) < 0) {
244 perror(destName);
245 return FALSE;
246 }
247 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
248 || S_ISSOCK(srcStatBuf.st_mode)) {
249 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
250 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
251 perror(destName);
252 return FALSE;
253 }
254 } else if (S_ISREG(srcStatBuf.st_mode)) {
255 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
256 rfd = open(srcName, O_RDONLY);
257 if (rfd < 0) {
258 perror(srcName);
259 return FALSE;
260 }
261
262 wfd =
263 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
264 srcStatBuf.st_mode);
265 if (wfd < 0) {
266 perror(destName);
267 close(rfd);
268 return FALSE;
269 }
270
271 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
272 if (fullWrite(wfd, buf, rcc) < 0)
273 goto error_exit;
274 }
275 if (rcc < 0) {
276 goto error_exit;
277 }
278
279 close(rfd);
280 if (close(wfd) < 0) {
281 return FALSE;
282 }
283 }
284
285 if (setModes == TRUE) {
286 /* This is fine, since symlinks never get here */
287 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
288 perror(destName);
289 exit FALSE;
290 }
291 if (chmod(destName, srcStatBuf.st_mode) < 0) {
292 perror(destName);
293 exit FALSE;
294 }
295 times.actime = srcStatBuf.st_atime;
296 times.modtime = srcStatBuf.st_mtime;
297 if (utime(destName, &times) < 0) {
298 perror(destName);
299 exit FALSE;
300 }
301 }
302
Erik Andersenfac10d72000-02-07 05:29:42 +0000303 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000304
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 error_exit:
306 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000307 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000309
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000311}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000313
314
315
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000316#if defined BB_TAR || defined BB_LS
317
318#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
319#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
320
321/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
322static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 0, 0, S_ISUID,
324 0, 0, S_ISGID,
325 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000326};
327
328/* The 9 mode bits to test */
329static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 S_IRUSR, S_IWUSR, S_IXUSR,
331 S_IRGRP, S_IWGRP, S_IXGRP,
332 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000333};
334
335#define MODE1 "rwxrwxrwx"
336#define MODE0 "---------"
337#define SMODE1 "..s..s..t"
338#define SMODE0 "..S..S..T"
339
Eric Andersencc8ed391999-10-05 16:24:54 +0000340/*
341 * Return the standard ls-like mode string from a file mode.
342 * This is static and so is overwritten on each call.
343 */
Eric Andersenf811e071999-10-09 00:25:00 +0000344const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000345{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000347
Erik Andersene49d5ec2000-02-08 19:58:47 +0000348 int i;
349
350 buf[0] = TYPECHAR(mode);
351 for (i = 0; i < 9; i++) {
352 if (mode & SBIT[i])
353 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
354 else
355 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
356 }
357 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000358}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000360
361
Eric Andersen1792f8c1999-12-09 06:11:36 +0000362#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000363/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000364 * Return the standard ls-like time string from a time_t
365 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000366 */
Eric Andersenf811e071999-10-09 00:25:00 +0000367const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000368{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 time_t now;
370 char *str;
371 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000372
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000374
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000376
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 strcpy(buf, &str[4]);
378 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000379
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
381 strcpy(&buf[7], &str[20]);
382 buf[11] = '\0';
383 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000384
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000386}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000388
Erik Andersenfac10d72000-02-07 05:29:42 +0000389#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000390/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000391 * Write all of the supplied buffer out to a file.
392 * This does multiple writes as necessary.
393 * Returns the amount written, or -1 on an error.
394 */
Eric Andersenf811e071999-10-09 00:25:00 +0000395int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000396{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397 int cc;
398 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000401
Erik Andersene49d5ec2000-02-08 19:58:47 +0000402 while (len > 0) {
403 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000404
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 if (cc < 0)
406 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000407
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 buf += cc;
409 total += cc;
410 len -= cc;
411 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000412
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000414}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000416
417
Eric Andersen1792f8c1999-12-09 06:11:36 +0000418#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000419/*
420 * Read all of the supplied buffer from a file.
421 * This does multiple reads as necessary.
422 * Returns the amount read, or -1 on an error.
423 * A short read is returned on an end of file.
424 */
Eric Andersenf811e071999-10-09 00:25:00 +0000425int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000426{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 int cc;
428 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000429
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000431
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 while (len > 0) {
433 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000434
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 if (cc < 0)
436 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000437
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 if (cc == 0)
439 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000440
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 buf += cc;
442 total += cc;
443 len -= cc;
444 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000445
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000447}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000449
450
Erik Andersenfac10d72000-02-07 05:29:42 +0000451#if defined (BB_CHMOD_CHOWN_CHGRP) \
452 || defined (BB_CP_MV) \
453 || defined (BB_FIND) \
454 || defined (BB_LS) \
455 || defined (BB_INSMOD)
Eric Andersencc8ed391999-10-05 16:24:54 +0000456/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000457 * Walk down all the directories under the specified
458 * location, and do something (something specified
459 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000460 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000461 * Unfortunatly, while nftw(3) could replace this and reduce
462 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000463 * and so isn't sufficiently portable to take over since glibc2.1
464 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000465 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000466int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 int recurse, int followLinks, int depthFirst,
468 int (*fileAction) (const char *fileName,
469 struct stat * statbuf),
470 int (*dirAction) (const char *fileName,
471 struct stat * statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000472{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 int status;
474 struct stat statbuf;
475 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000476
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477 if (followLinks == TRUE)
478 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000479 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000481
Eric Andersen2b69c401999-10-05 22:58:32 +0000482 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483#ifdef BB_DEBUG_PRINT_SCAFFOLD
484 fprintf(stderr,
485 "status=%d followLinks=%d TRUE=%d\n",
486 status, followLinks, TRUE);
487#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000488 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000489 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000490 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491
492 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
493 if (fileAction == NULL)
494 return TRUE;
495 else
496 return fileAction(fileName, &statbuf);
497 }
498
499 if (recurse == FALSE) {
500 if (S_ISDIR(statbuf.st_mode)) {
501 if (dirAction != NULL)
502 return (dirAction(fileName, &statbuf));
503 else
504 return TRUE;
505 }
506 }
507
508 if (S_ISDIR(statbuf.st_mode)) {
509 DIR *dir;
510
511 dir = opendir(fileName);
512 if (!dir) {
513 perror(fileName);
514 return FALSE;
515 }
516 if (dirAction != NULL && depthFirst == FALSE) {
517 status = dirAction(fileName, &statbuf);
518 if (status == FALSE) {
519 perror(fileName);
520 return FALSE;
521 }
522 }
523 while ((next = readdir(dir)) != NULL) {
524 char nextFile[PATH_MAX + 1];
525
526 if ((strcmp(next->d_name, "..") == 0)
527 || (strcmp(next->d_name, ".") == 0)) {
528 continue;
529 }
530 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
531 fprintf(stderr, name_too_long, "ftw");
532 return FALSE;
533 }
534 sprintf(nextFile, "%s/%s", fileName, next->d_name);
535 status =
536 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
537 fileAction, dirAction);
538 if (status < 0) {
539 closedir(dir);
540 return FALSE;
541 }
542 }
543 status = closedir(dir);
544 if (status < 0) {
545 perror(fileName);
546 return FALSE;
547 }
548 if (dirAction != NULL && depthFirst == TRUE) {
549 status = dirAction(fileName, &statbuf);
550 if (status == FALSE) {
551 perror(fileName);
552 return FALSE;
553 }
554 }
555 } else {
556 if (fileAction == NULL)
557 return TRUE;
558 else
559 return fileAction(fileName, &statbuf);
560 }
561 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000562}
563
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000565
Eric Andersenf6be9441999-10-13 21:12:06 +0000566
567
Erik Andersena8991081999-12-29 03:34:00 +0000568#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000569/*
570 * Attempt to create the directories along the specified path, except for
571 * the final component. The mode is given for the final directory only,
572 * while all previous ones get default protections. Errors are not reported
573 * here, as failures to restore files can be reported later.
574 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000576{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 char *cp;
578 char *cpOld;
579 char buf[PATH_MAX + 1];
580 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000581
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 strcpy(buf, name);
583 for (cp = buf; *cp == '/'; cp++);
584 cp = strchr(cp, '/');
585 while (cp) {
586 cpOld = cp;
587 cp = strchr(cp + 1, '/');
588 *cpOld = '\0';
589 retVal = mkdir(buf, cp ? 0777 : mode);
590 if (retVal != 0 && errno != EEXIST) {
591 perror(buf);
592 return FALSE;
593 }
594 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000595 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000597}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000599
600
601
602#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000603/* [ugoa]{+|-|=}[rwxst] */
604
605
606
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000608{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 mode_t andMode =
610
611 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000612 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 mode_t mode = 0;
614 mode_t groups = 0;
615 char type;
616 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000617
618 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000619 for (;;) {
620 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000621 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000622 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000623 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000625 continue;
626 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000628 continue;
629 case 'o':
630 groups |= S_IRWXO;
631 continue;
632 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000634 continue;
635 case '+':
636 case '=':
637 case '-':
638 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 if (groups == 0) /* The default is "all" */
640 groups |=
641 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000642 break;
643 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 if (isdigit(c) && c >= '0' && c <= '7' &&
645 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000646 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000647 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000649 return (FALSE);
650 }
651 break;
652 }
653
Erik Andersene49d5ec2000-02-08 19:58:47 +0000654 while ((c = *s++) != '\0') {
655 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000656 case ',':
657 break;
658 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000660 continue;
661 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000663 continue;
664 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000666 continue;
667 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000668 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000669 continue;
670 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000671 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000672 continue;
673 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000674 *theMode &= andMode;
675 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000677 }
678 break;
679 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000681 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000682 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000683 /* fall through */
684 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000685 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000686 break;
687 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000688 andMode &= ~(mode & groups);
689 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000690 break;
691 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000693 *theMode &= andMode;
694 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000695 return (TRUE);
696}
Eric Andersence8f3b91999-10-20 07:03:36 +0000697
698
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000700
Eric Andersene674eb71999-10-19 20:52:57 +0000701
702
Eric Andersend23f9ba1999-10-20 19:18:15 +0000703
704
705
706
707#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
708
709/* Use this to avoid needing the glibc NSS stuff
710 * This uses storage buf to hold things.
711 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712uid_t my_getid(const char *filename, char *name, uid_t id)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000713{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000714 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000715 char *rname, *start, *end, buf[128];
716 uid_t rid;
717
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000719 if (file == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 perror(filename);
721 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000722 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000723
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000725 if (buf[0] == '#')
726 continue;
727
728 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000730 if (end == NULL)
731 continue;
732 *end = '\0';
733 rname = start;
734
735 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000737 if (end == NULL)
738 continue;
739
740 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 rid = (uid_t) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000742 if (end == start)
743 continue;
744
745 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 if (0 == strcmp(rname, name)) {
747 fclose(file);
748 return (rid);
749 }
750 }
751 if (id != -1 && id == rid) {
752 strncpy(name, rname, 8);
753 fclose(file);
754 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000755 }
756 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000757 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000758 return (-1);
759}
760
Erik Andersene49d5ec2000-02-08 19:58:47 +0000761uid_t my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000762{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000763 return my_getid("/etc/passwd", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000764}
765
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766gid_t my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000767{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000768 return my_getid("/etc/group", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000769}
770
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771void my_getpwuid(char *name, uid_t uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000772{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 my_getid("/etc/passwd", name, uid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000774}
775
Erik Andersene49d5ec2000-02-08 19:58:47 +0000776void my_getgrgid(char *group, gid_t gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000777{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000778 my_getid("/etc/group", group, gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000779}
780
781
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000783
784
Eric Andersenaa0765e1999-10-22 04:30:20 +0000785
Eric Andersen0460ff21999-10-25 23:32:44 +0000786
787#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
788
789
790#include <linux/kd.h>
791#include <sys/ioctl.h>
792
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000794{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 char arg;
796
797 arg = 0;
798 return (ioctl(fd, KDGKBTYPE, &arg) == 0
799 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000800}
801
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000803{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 int fd;
805
806 /* try read-only */
807 fd = open(fnam, O_RDWR);
808
809 /* if failed, try read-only */
810 if (fd < 0 && errno == EACCES)
811 fd = open(fnam, O_RDONLY);
812
813 /* if failed, try write-only */
814 if (fd < 0 && errno == EACCES)
815 fd = open(fnam, O_WRONLY);
816
817 /* if failed, fail */
818 if (fd < 0)
819 return -1;
820
821 /* if not a console, fail */
822 if (!is_a_console(fd)) {
823 close(fd);
824 return -1;
825 }
826
827 /* success */
828 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000829}
830
831/*
832 * Get an fd for use with kbd/console ioctls.
833 * We try several things because opening /dev/console will fail
834 * if someone else used X (which does a chown on /dev/console).
835 *
836 * if tty_name is non-NULL, try this one instead.
837 */
838
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000840{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000842
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843 if (tty_name) {
844 if (-1 == (fd = open_a_console(tty_name)))
845 return -1;
846 else
847 return fd;
848 }
849
850 fd = open_a_console("/dev/tty");
851 if (fd >= 0)
852 return fd;
853
854 fd = open_a_console("/dev/tty0");
855 if (fd >= 0)
856 return fd;
857
858 fd = open_a_console("/dev/console");
859 if (fd >= 0)
860 return fd;
861
862 for (fd = 0; fd < 3; fd++)
863 if (is_a_console(fd))
864 return fd;
865
866 fprintf(stderr,
867 "Couldnt get a file descriptor referring to the console\n");
868 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000869}
870
871
Erik Andersene49d5ec2000-02-08 19:58:47 +0000872#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000873
874
Erik Andersene49d5ec2000-02-08 19:58:47 +0000875#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000876
877/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000878char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000879{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000881
Erik Andersene49d5ec2000-02-08 19:58:47 +0000882 while (*haystack) {
883 if (!strncasecmp(haystack, needle, len))
884 break;
885 haystack++;
886 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000887
Erik Andersene49d5ec2000-02-08 19:58:47 +0000888 if (!(*haystack))
889 haystack = NULL;
890
891 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000892}
893
Eric Andersenc1525e81999-10-29 00:07:31 +0000894/* This tries to find a needle in a haystack, but does so by
895 * only trying to match literal strings (look 'ma, no regexps!)
896 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000897 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000898 * -Erik Andersen
899 */
900extern int find_match(char *haystack, char *needle, int ignoreCase)
901{
902
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 if (ignoreCase == FALSE)
904 haystack = strstr(haystack, needle);
905 else
906 haystack = stristr(haystack, needle);
907 if (haystack == NULL)
908 return FALSE;
909 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000910}
911
912
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000913/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000914extern int replace_match(char *haystack, char *needle, char *newNeedle,
915 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +0000916{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917 int foundOne = 0;
918 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000919
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 if (ignoreCase == FALSE)
921 where = strstr(haystack, needle);
922 else
923 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000924
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 if (strcmp(needle, newNeedle) == 0)
926 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000927
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928 oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
929 while (where != NULL) {
930 foundOne++;
931 strcpy(oldhayStack, haystack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000932#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 if (strlen(newNeedle) > strlen(needle)) {
934 haystack =
935 (char *) realloc(haystack,
936 (unsigned) (strlen(haystack) -
937 strlen(needle) +
938 strlen(newNeedle)));
939 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000940#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000941 for (slider = haystack, slider1 = oldhayStack; slider != where;
942 slider++, slider1++);
943 *slider = 0;
944 haystack = strcat(haystack, newNeedle);
945 slider1 += strlen(needle);
946 haystack = strcat(haystack, slider1);
947 where = strstr(slider, needle);
948 }
949 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000950
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951 if (foundOne > 0)
952 return TRUE;
953 else
954 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000955}
956
Erik Andersene49d5ec2000-02-08 19:58:47 +0000957#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +0000958
959
Eric Andersenb186d981999-12-03 09:19:54 +0000960#if defined BB_FIND
961/*
962 * Routine to see if a text string is matched by a wildcard pattern.
963 * Returns TRUE if the text is matched, or FALSE if it is not matched
964 * or if the pattern is invalid.
965 * * matches zero or more characters
966 * ? matches a single character
967 * [abc] matches 'a', 'b' or 'c'
968 * \c quotes character c
969 * Adapted from code written by Ingo Wilken, and
970 * then taken from sash, Copyright (c) 1999 by David I. Bell
971 * Permission is granted to use, distribute, or modify this source,
972 * provided that this copyright notice remains intact.
973 * Permission to distribute this code under the GPL has been granted.
974 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +0000976{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000977 const char *retryPat;
978 const char *retryText;
979 int ch;
980 int found;
Eric Andersenb186d981999-12-03 09:19:54 +0000981
Erik Andersene49d5ec2000-02-08 19:58:47 +0000982 retryPat = NULL;
983 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +0000984
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +0000986 ch = *pattern++;
987
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 switch (ch) {
989 case '*':
990 retryPat = pattern;
991 retryText = text;
992 break;
993
994 case '[':
995 found = FALSE;
996
997 while ((ch = *pattern++) != ']') {
998 if (ch == '\\')
999 ch = *pattern++;
1000
1001 if (ch == '\0')
1002 return FALSE;
1003
1004 if (*text == ch)
1005 found = TRUE;
1006 }
1007
1008 //if (!found)
1009 if (found == TRUE) {
1010 pattern = retryPat;
1011 text = ++retryText;
1012 }
1013
1014 /* fall into next case */
1015
1016 case '?':
1017 if (*text++ == '\0')
1018 return FALSE;
1019
1020 break;
1021
1022 case '\\':
1023 ch = *pattern++;
1024
1025 if (ch == '\0')
1026 return FALSE;
1027
1028 /* fall into next case */
1029
1030 default:
1031 if (*text == ch) {
1032 if (*text)
1033 text++;
1034 break;
1035 }
1036
1037 if (*text) {
1038 pattern = retryPat;
1039 text = ++retryText;
1040 break;
1041 }
1042
Eric Andersenb186d981999-12-03 09:19:54 +00001043 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001044 }
1045
Erik Andersene49d5ec2000-02-08 19:58:47 +00001046 if (pattern == NULL)
1047 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001048 }
1049
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001051}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001053
1054
Eric Andersen29d2e361999-11-06 06:07:27 +00001055
1056
Erik Andersen7dc16072000-01-04 01:10:25 +00001057#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001058/*
1059 * Given a block device, find the mount table entry if that block device
1060 * is mounted.
1061 *
1062 * Given any other file (or directory), find the mount table entry for its
1063 * filesystem.
1064 */
1065extern struct mntent *findMountPoint(const char *name, const char *table)
1066{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001067 struct stat s;
1068 dev_t mountDevice;
1069 FILE *mountTable;
1070 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001071
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 if (stat(name, &s) != 0)
1073 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001074
Erik Andersene49d5ec2000-02-08 19:58:47 +00001075 if ((s.st_mode & S_IFMT) == S_IFBLK)
1076 mountDevice = s.st_rdev;
1077 else
1078 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001079
1080
Erik Andersene49d5ec2000-02-08 19:58:47 +00001081 if ((mountTable = setmntent(table, "r")) == 0)
1082 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001083
Erik Andersene49d5ec2000-02-08 19:58:47 +00001084 while ((mountEntry = getmntent(mountTable)) != 0) {
1085 if (strcmp(name, mountEntry->mnt_dir) == 0
1086 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1087 break;
1088 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1089 break;
1090 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1091 break;
1092 }
1093 endmntent(mountTable);
1094 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001095}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001096#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001097
1098
1099
Eric Andersen1792f8c1999-12-09 06:11:36 +00001100#if defined BB_DD || defined BB_TAIL
1101/*
1102 * Read a number with a possible multiplier.
1103 * Returns -1 if the number format is illegal.
1104 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001105extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001106{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001108
Erik Andersene49d5ec2000-02-08 19:58:47 +00001109 if (!isDecimal(*cp))
1110 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001111
Erik Andersene49d5ec2000-02-08 19:58:47 +00001112 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001113
Erik Andersene49d5ec2000-02-08 19:58:47 +00001114 while (isDecimal(*cp))
1115 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001116
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 switch (*cp++) {
1118 case 'M':
1119 case 'm': /* `tail' uses it traditionally */
1120 value *= 1048576;
1121 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001122
Erik Andersene49d5ec2000-02-08 19:58:47 +00001123 case 'k':
1124 value *= 1024;
1125 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001126
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 case 'b':
1128 value *= 512;
1129 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001130
Erik Andersene49d5ec2000-02-08 19:58:47 +00001131 case 'w':
1132 value *= 2;
1133 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001134
Erik Andersene49d5ec2000-02-08 19:58:47 +00001135 case '\0':
1136 return value;
1137
1138 default:
1139 return -1;
1140 }
1141
1142 if (*cp)
1143 return -1;
1144
Eric Andersen1792f8c1999-12-09 06:11:36 +00001145 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001146}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001147#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001148
Eric Andersen6805d5d1999-12-09 22:39:55 +00001149
Erik Andersen31cf8e02000-02-07 20:31:19 +00001150#if defined BB_INIT || defined BB_SYSLOGD
1151/* try to open up the specified device */
1152extern int device_open(char *device, int mode)
1153{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001155
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001157
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 /* Retry up to 5 times */
1159 for (f = 0; f < 5; f++)
1160 if ((fd = open(device, m, 0600)) >= 0)
1161 break;
1162 if (fd < 0)
1163 return fd;
1164 /* Reset original flags. */
1165 if (m != mode)
1166 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001167 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001168}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001170
1171
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
Eric Andersen6805d5d1999-12-09 22:39:55 +00001173
1174#if ! defined BB_FEATURE_USE_PROCFS
1175#error Sorry, I depend on the /proc filesystem right now.
1176#endif
John Beppuf95ca971999-12-09 22:10:18 +00001177/* findInitPid()
1178 *
1179 * This finds the pid of init (which is not always 1).
1180 * Currently, it's implemented by rummaging through the proc filesystem.
1181 *
1182 * [return]
1183 * 0 failure
1184 * pid when init's pid is found.
1185 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001186extern pid_t findInitPid()
John Beppuf95ca971999-12-09 22:10:18 +00001187{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001188 pid_t init_pid;
1189 char filename[256];
1190 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 /* no need to opendir ;) */
1193 for (init_pid = 1; init_pid < 65536; init_pid++) {
1194 FILE *status;
John Beppuf95ca971999-12-09 22:10:18 +00001195
Erik Andersene49d5ec2000-02-08 19:58:47 +00001196 sprintf(filename, "/proc/%d/status", init_pid);
1197 status = fopen(filename, "r");
1198 if (!status) {
1199 continue;
1200 }
1201 fgets(buffer, 256, status);
1202 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001203
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 if ((strstr(buffer, "init\n") != NULL)) {
1205 return init_pid;
1206 }
John Beppuf95ca971999-12-09 22:10:18 +00001207 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001208 return 0;
John Beppuf95ca971999-12-09 22:10:18 +00001209}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210#endif /* BB_INIT || BB_HALT || BB_REBOOT */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001211
Erik Andersenfac10d72000-02-07 05:29:42 +00001212#if defined BB_GUNZIP \
1213 || defined BB_GZIP \
1214 || defined BB_PRINTF \
1215 || defined BB_TAIL
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001217{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001218 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001219
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 if (cp == NULL) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +00001221 errorMsg("out of memory");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 }
1223 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001224}
1225
Erik Andersene49d5ec2000-02-08 19:58:47 +00001226#endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001227
Erik Andersen7dc16072000-01-04 01:10:25 +00001228#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1229extern int vdprintf(int d, const char *format, va_list ap)
1230{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001231 char buf[BUF_SIZE];
1232 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001233
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 len = vsprintf(buf, format, ap);
1235 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001236}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001237#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001238
Erik Andersen5cbdd712000-01-26 20:06:48 +00001239#if defined BB_FEATURE_MOUNT_LOOP
1240extern int del_loop(const char *device)
1241{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001242 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001243
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 if ((fd = open(device, O_RDONLY)) < 0) {
1245 perror(device);
1246 return (FALSE);
1247 }
1248 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1249 perror("ioctl: LOOP_CLR_FD");
1250 return (FALSE);
1251 }
1252 close(fd);
1253 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001254}
Erik Andersende7965c2000-01-26 23:49:21 +00001255
Erik Andersene49d5ec2000-02-08 19:58:47 +00001256extern int set_loop(const char *device, const char *file, int offset,
1257 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001258{
1259 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001260 int fd, ffd, mode;
1261
Erik Andersende7965c2000-01-26 23:49:21 +00001262 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001263 if ((ffd = open(file, mode)) < 0 && !*loopro
1264 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1265 perror(file);
1266 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001267 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001268 if ((fd = open(device, mode)) < 0) {
1269 close(ffd);
1270 perror(device);
1271 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001272 }
1273 *loopro = (mode == O_RDONLY);
1274
1275 memset(&loopinfo, 0, sizeof(loopinfo));
1276 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001277 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001278
1279 loopinfo.lo_offset = offset;
1280
1281 loopinfo.lo_encrypt_key_size = 0;
1282 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1283 perror("ioctl: LOOP_SET_FD");
1284 close(fd);
1285 close(ffd);
1286 return 1;
1287 }
1288 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1289 (void) ioctl(fd, LOOP_CLR_FD, 0);
1290 perror("ioctl: LOOP_SET_STATUS");
1291 close(fd);
1292 close(ffd);
1293 return 1;
1294 }
1295 close(fd);
1296 close(ffd);
1297 return 0;
1298}
1299
Erik Andersene49d5ec2000-02-08 19:58:47 +00001300extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001301{
1302 char dev[20];
1303 int i, fd;
1304 struct stat statbuf;
1305 struct loop_info loopinfo;
1306
Erik Andersene49d5ec2000-02-08 19:58:47 +00001307 for (i = 0; i <= 7; i++) {
1308 sprintf(dev, "/dev/loop%d", i);
1309 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1310 if ((fd = open(dev, O_RDONLY)) >= 0) {
1311 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1312 if (errno == ENXIO) { /* probably free */
1313 close(fd);
1314 return strdup(dev);
1315 }
1316 }
1317 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001318 }
Erik Andersende7965c2000-01-26 23:49:21 +00001319 }
Erik Andersende7965c2000-01-26 23:49:21 +00001320 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001321 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001322}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001323#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001324
Erik Andersene132f4b2000-02-09 04:16:43 +00001325#if defined BB_MTAB
1326#define whine_if_fstab_is_missing() {}
1327#else
1328extern void whine_if_fstab_is_missing()
1329{
1330 struct stat statBuf;
1331
1332 if (stat("/etc/fstab", &statBuf) < 0)
1333 fprintf(stderr,
1334 "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1335}
1336#endif
1337
1338
Erik Andersen5cbdd712000-01-26 20:06:48 +00001339
Eric Andersencc8ed391999-10-05 16:24:54 +00001340/* END CODE */