blob: 6d7fa955b69c6464f930ec58436f19b6bde76116 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
2 * Utility routines.
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 * Copyright (C) tons of folks. Tracking down who wrote what
5 * isn't something I'm going to worry about... If you wrote something
6 * here, please feel free to acknowledge your work.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
23 * Permission has been granted to redistribute this code under the GPL.
24 *
25 */
26
Eric Andersen2b69c401999-10-05 22:58:32 +000027#include "internal.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000028#if defined (BB_CHMOD_CHOWN_CHGRP) \
29 || defined (BB_CP_MV) \
30 || defined (BB_FIND) \
31 || defined (BB_LS) \
32 || defined (BB_INSMOD)
33/* same conditions as recursiveAction */
34#define bb_need_name_too_long
35#endif
36#define BB_DECLARE_EXTERN
37#include "messages.c"
38
Eric Andersen2b69c401999-10-05 22:58:32 +000039#include <stdio.h>
40#include <string.h>
41#include <errno.h>
42#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000043#include <dirent.h>
44#include <time.h>
45#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000046#include <sys/stat.h>
47#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000048#include <ctype.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000049#include <sys/param.h> /* for PATH_MAX */
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Erik Andersen5cbdd712000-01-26 20:06:48 +000051#if defined BB_FEATURE_MOUNT_LOOP
52#include <fcntl.h>
53#include <sys/ioctl.h>
54#include <linux/loop.h>
55#endif
56
57
Eric Andersend0246fb1999-11-04 21:18:07 +000058#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000059# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000060const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000061# else
62# if defined BB_MTAB
63const char mtab_file[] = "/etc/mtab";
64# else
65# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
66# endif
67# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000068#endif
69
70
Eric Andersend73dc5b1999-11-10 23:13:02 +000071extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000072{
Erik Andersenfac10d72000-02-07 05:29:42 +000073 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
74 BB_VER, BB_BT);
Eric Andersenb0e9a701999-10-18 22:28:26 +000075 fprintf(stderr, "Usage: %s\n", usage);
Erik Andersenfac10d72000-02-07 05:29:42 +000076 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000077}
78
79
Eric Andersend23f9ba1999-10-20 19:18:15 +000080#if defined (BB_INIT) || defined (BB_PS)
81
Eric Andersen0ecb54a1999-12-05 23:24:55 +000082#if ! defined BB_FEATURE_USE_PROCFS
83#error Sorry, I depend on the /proc filesystem right now.
84#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000085/* Returns kernel version encoded as major*65536 + minor*256 + patch,
86 * so, for example, to check if the kernel is greater than 2.2.11:
87 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
88 */
89int
90get_kernel_revision()
91{
Eric Andersenaa0765e1999-10-22 04:30:20 +000092 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +000093 int major=0, minor=0, patch=0;
94
Erik Andersenfac10d72000-02-07 05:29:42 +000095 file = fopen("/proc/sys/kernel/osrelease", "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +000096 if (file == NULL) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +000097 /* bummer, /proc must not be mounted... */
Eric Andersenaa0765e1999-10-22 04:30:20 +000098 return( 0);
99 }
100 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
101 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000102 return major*65536 + minor*256 + patch;
103}
Erik Andersenfac10d72000-02-07 05:29:42 +0000104#endif /* BB_INIT || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000105
106
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000107
Erik Andersenfac10d72000-02-07 05:29:42 +0000108#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000109/*
110 * Return TRUE if a fileName is a directory.
111 * Nonexistant files return FALSE.
112 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000113int isDirectory(const char *fileName, const int followLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000114{
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000115 struct stat statBuf;
Erik Andersenfac10d72000-02-07 05:29:42 +0000116 int status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Erik Andersenfac10d72000-02-07 05:29:42 +0000118 if (followLinks == TRUE)
119 status = stat(fileName, &statBuf);
120 else
121 status = lstat(fileName, &statBuf);
122
123 if (status < 0)
124 return FALSE;
Eric Andersen9b587181999-10-17 05:43:39 +0000125 if (S_ISDIR(statBuf.st_mode))
Erik Andersenfac10d72000-02-07 05:29:42 +0000126 return TRUE;
127 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000128}
Erik Andersenfac10d72000-02-07 05:29:42 +0000129#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000130
Erik Andersenfac10d72000-02-07 05:29:42 +0000131#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000132/*
133 * Copy one file to another, while possibly preserving its modes, times,
134 * and modes. Returns TRUE if successful, or FALSE on a failure with an
135 * error message output. (Failure is not indicted if the attributes cannot
136 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000137 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000138 */
139int
Eric Andersen3c163821999-10-14 22:16:57 +0000140copyFile( const char *srcName, const char *destName,
Erik Andersenfac10d72000-02-07 05:29:42 +0000141 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000142{
143 int rfd;
144 int wfd;
145 int rcc;
Erik Andersenfac10d72000-02-07 05:29:42 +0000146 int status;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000147 char buf[BUF_SIZE];
148 struct stat srcStatBuf;
149 struct stat dstStatBuf;
150 struct utimbuf times;
151
Erik Andersenfac10d72000-02-07 05:29:42 +0000152 if (followLinks == TRUE)
153 status = stat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000154 else
Erik Andersenfac10d72000-02-07 05:29:42 +0000155 status = lstat(srcName, &srcStatBuf);
156
157 if (status < 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000158 perror(srcName);
159 return FALSE;
160 }
161
Erik Andersenfac10d72000-02-07 05:29:42 +0000162 if (followLinks == TRUE)
163 status = stat(destName, &dstStatBuf);
164 else
165 status = lstat(destName, &dstStatBuf);
166
167 if (status < 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000168 dstStatBuf.st_ino = -1;
169 dstStatBuf.st_dev = -1;
170 }
171
172 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
173 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
174 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
175 return FALSE;
176 }
177
178 if (S_ISDIR(srcStatBuf.st_mode)) {
179 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
180 /* Make sure the directory is writable */
Erik Andersenfac10d72000-02-07 05:29:42 +0000181 status = mkdir(destName, 0777777 ^ umask(0));
182 if (status < 0 && errno != EEXIST) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000183 perror(destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000184 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000185 }
186 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersenfac10d72000-02-07 05:29:42 +0000187 char link_val[PATH_MAX + 1];
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000188 int link_size;
189
190 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000191 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
192 link_size = readlink(srcName, &link_val[0], PATH_MAX);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000193 if (link_size < 0) {
194 perror(srcName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000195 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000196 }
197 link_val[link_size] = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000198 status = symlink(link_val, destName);
199 if (status < 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000200 perror(destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000201 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000202 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000203#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
204 if (setModes == TRUE) {
Erik Andersenfac10d72000-02-07 05:29:42 +0000205 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
206 perror(destName);
207 return FALSE;
208 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000209 }
210#endif
Erik Andersenfac10d72000-02-07 05:29:42 +0000211 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000212 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
213 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000214 if (mkfifo(destName, 0644) < 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000215 perror(destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000216 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000217 }
218 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
Erik Andersenfac10d72000-02-07 05:29:42 +0000219 || S_ISSOCK (srcStatBuf.st_mode)) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000220 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000221 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000222 perror(destName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000223 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000224 }
225 } else if (S_ISREG(srcStatBuf.st_mode)) {
226 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
227 rfd = open(srcName, O_RDONLY);
228 if (rfd < 0) {
229 perror(srcName);
230 return FALSE;
231 }
232
Erik Andersenfac10d72000-02-07 05:29:42 +0000233 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC, srcStatBuf.st_mode);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000234 if (wfd < 0) {
235 perror(destName);
236 close(rfd);
237 return FALSE;
238 }
239
240 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
241 if (fullWrite(wfd, buf, rcc) < 0)
242 goto error_exit;
243 }
244 if (rcc < 0) {
245 goto error_exit;
246 }
247
248 close(rfd);
249 if (close(wfd) < 0) {
250 return FALSE;
251 }
252 }
253
254 if (setModes == TRUE) {
Erik Andersen8e759aa2000-01-29 05:52:40 +0000255 /* This is fine, since symlinks never get here */
Erik Andersenfac10d72000-02-07 05:29:42 +0000256 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
257 perror(destName);
258 exit FALSE;
259 }
260 if (chmod(destName, srcStatBuf.st_mode) < 0) {
261 perror(destName);
262 exit FALSE;
263 }
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000264 times.actime = srcStatBuf.st_atime;
265 times.modtime = srcStatBuf.st_mtime;
Erik Andersenfac10d72000-02-07 05:29:42 +0000266 if (utime(destName, &times) < 0) {
267 perror(destName);
268 exit FALSE;
269 }
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000270 }
271
272 return TRUE;
273
Erik Andersenfac10d72000-02-07 05:29:42 +0000274 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000275 perror(destName);
276 close(rfd);
277 close(wfd);
278
279 return FALSE;
280}
Erik Andersenfac10d72000-02-07 05:29:42 +0000281#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000282
283
284
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000285#if defined BB_TAR || defined BB_LS
286
287#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
288#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
289
290/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
291static const mode_t SBIT[] = {
292 0, 0, S_ISUID,
293 0, 0, S_ISGID,
294 0, 0, S_ISVTX
295};
296
297/* The 9 mode bits to test */
298static const mode_t MBIT[] = {
299 S_IRUSR, S_IWUSR, S_IXUSR,
300 S_IRGRP, S_IWGRP, S_IXGRP,
301 S_IROTH, S_IWOTH, S_IXOTH
302};
303
304#define MODE1 "rwxrwxrwx"
305#define MODE0 "---------"
306#define SMODE1 "..s..s..t"
307#define SMODE0 "..S..S..T"
308
Eric Andersencc8ed391999-10-05 16:24:54 +0000309/*
310 * Return the standard ls-like mode string from a file mode.
311 * This is static and so is overwritten on each call.
312 */
Eric Andersenf811e071999-10-09 00:25:00 +0000313const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000314{
Eric Andersenf811e071999-10-09 00:25:00 +0000315 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000316
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000317 int i;
318 buf[0] = TYPECHAR(mode);
319 for (i=0; i<9; i++) {
320 if (mode & SBIT[i])
321 buf[i+1] = (mode & MBIT[i])?
322 SMODE1[i] : SMODE0[i];
323 else
324 buf[i+1] = (mode & MBIT[i])?
325 MODE1[i] : MODE0[i];
326 }
Eric Andersenf811e071999-10-09 00:25:00 +0000327 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000328}
Erik Andersenfac10d72000-02-07 05:29:42 +0000329#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000330
331
Eric Andersen1792f8c1999-12-09 06:11:36 +0000332#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000333/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000334 * Return the standard ls-like time string from a time_t
335 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000336 */
Eric Andersenf811e071999-10-09 00:25:00 +0000337const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000338{
Eric Andersenf811e071999-10-09 00:25:00 +0000339 time_t now;
340 char *str;
341 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000342
Eric Andersenf811e071999-10-09 00:25:00 +0000343 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000344
Eric Andersenf811e071999-10-09 00:25:00 +0000345 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000346
Eric Andersenf811e071999-10-09 00:25:00 +0000347 strcpy(buf, &str[4]);
348 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000349
Eric Andersenf811e071999-10-09 00:25:00 +0000350 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
351 strcpy(&buf[7], &str[20]);
352 buf[11] = '\0';
353 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000354
Eric Andersenf811e071999-10-09 00:25:00 +0000355 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000356}
Erik Andersenfac10d72000-02-07 05:29:42 +0000357#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000358
Erik Andersenfac10d72000-02-07 05:29:42 +0000359#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000360/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000361 * Write all of the supplied buffer out to a file.
362 * This does multiple writes as necessary.
363 * Returns the amount written, or -1 on an error.
364 */
Eric Andersenf811e071999-10-09 00:25:00 +0000365int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000366{
Eric Andersenf811e071999-10-09 00:25:00 +0000367 int cc;
368 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000369
Eric Andersenf811e071999-10-09 00:25:00 +0000370 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000371
Eric Andersenf811e071999-10-09 00:25:00 +0000372 while (len > 0) {
373 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000374
Eric Andersenf811e071999-10-09 00:25:00 +0000375 if (cc < 0)
376 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000377
Eric Andersenf811e071999-10-09 00:25:00 +0000378 buf += cc;
379 total += cc;
380 len -= cc;
381 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000382
Eric Andersenf811e071999-10-09 00:25:00 +0000383 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000384}
Erik Andersenfac10d72000-02-07 05:29:42 +0000385#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000386
387
Eric Andersen1792f8c1999-12-09 06:11:36 +0000388#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000389/*
390 * Read all of the supplied buffer from a file.
391 * This does multiple reads as necessary.
392 * Returns the amount read, or -1 on an error.
393 * A short read is returned on an end of file.
394 */
Eric Andersenf811e071999-10-09 00:25:00 +0000395int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000396{
Eric Andersenf811e071999-10-09 00:25:00 +0000397 int cc;
398 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Eric Andersenf811e071999-10-09 00:25:00 +0000400 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000401
Eric Andersenf811e071999-10-09 00:25:00 +0000402 while (len > 0) {
403 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000404
Eric Andersenf811e071999-10-09 00:25:00 +0000405 if (cc < 0)
406 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000407
Eric Andersenf811e071999-10-09 00:25:00 +0000408 if (cc == 0)
409 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000410
Eric Andersenf811e071999-10-09 00:25:00 +0000411 buf += cc;
412 total += cc;
413 len -= cc;
414 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000415
Eric Andersenf811e071999-10-09 00:25:00 +0000416 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000417}
Erik Andersenfac10d72000-02-07 05:29:42 +0000418#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000419
420
Erik Andersenfac10d72000-02-07 05:29:42 +0000421#if defined (BB_CHMOD_CHOWN_CHGRP) \
422 || defined (BB_CP_MV) \
423 || defined (BB_FIND) \
424 || defined (BB_LS) \
425 || defined (BB_INSMOD)
Eric Andersencc8ed391999-10-05 16:24:54 +0000426/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000427 * Walk down all the directories under the specified
428 * location, and do something (something specified
429 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000430 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000431 * Unfortunatly, while nftw(3) could replace this and reduce
432 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000433 * and so isn't sufficiently portable to take over since glibc2.1
434 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000435 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000436int recursiveAction(const char *fileName,
437 int recurse, int followLinks, int depthFirst,
438 int (*fileAction) (const char *fileName,
439 struct stat* statbuf),
440 int (*dirAction) (const char *fileName,
441 struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000442{
Eric Andersenf811e071999-10-09 00:25:00 +0000443 int status;
Erik Andersenfac10d72000-02-07 05:29:42 +0000444 struct stat statbuf;
Eric Andersenf811e071999-10-09 00:25:00 +0000445 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000446
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000447 if (followLinks == TRUE)
Eric Andersenf811e071999-10-09 00:25:00 +0000448 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000449 else
450 status = lstat(fileName, &statbuf);
451
Eric Andersencc8ed391999-10-05 16:24:54 +0000452 if (status < 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +0000453#ifdef BB_DEBUG_PRINT_SCAFFOLD
454 fprintf(stderr,
455 "status=%d followLinks=%d TRUE=%d\n",
456 status, followLinks, TRUE);
457#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000458 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000459 return FALSE;
Eric Andersenf811e071999-10-09 00:25:00 +0000460 }
461
Erik Andersenfac10d72000-02-07 05:29:42 +0000462 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
Eric Andersen50d63601999-11-09 01:47:36 +0000463 if (fileAction == NULL)
Erik Andersenfac10d72000-02-07 05:29:42 +0000464 return TRUE;
Eric Andersen50d63601999-11-09 01:47:36 +0000465 else
Erik Andersenfac10d72000-02-07 05:29:42 +0000466 return fileAction(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000467 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000468
Eric Andersenf811e071999-10-09 00:25:00 +0000469 if (recurse == FALSE) {
470 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000471 if (dirAction != NULL)
Eric Andersen9b587181999-10-17 05:43:39 +0000472 return (dirAction(fileName, &statbuf));
Eric Andersenf811e071999-10-09 00:25:00 +0000473 else
Erik Andersenfac10d72000-02-07 05:29:42 +0000474 return TRUE;
475 }
Erik Andersen05df2392000-01-13 04:43:48 +0000476 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000477
Erik Andersenfac10d72000-02-07 05:29:42 +0000478 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen2b69c401999-10-05 22:58:32 +0000479 DIR *dir;
480 dir = opendir(fileName);
481 if (!dir) {
482 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000483 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000484 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000485 if (dirAction != NULL && depthFirst == FALSE) {
Eric Andersen9b587181999-10-17 05:43:39 +0000486 status = dirAction(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000487 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000488 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000489 return FALSE;
Eric Andersenf811e071999-10-09 00:25:00 +0000490 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000491 }
Eric Andersenf811e071999-10-09 00:25:00 +0000492 while ((next = readdir(dir)) != NULL) {
Erik Andersenfac10d72000-02-07 05:29:42 +0000493 char nextFile[PATH_MAX + 1];
Eric Andersenf811e071999-10-09 00:25:00 +0000494 if ((strcmp(next->d_name, "..") == 0)
Erik Andersenfac10d72000-02-07 05:29:42 +0000495 || (strcmp(next->d_name, ".") == 0)) {
Eric Andersenf811e071999-10-09 00:25:00 +0000496 continue;
497 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000498 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
499 fprintf(stderr, name_too_long, "ftw");
500 return FALSE;
501 }
Eric Andersenf811e071999-10-09 00:25:00 +0000502 sprintf(nextFile, "%s/%s", fileName, next->d_name);
503 status =
Eric Andersen63a0e531999-10-22 05:12:14 +0000504 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersenfac10d72000-02-07 05:29:42 +0000505 fileAction, dirAction);
Eric Andersenf811e071999-10-09 00:25:00 +0000506 if (status < 0) {
507 closedir(dir);
Erik Andersenfac10d72000-02-07 05:29:42 +0000508 return FALSE;
Eric Andersenf811e071999-10-09 00:25:00 +0000509 }
510 }
511 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000512 if (status < 0) {
513 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000514 return FALSE;
Eric Andersen2b69c401999-10-05 22:58:32 +0000515 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000516 if (dirAction != NULL && depthFirst == TRUE) {
Eric Andersenbed30e91999-10-18 19:02:32 +0000517 status = dirAction(fileName, &statbuf);
518 if (status == FALSE) {
519 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000520 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000521 }
522 }
Eric Andersenf811e071999-10-09 00:25:00 +0000523 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000524 if (fileAction == NULL)
Erik Andersenfac10d72000-02-07 05:29:42 +0000525 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000526 else
Erik Andersenfac10d72000-02-07 05:29:42 +0000527 return fileAction(fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000528 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000529 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000530}
531
Erik Andersenfac10d72000-02-07 05:29:42 +0000532#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000533
Eric Andersenf6be9441999-10-13 21:12:06 +0000534
535
Erik Andersena8991081999-12-29 03:34:00 +0000536#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000537/*
538 * Attempt to create the directories along the specified path, except for
539 * the final component. The mode is given for the final directory only,
540 * while all previous ones get default protections. Errors are not reported
541 * here, as failures to restore files can be reported later.
542 */
Erik Andersen06936df2000-01-23 02:14:20 +0000543extern int createPath (const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000544{
545 char *cp;
546 char *cpOld;
Erik Andersenfac10d72000-02-07 05:29:42 +0000547 char buf[PATH_MAX + 1];
Erik Andersen06936df2000-01-23 02:14:20 +0000548 int retVal=0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000549
Eric Andersencb41c2e1999-11-22 07:41:00 +0000550 strcpy( buf, name);
Erik Andersenfac10d72000-02-07 05:29:42 +0000551 cp = strchr(buf, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000552 while (cp) {
553 cpOld = cp;
Erik Andersenfac10d72000-02-07 05:29:42 +0000554 cp = strchr(cp + 1, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000555 *cpOld = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000556 retVal = mkdir(buf, cp ? 0777 : mode);
Erik Andersence5b4662000-01-27 19:50:47 +0000557 if (retVal != 0 && errno != EEXIST) {
Erik Andersenfac10d72000-02-07 05:29:42 +0000558 perror(buf);
559 return FALSE;
Erik Andersen06936df2000-01-23 02:14:20 +0000560 }
Erik Andersence5b4662000-01-27 19:50:47 +0000561 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000562 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000563 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000564}
Erik Andersenfac10d72000-02-07 05:29:42 +0000565#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000566
567
568
569#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000570/* [ugoa]{+|-|=}[rwxst] */
571
572
573
574extern int
575parse_mode( const char* s, mode_t* theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000576{
Eric Andersence8f3b91999-10-20 07:03:36 +0000577 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
578 mode_t orMode = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000579 mode_t mode = 0;
Eric Andersence8f3b91999-10-20 07:03:36 +0000580 mode_t groups = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000581 char type;
582 char c;
583
584 do {
585 for ( ; ; ) {
586 switch ( c = *s++ ) {
587 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000588 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000589 case 'u':
590 groups |= S_ISUID|S_IRWXU;
591 continue;
592 case 'g':
593 groups |= S_ISGID|S_IRWXG;
594 continue;
595 case 'o':
596 groups |= S_IRWXO;
597 continue;
598 case 'a':
599 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
600 continue;
601 case '+':
602 case '=':
603 case '-':
604 type = c;
Eric Andersence8f3b91999-10-20 07:03:36 +0000605 if ( groups == 0 ) /* The default is "all" */
Eric Andersenf6be9441999-10-13 21:12:06 +0000606 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
607 break;
608 default:
Eric Andersenfa0540f1999-10-22 18:18:31 +0000609 if ( isdigit(c) && c >= '0' && c <= '7' &&
610 mode == 0 && groups == 0 ) {
611 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000612 return (TRUE);
613 }
614 else
615 return (FALSE);
616 }
617 break;
618 }
619
620 while ( (c = *s++) != '\0' ) {
621 switch ( c ) {
622 case ',':
623 break;
624 case 'r':
625 mode |= S_IRUSR|S_IRGRP|S_IROTH;
626 continue;
627 case 'w':
628 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
629 continue;
630 case 'x':
631 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
632 continue;
633 case 's':
634 mode |= S_IXGRP|S_ISUID|S_ISGID;
635 continue;
636 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000637 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000638 continue;
639 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000640 *theMode &= andMode;
641 *theMode |= orMode;
642 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000643 }
644 break;
645 }
646 switch ( type ) {
647 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000648 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000649 /* fall through */
650 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000651 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000652 break;
653 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000654 andMode &= ~(mode & groups);
655 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000656 break;
657 }
658 } while ( c == ',' );
Eric Andersence8f3b91999-10-20 07:03:36 +0000659 *theMode &= andMode;
660 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000661 return (TRUE);
662}
Eric Andersence8f3b91999-10-20 07:03:36 +0000663
664
Erik Andersenfac10d72000-02-07 05:29:42 +0000665#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000666
Eric Andersene674eb71999-10-19 20:52:57 +0000667
668
Eric Andersend23f9ba1999-10-20 19:18:15 +0000669
670
671
672
673#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
674
675/* Use this to avoid needing the glibc NSS stuff
676 * This uses storage buf to hold things.
677 * */
678uid_t
679my_getid(const char *filename, char *name, uid_t id)
680{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000681 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000682 char *rname, *start, *end, buf[128];
683 uid_t rid;
684
Eric Andersenaa0765e1999-10-22 04:30:20 +0000685 file=fopen(filename,"r");
686 if (file == NULL) {
687 perror(filename);
688 return (-1);
689 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000690
Eric Andersenaa0765e1999-10-22 04:30:20 +0000691 while (fgets (buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000692 if (buf[0] == '#')
693 continue;
694
695 start = buf;
696 end = strchr (start, ':');
697 if (end == NULL)
698 continue;
699 *end = '\0';
700 rname = start;
701
702 start = end + 1;
703 end = strchr (start, ':');
704 if (end == NULL)
705 continue;
706
707 start = end + 1;
708 rid = (uid_t) strtol (start, &end, 10);
709 if (end == start)
710 continue;
711
712 if (name) {
Eric Andersen48091fb1999-12-09 01:15:52 +0000713 if (0 == strcmp(rname, name)) {
714 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000715 return( rid);
Eric Andersen48091fb1999-12-09 01:15:52 +0000716 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000717 }
718 if ( id != -1 && id == rid ) {
719 strncpy(name, rname, 8);
Eric Andersen48091fb1999-12-09 01:15:52 +0000720 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000721 return( TRUE);
722 }
723 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000724 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000725 return (-1);
726}
727
728uid_t
729my_getpwnam(char *name)
730{
731 return my_getid("/etc/passwd", name, -1);
732}
733
734gid_t
735my_getgrnam(char *name)
736{
737 return my_getid("/etc/group", name, -1);
738}
739
740void
741my_getpwuid(char* name, uid_t uid)
742{
743 my_getid("/etc/passwd", name, uid);
744}
745
746void
747my_getgrgid(char* group, gid_t gid)
748{
749 my_getid("/etc/group", group, gid);
750}
751
752
Erik Andersenfac10d72000-02-07 05:29:42 +0000753#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000754
755
Eric Andersenaa0765e1999-10-22 04:30:20 +0000756
Eric Andersen0460ff21999-10-25 23:32:44 +0000757
758#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
759
760
761#include <linux/kd.h>
762#include <sys/ioctl.h>
763
764int is_a_console(int fd)
765{
766 char arg;
767
768 arg = 0;
769 return (ioctl(fd, KDGKBTYPE, &arg) == 0
770 && ((arg == KB_101) || (arg == KB_84)));
771}
772
773static int open_a_console(char *fnam)
774{
775 int fd;
776
777 /* try read-only */
778 fd = open(fnam, O_RDWR);
779
780 /* if failed, try read-only */
781 if (fd < 0 && errno == EACCES)
782 fd = open(fnam, O_RDONLY);
783
784 /* if failed, try write-only */
785 if (fd < 0 && errno == EACCES)
786 fd = open(fnam, O_WRONLY);
787
788 /* if failed, fail */
789 if (fd < 0)
790 return -1;
791
792 /* if not a console, fail */
793 if (! is_a_console(fd))
794 {
795 close(fd);
796 return -1;
797 }
798
799 /* success */
800 return fd;
801}
802
803/*
804 * Get an fd for use with kbd/console ioctls.
805 * We try several things because opening /dev/console will fail
806 * if someone else used X (which does a chown on /dev/console).
807 *
808 * if tty_name is non-NULL, try this one instead.
809 */
810
811int get_console_fd(char* tty_name)
812{
813 int fd;
814
815 if (tty_name)
816 {
817 if (-1 == (fd = open_a_console(tty_name)))
818 return -1;
819 else
820 return fd;
821 }
822
823 fd = open_a_console("/dev/tty");
824 if (fd >= 0)
825 return fd;
826
827 fd = open_a_console("/dev/tty0");
828 if (fd >= 0)
829 return fd;
830
831 fd = open_a_console("/dev/console");
832 if (fd >= 0)
833 return fd;
834
835 for (fd = 0; fd < 3; fd++)
836 if (is_a_console(fd))
837 return fd;
838
839 fprintf(stderr,
840 "Couldnt get a file descriptor referring to the console\n");
841 return -1; /* total failure */
842}
843
844
Erik Andersenfac10d72000-02-07 05:29:42 +0000845#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000846
847
Eric Andersenb186d981999-12-03 09:19:54 +0000848#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000849
850/* Do a case insensitive strstr() */
851char* stristr(char *haystack, const char *needle)
852{
853 int len = strlen( needle );
854 while( *haystack ) {
855 if( !strncasecmp( haystack, needle, len ) )
856 break;
857 haystack++;
858 }
859
860 if( !(*haystack) )
861 haystack = NULL;
862
863 return haystack;
864}
865
Eric Andersenc1525e81999-10-29 00:07:31 +0000866/* This tries to find a needle in a haystack, but does so by
867 * only trying to match literal strings (look 'ma, no regexps!)
868 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000869 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000870 * -Erik Andersen
871 */
872extern int find_match(char *haystack, char *needle, int ignoreCase)
873{
874
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000875 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000876 haystack = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000877 else
878 haystack = stristr (haystack, needle);
879 if (haystack == NULL)
880 return FALSE;
881 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000882}
883
884
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000885/* This performs substitutions after a string match has been found. */
Eric Andersenc1525e81999-10-29 00:07:31 +0000886extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
887{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000888 int foundOne=0;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000889 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000890
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000891 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000892 where = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000893 else
894 where = stristr (haystack, needle);
895
896 if (strcmp(needle, newNeedle)==0)
897 return FALSE;
898
899 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
900 while(where!=NULL) {
901 foundOne++;
902 strcpy(oldhayStack, haystack);
903#if 0
904 if ( strlen(newNeedle) > strlen(needle)) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000905 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
906 strlen(needle) + strlen(newNeedle)));
Eric Andersenc1525e81999-10-29 00:07:31 +0000907 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000908#endif
909 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
910 *slider=0;
911 haystack=strcat(haystack, newNeedle);
912 slider1+=strlen(needle);
913 haystack = strcat(haystack, slider1);
914 where = strstr (slider, needle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000915 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000916 free( oldhayStack);
917
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000918 if (foundOne > 0)
Eric Andersenc1525e81999-10-29 00:07:31 +0000919 return TRUE;
920 else
921 return FALSE;
922}
923
Erik Andersenfac10d72000-02-07 05:29:42 +0000924#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +0000925
926
Eric Andersenb186d981999-12-03 09:19:54 +0000927#if defined BB_FIND
928/*
929 * Routine to see if a text string is matched by a wildcard pattern.
930 * Returns TRUE if the text is matched, or FALSE if it is not matched
931 * or if the pattern is invalid.
932 * * matches zero or more characters
933 * ? matches a single character
934 * [abc] matches 'a', 'b' or 'c'
935 * \c quotes character c
936 * Adapted from code written by Ingo Wilken, and
937 * then taken from sash, Copyright (c) 1999 by David I. Bell
938 * Permission is granted to use, distribute, or modify this source,
939 * provided that this copyright notice remains intact.
940 * Permission to distribute this code under the GPL has been granted.
941 */
942extern int
943check_wildcard_match(const char* text, const char* pattern)
944{
945 const char* retryPat;
946 const char* retryText;
947 int ch;
948 int found;
949
950 retryPat = NULL;
951 retryText = NULL;
952
953 while (*text || *pattern)
954 {
955 ch = *pattern++;
956
957 switch (ch)
958 {
959 case '*':
960 retryPat = pattern;
961 retryText = text;
962 break;
963
964 case '[':
965 found = FALSE;
966
967 while ((ch = *pattern++) != ']')
968 {
969 if (ch == '\\')
970 ch = *pattern++;
971
972 if (ch == '\0')
973 return FALSE;
974
975 if (*text == ch)
976 found = TRUE;
977 }
978
979 //if (!found)
980 if (found==TRUE)
981 {
982 pattern = retryPat;
983 text = ++retryText;
984 }
985
986 /* fall into next case */
987
988 case '?':
989 if (*text++ == '\0')
990 return FALSE;
991
992 break;
993
994 case '\\':
995 ch = *pattern++;
996
997 if (ch == '\0')
998 return FALSE;
999
1000 /* fall into next case */
1001
1002 default:
1003 if (*text == ch)
1004 {
1005 if (*text)
1006 text++;
1007 break;
1008 }
1009
1010 if (*text)
1011 {
1012 pattern = retryPat;
1013 text = ++retryText;
1014 break;
1015 }
1016
1017 return FALSE;
1018 }
1019
1020 if (pattern == NULL)
1021 return FALSE;
1022 }
1023
1024 return TRUE;
1025}
Erik Andersenfac10d72000-02-07 05:29:42 +00001026#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001027
1028
Eric Andersen29d2e361999-11-06 06:07:27 +00001029
1030
Erik Andersen7dc16072000-01-04 01:10:25 +00001031#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001032/*
1033 * Given a block device, find the mount table entry if that block device
1034 * is mounted.
1035 *
1036 * Given any other file (or directory), find the mount table entry for its
1037 * filesystem.
1038 */
1039extern struct mntent *findMountPoint(const char *name, const char *table)
1040{
1041 struct stat s;
1042 dev_t mountDevice;
1043 FILE *mountTable;
1044 struct mntent *mountEntry;
1045
1046 if (stat(name, &s) != 0)
1047 return 0;
1048
1049 if ((s.st_mode & S_IFMT) == S_IFBLK)
1050 mountDevice = s.st_rdev;
1051 else
1052 mountDevice = s.st_dev;
1053
1054
1055 if ((mountTable = setmntent(table, "r")) == 0)
1056 return 0;
1057
1058 while ((mountEntry = getmntent(mountTable)) != 0) {
1059 if (strcmp(name, mountEntry->mnt_dir) == 0
1060 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1061 break;
1062 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1063 break;
1064 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1065 break;
1066 }
1067 endmntent(mountTable);
1068 return mountEntry;
1069}
Erik Andersenfac10d72000-02-07 05:29:42 +00001070#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001071
1072
1073
Eric Andersen1792f8c1999-12-09 06:11:36 +00001074#if defined BB_DD || defined BB_TAIL
1075/*
1076 * Read a number with a possible multiplier.
1077 * Returns -1 if the number format is illegal.
1078 */
1079extern long getNum (const char *cp)
1080{
1081 long value;
1082
1083 if (!isDecimal (*cp))
1084 return -1;
1085
1086 value = 0;
1087
1088 while (isDecimal (*cp))
1089 value = value * 10 + *cp++ - '0';
1090
1091 switch (*cp++) {
Erik Andersenfac10d72000-02-07 05:29:42 +00001092 case 'M':
1093 case 'm': /* `tail' uses it traditionally */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001094 value *= 1048576;
1095 break;
1096
1097 case 'k':
1098 value *= 1024;
1099 break;
1100
1101 case 'b':
1102 value *= 512;
1103 break;
1104
1105 case 'w':
1106 value *= 2;
1107 break;
1108
1109 case '\0':
1110 return value;
1111
1112 default:
1113 return -1;
1114 }
1115
1116 if (*cp)
1117 return -1;
1118
1119 return value;
1120}
Erik Andersenfac10d72000-02-07 05:29:42 +00001121#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001122
Eric Andersen6805d5d1999-12-09 22:39:55 +00001123
Erik Andersen31cf8e02000-02-07 20:31:19 +00001124#if defined BB_INIT || defined BB_SYSLOGD
1125/* try to open up the specified device */
1126extern int device_open(char *device, int mode)
1127{
1128 int m, f, fd = -1;
1129
1130 m = mode | O_NONBLOCK;
1131
1132 /* Retry up to 5 times */
1133 for (f = 0; f < 5; f++)
1134 if ((fd = open(device, m, 0600)) >= 0)
1135 break;
1136 if (fd < 0)
1137 return fd;
1138 /* Reset original flags. */
1139 if (m != mode)
1140 fcntl(fd, F_SETFL, mode);
1141 return fd;
1142}
1143#endif /* BB_INIT BB_SYSLOGD */
1144
1145
Eric Andersen6805d5d1999-12-09 22:39:55 +00001146#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
1147
1148#if ! defined BB_FEATURE_USE_PROCFS
1149#error Sorry, I depend on the /proc filesystem right now.
1150#endif
John Beppuf95ca971999-12-09 22:10:18 +00001151/* findInitPid()
1152 *
1153 * This finds the pid of init (which is not always 1).
1154 * Currently, it's implemented by rummaging through the proc filesystem.
1155 *
1156 * [return]
1157 * 0 failure
1158 * pid when init's pid is found.
1159 */
1160extern pid_t
1161findInitPid()
1162{
1163 pid_t init_pid;
1164 char filename[256];
1165 char buffer[256];
1166
1167 /* no need to opendir ;) */
1168 for (init_pid = 1; init_pid < 65536; init_pid++) {
1169 FILE *status;
1170
1171 sprintf(filename, "/proc/%d/status", init_pid);
1172 status = fopen(filename, "r");
1173 if (!status) { continue; }
1174 fgets(buffer, 256, status);
1175 fclose(status);
1176
Eric Andersen6805d5d1999-12-09 22:39:55 +00001177 if ( (strstr(buffer, "init\n") != NULL )) {
John Beppuf95ca971999-12-09 22:10:18 +00001178 return init_pid;
1179 }
1180 }
1181 return 0;
1182}
Erik Andersenfac10d72000-02-07 05:29:42 +00001183#endif /* BB_INIT || BB_HALT || BB_REBOOT */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001184
Erik Andersenfac10d72000-02-07 05:29:42 +00001185#if defined BB_GUNZIP \
1186 || defined BB_GZIP \
1187 || defined BB_PRINTF \
1188 || defined BB_TAIL
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001189extern void *xmalloc (size_t size)
1190{
1191 void *cp = malloc (size);
1192
1193 if (cp == NULL) {
1194 error("out of memory");
1195 }
1196 return cp;
1197}
1198
1199extern void error(char *msg)
1200{
1201 fprintf(stderr, "\n%s\n", msg);
1202 exit(1);
1203}
Erik Andersenfac10d72000-02-07 05:29:42 +00001204#endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001205
Erik Andersen7dc16072000-01-04 01:10:25 +00001206#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1207extern int vdprintf(int d, const char *format, va_list ap)
1208{
1209 char buf[BUF_SIZE];
1210 int len;
1211
1212 len = vsprintf(buf, format, ap);
1213 return write(d, buf, len);
1214}
Erik Andersenfac10d72000-02-07 05:29:42 +00001215#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001216
Erik Andersen5cbdd712000-01-26 20:06:48 +00001217#if defined BB_FEATURE_MOUNT_LOOP
1218extern int del_loop(const char *device)
1219{
1220 int fd;
1221
1222 if ((fd = open(device, O_RDONLY)) < 0) {
1223 perror(device);
1224 return( FALSE);
1225 }
1226 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1227 perror("ioctl: LOOP_CLR_FD");
1228 return( FALSE);
1229 }
1230 close(fd);
1231 return( TRUE);
1232}
Erik Andersende7965c2000-01-26 23:49:21 +00001233
1234extern int set_loop(const char *device, const char *file, int offset, int *loopro)
1235{
1236 struct loop_info loopinfo;
1237 int fd, ffd, mode;
1238
1239 mode = *loopro ? O_RDONLY : O_RDWR;
1240 if ((ffd = open (file, mode)) < 0 && !*loopro
1241 && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
1242 perror (file);
1243 return 1;
1244 }
1245 if ((fd = open (device, mode)) < 0) {
1246 close(ffd);
1247 perror (device);
1248 return 1;
1249 }
1250 *loopro = (mode == O_RDONLY);
1251
1252 memset(&loopinfo, 0, sizeof(loopinfo));
1253 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1254 loopinfo.lo_name[LO_NAME_SIZE-1] = 0;
1255
1256 loopinfo.lo_offset = offset;
1257
1258 loopinfo.lo_encrypt_key_size = 0;
1259 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1260 perror("ioctl: LOOP_SET_FD");
1261 close(fd);
1262 close(ffd);
1263 return 1;
1264 }
1265 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1266 (void) ioctl(fd, LOOP_CLR_FD, 0);
1267 perror("ioctl: LOOP_SET_STATUS");
1268 close(fd);
1269 close(ffd);
1270 return 1;
1271 }
1272 close(fd);
1273 close(ffd);
1274 return 0;
1275}
1276
1277extern char *find_unused_loop_device (void)
1278{
1279 char dev[20];
1280 int i, fd;
1281 struct stat statbuf;
1282 struct loop_info loopinfo;
1283
1284 for(i = 0; i <= 7; i++) {
1285 sprintf(dev, "/dev/loop%d", i);
1286 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1287 if ((fd = open (dev, O_RDONLY)) >= 0) {
1288 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1289 if (errno == ENXIO) { /* probably free */
1290 close (fd);
1291 return strdup(dev);
1292 }
1293 }
1294 close (fd);
1295 }
1296 }
1297 }
1298 return NULL;
1299}
1300#endif /* BB_FEATURE_MOUNT_LOOP */
1301
Erik Andersen5cbdd712000-01-26 20:06:48 +00001302
Eric Andersencc8ed391999-10-05 16:24:54 +00001303/* END CODE */