blob: ade47bde0906d30edff5880cb9ef8e52d207b1c1 [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"
28#include <stdio.h>
29#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000032#include <dirent.h>
33#include <time.h>
34#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000035#include <sys/stat.h>
36#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000037#include <ctype.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Eric Andersend0246fb1999-11-04 21:18:07 +000039#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000040# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000041const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000042# else
43# if defined BB_MTAB
44const char mtab_file[] = "/etc/mtab";
45# else
46# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
47# endif
48# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000049#endif
50
51
Eric Andersend73dc5b1999-11-10 23:13:02 +000052extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000053{
Eric Andersena07f0b01999-10-22 19:49:09 +000054 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
Eric Andersenb0e9a701999-10-18 22:28:26 +000055 fprintf(stderr, "Usage: %s\n", usage);
56 exit(FALSE);
57}
58
59
Eric Andersend23f9ba1999-10-20 19:18:15 +000060#if defined (BB_INIT) || defined (BB_PS)
61
Eric Andersen0ecb54a1999-12-05 23:24:55 +000062#if ! defined BB_FEATURE_USE_PROCFS
63#error Sorry, I depend on the /proc filesystem right now.
64#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000065/* Returns kernel version encoded as major*65536 + minor*256 + patch,
66 * so, for example, to check if the kernel is greater than 2.2.11:
67 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
68 */
69int
70get_kernel_revision()
71{
Eric Andersenaa0765e1999-10-22 04:30:20 +000072 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +000073 int major=0, minor=0, patch=0;
Eric Andersenaa0765e1999-10-22 04:30:20 +000074 char* filename="/proc/sys/kernel/osrelease";
Eric Andersend23f9ba1999-10-20 19:18:15 +000075
Eric Andersenaa0765e1999-10-22 04:30:20 +000076 file = fopen(filename,"r");
77 if (file == NULL) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +000078 /* bummer, /proc must not be mounted... */
Eric Andersenaa0765e1999-10-22 04:30:20 +000079 return( 0);
80 }
81 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
82 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +000083 return major*65536 + minor*256 + patch;
84}
85
86#endif
87
88
Eric Andersenc6cb79d1999-10-13 18:01:10 +000089
90#if defined (BB_CP) || defined (BB_MV)
91/*
92 * Return TRUE if a fileName is a directory.
93 * Nonexistant files return FALSE.
94 */
95int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +000096{
Eric Andersenc6cb79d1999-10-13 18:01:10 +000097 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +000098
Eric Andersenc6cb79d1999-10-13 18:01:10 +000099 if (stat(name, &statBuf) < 0)
100 return FALSE;
Eric Andersen9b587181999-10-17 05:43:39 +0000101 if (S_ISDIR(statBuf.st_mode))
102 return TRUE;
103 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000104}
105
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000106
107/*
108 * Copy one file to another, while possibly preserving its modes, times,
109 * and modes. Returns TRUE if successful, or FALSE on a failure with an
110 * error message output. (Failure is not indicted if the attributes cannot
111 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000112 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000113 */
114int
Eric Andersen3c163821999-10-14 22:16:57 +0000115copyFile( const char *srcName, const char *destName,
116 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000117{
118 int rfd;
119 int wfd;
120 int rcc;
121 int result;
122 char buf[BUF_SIZE];
123 struct stat srcStatBuf;
124 struct stat dstStatBuf;
125 struct utimbuf times;
126
127 if (followLinks == FALSE)
128 result = stat(srcName, &srcStatBuf);
129 else
130 result = lstat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000131 if (result < 0) {
132 perror(srcName);
133 return FALSE;
134 }
135
136 if (followLinks == FALSE)
137 result = stat(destName, &dstStatBuf);
138 else
139 result = lstat(destName, &dstStatBuf);
140 if (result < 0) {
141 dstStatBuf.st_ino = -1;
142 dstStatBuf.st_dev = -1;
143 }
144
145 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
146 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
147 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
148 return FALSE;
149 }
150
151 if (S_ISDIR(srcStatBuf.st_mode)) {
152 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
153 /* Make sure the directory is writable */
Erik Andersen2fe08c71999-12-29 02:10:35 +0000154 result = mkdir(destName, 0777777 ^ umask(0));
155 if (result < 0 && errno != EEXIST) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000156 perror(destName);
157 return (FALSE);
158 }
159 } else if (S_ISLNK(srcStatBuf.st_mode)) {
160 char *link_val;
161 int link_size;
162
163 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
164 link_val = (char *) alloca(PATH_MAX + 2);
165 link_size = readlink(srcName, link_val, PATH_MAX + 1);
166 if (link_size < 0) {
167 perror(srcName);
168 return (FALSE);
169 }
170 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000171 link_size = symlink(link_val, destName);
172 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000173 perror(destName);
174 return (FALSE);
175 }
176 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
177 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
178 if (mkfifo(destName, 644)) {
179 perror(destName);
180 return (FALSE);
181 }
182 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
183 || S_ISSOCK (srcStatBuf.st_mode)) {
184 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
185 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
186 perror(destName);
187 return (FALSE);
188 }
189 } else if (S_ISREG(srcStatBuf.st_mode)) {
190 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
191 rfd = open(srcName, O_RDONLY);
192 if (rfd < 0) {
193 perror(srcName);
194 return FALSE;
195 }
196
197 wfd = creat(destName, srcStatBuf.st_mode);
198 if (wfd < 0) {
199 perror(destName);
200 close(rfd);
201 return FALSE;
202 }
203
204 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
205 if (fullWrite(wfd, buf, rcc) < 0)
206 goto error_exit;
207 }
208 if (rcc < 0) {
209 goto error_exit;
210 }
211
212 close(rfd);
213 if (close(wfd) < 0) {
214 return FALSE;
215 }
216 }
217
218 if (setModes == TRUE) {
219 //fprintf(stderr, "Setting permissions for %s\n", destName);
220 chmod(destName, srcStatBuf.st_mode);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000221#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
222 if (followLinks == FALSE)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000223 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000224 else
225#endif
226 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000227
228 times.actime = srcStatBuf.st_atime;
229 times.modtime = srcStatBuf.st_mtime;
230
231 utime(destName, &times);
232 }
233
234 return TRUE;
235
236
237 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000238 perror(destName);
239 close(rfd);
240 close(wfd);
241
242 return FALSE;
243}
Eric Andersencc8ed391999-10-05 16:24:54 +0000244#endif
245
246
247
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000248#if defined BB_TAR || defined BB_LS
249
250#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
251#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
252
253/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
254static const mode_t SBIT[] = {
255 0, 0, S_ISUID,
256 0, 0, S_ISGID,
257 0, 0, S_ISVTX
258};
259
260/* The 9 mode bits to test */
261static const mode_t MBIT[] = {
262 S_IRUSR, S_IWUSR, S_IXUSR,
263 S_IRGRP, S_IWGRP, S_IXGRP,
264 S_IROTH, S_IWOTH, S_IXOTH
265};
266
267#define MODE1 "rwxrwxrwx"
268#define MODE0 "---------"
269#define SMODE1 "..s..s..t"
270#define SMODE0 "..S..S..T"
271
Eric Andersencc8ed391999-10-05 16:24:54 +0000272/*
273 * Return the standard ls-like mode string from a file mode.
274 * This is static and so is overwritten on each call.
275 */
Eric Andersenf811e071999-10-09 00:25:00 +0000276const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000277{
Eric Andersenf811e071999-10-09 00:25:00 +0000278 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000279
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000280 int i;
281 buf[0] = TYPECHAR(mode);
282 for (i=0; i<9; i++) {
283 if (mode & SBIT[i])
284 buf[i+1] = (mode & MBIT[i])?
285 SMODE1[i] : SMODE0[i];
286 else
287 buf[i+1] = (mode & MBIT[i])?
288 MODE1[i] : MODE0[i];
289 }
Eric Andersenf811e071999-10-09 00:25:00 +0000290 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000291}
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000292#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000293
294
Eric Andersen1792f8c1999-12-09 06:11:36 +0000295#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000296/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000297 * Return the standard ls-like time string from a time_t
298 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000299 */
Eric Andersenf811e071999-10-09 00:25:00 +0000300const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000301{
Eric Andersenf811e071999-10-09 00:25:00 +0000302 time_t now;
303 char *str;
304 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000305
Eric Andersenf811e071999-10-09 00:25:00 +0000306 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000307
Eric Andersenf811e071999-10-09 00:25:00 +0000308 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000309
Eric Andersenf811e071999-10-09 00:25:00 +0000310 strcpy(buf, &str[4]);
311 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000312
Eric Andersenf811e071999-10-09 00:25:00 +0000313 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
314 strcpy(&buf[7], &str[20]);
315 buf[11] = '\0';
316 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000317
Eric Andersenf811e071999-10-09 00:25:00 +0000318 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000319}
Erik Andersen31638212000-01-15 22:28:50 +0000320#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000321
Erik Andersen31638212000-01-15 22:28:50 +0000322#if defined BB_TAR || defined BB_CP || defined BB_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000323/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000324 * Write all of the supplied buffer out to a file.
325 * This does multiple writes as necessary.
326 * Returns the amount written, or -1 on an error.
327 */
Eric Andersenf811e071999-10-09 00:25:00 +0000328int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000329{
Eric Andersenf811e071999-10-09 00:25:00 +0000330 int cc;
331 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000332
Eric Andersenf811e071999-10-09 00:25:00 +0000333 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000334
Eric Andersenf811e071999-10-09 00:25:00 +0000335 while (len > 0) {
336 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000337
Eric Andersenf811e071999-10-09 00:25:00 +0000338 if (cc < 0)
339 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000340
Eric Andersenf811e071999-10-09 00:25:00 +0000341 buf += cc;
342 total += cc;
343 len -= cc;
344 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000345
Eric Andersenf811e071999-10-09 00:25:00 +0000346 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000347}
Eric Andersen1792f8c1999-12-09 06:11:36 +0000348#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000349
350
Eric Andersen1792f8c1999-12-09 06:11:36 +0000351#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000352/*
353 * Read all of the supplied buffer from a file.
354 * This does multiple reads as necessary.
355 * Returns the amount read, or -1 on an error.
356 * A short read is returned on an end of file.
357 */
Eric Andersenf811e071999-10-09 00:25:00 +0000358int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000359{
Eric Andersenf811e071999-10-09 00:25:00 +0000360 int cc;
361 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000362
Eric Andersenf811e071999-10-09 00:25:00 +0000363 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000364
Eric Andersenf811e071999-10-09 00:25:00 +0000365 while (len > 0) {
366 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000367
Eric Andersenf811e071999-10-09 00:25:00 +0000368 if (cc < 0)
369 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000370
Eric Andersenf811e071999-10-09 00:25:00 +0000371 if (cc == 0)
372 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000373
Eric Andersenf811e071999-10-09 00:25:00 +0000374 buf += cc;
375 total += cc;
376 len -= cc;
377 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000378
Eric Andersenf811e071999-10-09 00:25:00 +0000379 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000380}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000381#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000382
383
Erik Andersen7dc16072000-01-04 01:10:25 +0000384#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS) || defined (BB_INSMOD)
Eric Andersencc8ed391999-10-05 16:24:54 +0000385/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000386 * Walk down all the directories under the specified
387 * location, and do something (something specified
388 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000389 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000390 * Unfortunatly, while nftw(3) could replace this and reduce
391 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000392 * and so isn't sufficiently portable to take over since glibc2.1
393 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000394 */
395int
Eric Andersen63a0e531999-10-22 05:12:14 +0000396recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersen9b587181999-10-17 05:43:39 +0000397 int (*fileAction) (const char *fileName, struct stat* statbuf),
398 int (*dirAction) (const char *fileName, struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000399{
Eric Andersenf811e071999-10-09 00:25:00 +0000400 int status;
Erik Andersen05df2392000-01-13 04:43:48 +0000401 struct stat statbuf, statbuf1;
Eric Andersenf811e071999-10-09 00:25:00 +0000402 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000403
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000404 if (followLinks == TRUE)
Eric Andersenf811e071999-10-09 00:25:00 +0000405 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000406 else
407 status = lstat(fileName, &statbuf);
408
Erik Andersen05df2392000-01-13 04:43:48 +0000409 status = lstat(fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000410 if (status < 0) {
411 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000412 return (FALSE);
413 }
414
Eric Andersen50d63601999-11-09 01:47:36 +0000415 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
416 if (fileAction == NULL)
417 return (TRUE);
418 else
419 return (fileAction(fileName, &statbuf));
420 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000421
Eric Andersenf811e071999-10-09 00:25:00 +0000422 if (recurse == FALSE) {
423 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000424 if (dirAction != NULL)
Eric Andersen9b587181999-10-17 05:43:39 +0000425 return (dirAction(fileName, &statbuf));
Eric Andersenf811e071999-10-09 00:25:00 +0000426 else
Eric Andersen3c163821999-10-14 22:16:57 +0000427 return (TRUE);
428 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000429 }
Erik Andersen05df2392000-01-13 04:43:48 +0000430
431 status = lstat(fileName, &statbuf1);
432 if (status < 0) {
433 perror(fileName);
434 return (FALSE);
435 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000436
Erik Andersen05df2392000-01-13 04:43:48 +0000437 if (S_ISDIR(statbuf.st_mode) && S_ISDIR(statbuf1.st_mode)) {
Eric Andersen2b69c401999-10-05 22:58:32 +0000438 DIR *dir;
439 dir = opendir(fileName);
440 if (!dir) {
441 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000442 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000443 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000444 if (dirAction != NULL && depthFirst == FALSE) {
Eric Andersen9b587181999-10-17 05:43:39 +0000445 status = dirAction(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000446 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000447 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000448 return (FALSE);
449 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000450 }
Eric Andersenf811e071999-10-09 00:25:00 +0000451 while ((next = readdir(dir)) != NULL) {
452 char nextFile[NAME_MAX];
453 if ((strcmp(next->d_name, "..") == 0)
454 || (strcmp(next->d_name, ".") == 0)) {
455 continue;
456 }
457 sprintf(nextFile, "%s/%s", fileName, next->d_name);
458 status =
Eric Andersen63a0e531999-10-22 05:12:14 +0000459 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Eric Andersenbed30e91999-10-18 19:02:32 +0000460 fileAction, dirAction);
Eric Andersenf811e071999-10-09 00:25:00 +0000461 if (status < 0) {
462 closedir(dir);
463 return (FALSE);
464 }
465 }
466 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000467 if (status < 0) {
468 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000469 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000470 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000471 if (dirAction != NULL && depthFirst == TRUE) {
Eric Andersenbed30e91999-10-18 19:02:32 +0000472 status = dirAction(fileName, &statbuf);
473 if (status == FALSE) {
474 perror(fileName);
475 return (FALSE);
476 }
477 }
Eric Andersenf811e071999-10-09 00:25:00 +0000478 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000479 if (fileAction == NULL)
480 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000481 else
Eric Andersen9b587181999-10-17 05:43:39 +0000482 return (fileAction(fileName, &statbuf));
Eric Andersencc8ed391999-10-05 16:24:54 +0000483 }
Eric Andersenf811e071999-10-09 00:25:00 +0000484 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000485}
486
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000487#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000488
Eric Andersenf6be9441999-10-13 21:12:06 +0000489
490
Erik Andersena8991081999-12-29 03:34:00 +0000491#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000492/*
493 * Attempt to create the directories along the specified path, except for
494 * the final component. The mode is given for the final directory only,
495 * while all previous ones get default protections. Errors are not reported
496 * here, as failures to restore files can be reported later.
497 */
498extern void createPath (const char *name, int mode)
499{
500 char *cp;
501 char *cpOld;
502 char buf[NAME_MAX];
503
Eric Andersencb41c2e1999-11-22 07:41:00 +0000504 strcpy( buf, name);
Eric Andersenf6be9441999-10-13 21:12:06 +0000505 cp = strchr (buf, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000506 while (cp) {
507 cpOld = cp;
508 cp = strchr (cp + 1, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000509 *cpOld = '\0';
Eric Andersenb6a44b81999-11-13 04:47:09 +0000510 mkdir (buf, cp ? 0777 : mode);
Eric Andersenf6be9441999-10-13 21:12:06 +0000511 *cpOld = '/';
512 }
513}
514#endif
515
516
517
518#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000519/* [ugoa]{+|-|=}[rwxst] */
520
521
522
523extern int
524parse_mode( const char* s, mode_t* theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000525{
Eric Andersence8f3b91999-10-20 07:03:36 +0000526 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
527 mode_t orMode = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000528 mode_t mode = 0;
Eric Andersence8f3b91999-10-20 07:03:36 +0000529 mode_t groups = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000530 char type;
531 char c;
532
533 do {
534 for ( ; ; ) {
535 switch ( c = *s++ ) {
536 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000537 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000538 case 'u':
539 groups |= S_ISUID|S_IRWXU;
540 continue;
541 case 'g':
542 groups |= S_ISGID|S_IRWXG;
543 continue;
544 case 'o':
545 groups |= S_IRWXO;
546 continue;
547 case 'a':
548 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
549 continue;
550 case '+':
551 case '=':
552 case '-':
553 type = c;
Eric Andersence8f3b91999-10-20 07:03:36 +0000554 if ( groups == 0 ) /* The default is "all" */
Eric Andersenf6be9441999-10-13 21:12:06 +0000555 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
556 break;
557 default:
Eric Andersenfa0540f1999-10-22 18:18:31 +0000558 if ( isdigit(c) && c >= '0' && c <= '7' &&
559 mode == 0 && groups == 0 ) {
560 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000561 return (TRUE);
562 }
563 else
564 return (FALSE);
565 }
566 break;
567 }
568
569 while ( (c = *s++) != '\0' ) {
570 switch ( c ) {
571 case ',':
572 break;
573 case 'r':
574 mode |= S_IRUSR|S_IRGRP|S_IROTH;
575 continue;
576 case 'w':
577 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
578 continue;
579 case 'x':
580 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
581 continue;
582 case 's':
583 mode |= S_IXGRP|S_ISUID|S_ISGID;
584 continue;
585 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000586 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000587 continue;
588 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000589 *theMode &= andMode;
590 *theMode |= orMode;
591 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000592 }
593 break;
594 }
595 switch ( type ) {
596 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000597 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000598 /* fall through */
599 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000600 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000601 break;
602 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000603 andMode &= ~(mode & groups);
604 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000605 break;
606 }
607 } while ( c == ',' );
Eric Andersence8f3b91999-10-20 07:03:36 +0000608 *theMode &= andMode;
609 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000610 return (TRUE);
611}
Eric Andersence8f3b91999-10-20 07:03:36 +0000612
613
Eric Andersenf6be9441999-10-13 21:12:06 +0000614#endif
615
Eric Andersene674eb71999-10-19 20:52:57 +0000616
617
Eric Andersend23f9ba1999-10-20 19:18:15 +0000618
619
620
621
622#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
623
624/* Use this to avoid needing the glibc NSS stuff
625 * This uses storage buf to hold things.
626 * */
627uid_t
628my_getid(const char *filename, char *name, uid_t id)
629{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000630 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000631 char *rname, *start, *end, buf[128];
632 uid_t rid;
633
Eric Andersenaa0765e1999-10-22 04:30:20 +0000634 file=fopen(filename,"r");
635 if (file == NULL) {
636 perror(filename);
637 return (-1);
638 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000639
Eric Andersenaa0765e1999-10-22 04:30:20 +0000640 while (fgets (buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000641 if (buf[0] == '#')
642 continue;
643
644 start = buf;
645 end = strchr (start, ':');
646 if (end == NULL)
647 continue;
648 *end = '\0';
649 rname = start;
650
651 start = end + 1;
652 end = strchr (start, ':');
653 if (end == NULL)
654 continue;
655
656 start = end + 1;
657 rid = (uid_t) strtol (start, &end, 10);
658 if (end == start)
659 continue;
660
661 if (name) {
Eric Andersen48091fb1999-12-09 01:15:52 +0000662 if (0 == strcmp(rname, name)) {
663 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000664 return( rid);
Eric Andersen48091fb1999-12-09 01:15:52 +0000665 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000666 }
667 if ( id != -1 && id == rid ) {
668 strncpy(name, rname, 8);
Eric Andersen48091fb1999-12-09 01:15:52 +0000669 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000670 return( TRUE);
671 }
672 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000673 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000674 return (-1);
675}
676
677uid_t
678my_getpwnam(char *name)
679{
680 return my_getid("/etc/passwd", name, -1);
681}
682
683gid_t
684my_getgrnam(char *name)
685{
686 return my_getid("/etc/group", name, -1);
687}
688
689void
690my_getpwuid(char* name, uid_t uid)
691{
692 my_getid("/etc/passwd", name, uid);
693}
694
695void
696my_getgrgid(char* group, gid_t gid)
697{
698 my_getid("/etc/group", group, gid);
699}
700
701
702#endif
703
704
Eric Andersenaa0765e1999-10-22 04:30:20 +0000705
Eric Andersen0460ff21999-10-25 23:32:44 +0000706
707#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
708
709
710#include <linux/kd.h>
711#include <sys/ioctl.h>
712
713int is_a_console(int fd)
714{
715 char arg;
716
717 arg = 0;
718 return (ioctl(fd, KDGKBTYPE, &arg) == 0
719 && ((arg == KB_101) || (arg == KB_84)));
720}
721
722static int open_a_console(char *fnam)
723{
724 int fd;
725
726 /* try read-only */
727 fd = open(fnam, O_RDWR);
728
729 /* if failed, try read-only */
730 if (fd < 0 && errno == EACCES)
731 fd = open(fnam, O_RDONLY);
732
733 /* if failed, try write-only */
734 if (fd < 0 && errno == EACCES)
735 fd = open(fnam, O_WRONLY);
736
737 /* if failed, fail */
738 if (fd < 0)
739 return -1;
740
741 /* if not a console, fail */
742 if (! is_a_console(fd))
743 {
744 close(fd);
745 return -1;
746 }
747
748 /* success */
749 return fd;
750}
751
752/*
753 * Get an fd for use with kbd/console ioctls.
754 * We try several things because opening /dev/console will fail
755 * if someone else used X (which does a chown on /dev/console).
756 *
757 * if tty_name is non-NULL, try this one instead.
758 */
759
760int get_console_fd(char* tty_name)
761{
762 int fd;
763
764 if (tty_name)
765 {
766 if (-1 == (fd = open_a_console(tty_name)))
767 return -1;
768 else
769 return fd;
770 }
771
772 fd = open_a_console("/dev/tty");
773 if (fd >= 0)
774 return fd;
775
776 fd = open_a_console("/dev/tty0");
777 if (fd >= 0)
778 return fd;
779
780 fd = open_a_console("/dev/console");
781 if (fd >= 0)
782 return fd;
783
784 for (fd = 0; fd < 3; fd++)
785 if (is_a_console(fd))
786 return fd;
787
788 fprintf(stderr,
789 "Couldnt get a file descriptor referring to the console\n");
790 return -1; /* total failure */
791}
792
793
794#endif
795
796
Eric Andersenb186d981999-12-03 09:19:54 +0000797#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000798
799/* Do a case insensitive strstr() */
800char* stristr(char *haystack, const char *needle)
801{
802 int len = strlen( needle );
803 while( *haystack ) {
804 if( !strncasecmp( haystack, needle, len ) )
805 break;
806 haystack++;
807 }
808
809 if( !(*haystack) )
810 haystack = NULL;
811
812 return haystack;
813}
814
Eric Andersenc1525e81999-10-29 00:07:31 +0000815/* This tries to find a needle in a haystack, but does so by
816 * only trying to match literal strings (look 'ma, no regexps!)
817 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000818 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000819 * -Erik Andersen
820 */
821extern int find_match(char *haystack, char *needle, int ignoreCase)
822{
823
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000824 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000825 haystack = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000826 else
827 haystack = stristr (haystack, needle);
828 if (haystack == NULL)
829 return FALSE;
830 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000831}
832
833
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000834/* This performs substitutions after a string match has been found. */
Eric Andersenc1525e81999-10-29 00:07:31 +0000835extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
836{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000837 int foundOne=0;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000838 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000839
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000840 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000841 where = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000842 else
843 where = stristr (haystack, needle);
844
845 if (strcmp(needle, newNeedle)==0)
846 return FALSE;
847
848 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
849 while(where!=NULL) {
850 foundOne++;
851 strcpy(oldhayStack, haystack);
852#if 0
853 if ( strlen(newNeedle) > strlen(needle)) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000854 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
855 strlen(needle) + strlen(newNeedle)));
Eric Andersenc1525e81999-10-29 00:07:31 +0000856 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000857#endif
858 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
859 *slider=0;
860 haystack=strcat(haystack, newNeedle);
861 slider1+=strlen(needle);
862 haystack = strcat(haystack, slider1);
863 where = strstr (slider, needle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000864 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000865 free( oldhayStack);
866
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000867 if (foundOne > 0)
Eric Andersenc1525e81999-10-29 00:07:31 +0000868 return TRUE;
869 else
870 return FALSE;
871}
872
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000873
Eric Andersenc1525e81999-10-29 00:07:31 +0000874#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000875
876
Eric Andersenb186d981999-12-03 09:19:54 +0000877#if defined BB_FIND
878/*
879 * Routine to see if a text string is matched by a wildcard pattern.
880 * Returns TRUE if the text is matched, or FALSE if it is not matched
881 * or if the pattern is invalid.
882 * * matches zero or more characters
883 * ? matches a single character
884 * [abc] matches 'a', 'b' or 'c'
885 * \c quotes character c
886 * Adapted from code written by Ingo Wilken, and
887 * then taken from sash, Copyright (c) 1999 by David I. Bell
888 * Permission is granted to use, distribute, or modify this source,
889 * provided that this copyright notice remains intact.
890 * Permission to distribute this code under the GPL has been granted.
891 */
892extern int
893check_wildcard_match(const char* text, const char* pattern)
894{
895 const char* retryPat;
896 const char* retryText;
897 int ch;
898 int found;
899
900 retryPat = NULL;
901 retryText = NULL;
902
903 while (*text || *pattern)
904 {
905 ch = *pattern++;
906
907 switch (ch)
908 {
909 case '*':
910 retryPat = pattern;
911 retryText = text;
912 break;
913
914 case '[':
915 found = FALSE;
916
917 while ((ch = *pattern++) != ']')
918 {
919 if (ch == '\\')
920 ch = *pattern++;
921
922 if (ch == '\0')
923 return FALSE;
924
925 if (*text == ch)
926 found = TRUE;
927 }
928
929 //if (!found)
930 if (found==TRUE)
931 {
932 pattern = retryPat;
933 text = ++retryText;
934 }
935
936 /* fall into next case */
937
938 case '?':
939 if (*text++ == '\0')
940 return FALSE;
941
942 break;
943
944 case '\\':
945 ch = *pattern++;
946
947 if (ch == '\0')
948 return FALSE;
949
950 /* fall into next case */
951
952 default:
953 if (*text == ch)
954 {
955 if (*text)
956 text++;
957 break;
958 }
959
960 if (*text)
961 {
962 pattern = retryPat;
963 text = ++retryText;
964 break;
965 }
966
967 return FALSE;
968 }
969
970 if (pattern == NULL)
971 return FALSE;
972 }
973
974 return TRUE;
975}
976#endif
977
978
Eric Andersen29d2e361999-11-06 06:07:27 +0000979
980
Erik Andersen7dc16072000-01-04 01:10:25 +0000981#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +0000982/*
983 * Given a block device, find the mount table entry if that block device
984 * is mounted.
985 *
986 * Given any other file (or directory), find the mount table entry for its
987 * filesystem.
988 */
989extern struct mntent *findMountPoint(const char *name, const char *table)
990{
991 struct stat s;
992 dev_t mountDevice;
993 FILE *mountTable;
994 struct mntent *mountEntry;
995
996 if (stat(name, &s) != 0)
997 return 0;
998
999 if ((s.st_mode & S_IFMT) == S_IFBLK)
1000 mountDevice = s.st_rdev;
1001 else
1002 mountDevice = s.st_dev;
1003
1004
1005 if ((mountTable = setmntent(table, "r")) == 0)
1006 return 0;
1007
1008 while ((mountEntry = getmntent(mountTable)) != 0) {
1009 if (strcmp(name, mountEntry->mnt_dir) == 0
1010 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1011 break;
1012 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1013 break;
1014 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1015 break;
1016 }
1017 endmntent(mountTable);
1018 return mountEntry;
1019}
Eric Andersen29d2e361999-11-06 06:07:27 +00001020#endif
1021
1022
1023
Eric Andersen1792f8c1999-12-09 06:11:36 +00001024#if defined BB_DD || defined BB_TAIL
1025/*
1026 * Read a number with a possible multiplier.
1027 * Returns -1 if the number format is illegal.
1028 */
1029extern long getNum (const char *cp)
1030{
1031 long value;
1032
1033 if (!isDecimal (*cp))
1034 return -1;
1035
1036 value = 0;
1037
1038 while (isDecimal (*cp))
1039 value = value * 10 + *cp++ - '0';
1040
1041 switch (*cp++) {
1042 case 'm':
1043 value *= 1048576;
1044 break;
1045
1046 case 'k':
1047 value *= 1024;
1048 break;
1049
1050 case 'b':
1051 value *= 512;
1052 break;
1053
1054 case 'w':
1055 value *= 2;
1056 break;
1057
1058 case '\0':
1059 return value;
1060
1061 default:
1062 return -1;
1063 }
1064
1065 if (*cp)
1066 return -1;
1067
1068 return value;
1069}
1070#endif
1071
Eric Andersen6805d5d1999-12-09 22:39:55 +00001072
1073#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
1074
1075#if ! defined BB_FEATURE_USE_PROCFS
1076#error Sorry, I depend on the /proc filesystem right now.
1077#endif
John Beppuf95ca971999-12-09 22:10:18 +00001078/* findInitPid()
1079 *
1080 * This finds the pid of init (which is not always 1).
1081 * Currently, it's implemented by rummaging through the proc filesystem.
1082 *
1083 * [return]
1084 * 0 failure
1085 * pid when init's pid is found.
1086 */
1087extern pid_t
1088findInitPid()
1089{
1090 pid_t init_pid;
1091 char filename[256];
1092 char buffer[256];
1093
1094 /* no need to opendir ;) */
1095 for (init_pid = 1; init_pid < 65536; init_pid++) {
1096 FILE *status;
1097
1098 sprintf(filename, "/proc/%d/status", init_pid);
1099 status = fopen(filename, "r");
1100 if (!status) { continue; }
1101 fgets(buffer, 256, status);
1102 fclose(status);
1103
Eric Andersen6805d5d1999-12-09 22:39:55 +00001104 if ( (strstr(buffer, "init\n") != NULL )) {
John Beppuf95ca971999-12-09 22:10:18 +00001105 return init_pid;
1106 }
1107 }
1108 return 0;
1109}
1110#endif
Eric Andersen1792f8c1999-12-09 06:11:36 +00001111
Erik Andersen7dc16072000-01-04 01:10:25 +00001112#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1113extern int vdprintf(int d, const char *format, va_list ap)
1114{
1115 char buf[BUF_SIZE];
1116 int len;
1117
1118 len = vsprintf(buf, format, ap);
1119 return write(d, buf, len);
1120}
1121#endif
1122
Eric Andersencc8ed391999-10-05 16:24:54 +00001123/* END CODE */