blob: 7f46f9bf469e7302a7fafd46c97c8c13aa6da59e [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 */
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Erik Andersen5cbdd712000-01-26 20:06:48 +000052#if defined BB_FEATURE_MOUNT_LOOP
53#include <fcntl.h>
54#include <sys/ioctl.h>
55#include <linux/loop.h>
56#endif
57
58
Eric Andersend0246fb1999-11-04 21:18:07 +000059#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000060# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000061const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000062# else
63# if defined BB_MTAB
64const char mtab_file[] = "/etc/mtab";
65# else
66# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
67# endif
68# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000069#endif
70
71
Eric Andersend73dc5b1999-11-10 23:13:02 +000072extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000073{
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
75 BB_VER, BB_BT);
76 fprintf(stderr, "Usage: %s\n", usage);
77 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000078}
79
80
Eric Andersend23f9ba1999-10-20 19:18:15 +000081#if defined (BB_INIT) || defined (BB_PS)
82
Eric Andersen0ecb54a1999-12-05 23:24:55 +000083#if ! defined BB_FEATURE_USE_PROCFS
84#error Sorry, I depend on the /proc filesystem right now.
85#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000086/* Returns kernel version encoded as major*65536 + minor*256 + patch,
87 * so, for example, to check if the kernel is greater than 2.2.11:
88 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
89 */
Erik Andersene49d5ec2000-02-08 19:58:47 +000090int get_kernel_revision()
Eric Andersend23f9ba1999-10-20 19:18:15 +000091{
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 FILE *file;
93 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +000094
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 file = fopen("/proc/sys/kernel/osrelease", "r");
96 if (file == NULL) {
97 /* bummer, /proc must not be mounted... */
98 return (0);
99 }
100 fscanf(file, "%d.%d.%d", &major, &minor, &patch);
101 fclose(file);
102 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000103}
Erik Andersene49d5ec2000-02-08 19:58:47 +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{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 struct stat statBuf;
116 int status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 if (followLinks == TRUE)
119 status = stat(fileName, &statBuf);
120 else
121 status = lstat(fileName, &statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000122
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 if (status < 0)
124 return FALSE;
125 if (S_ISDIR(statBuf.st_mode))
126 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
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140copyFile(const char *srcName, const char *destName,
141 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000142{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 int rfd;
144 int wfd;
145 int rcc;
146 int status;
147 char buf[BUF_SIZE];
148 struct stat srcStatBuf;
149 struct stat dstStatBuf;
150 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000151
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (followLinks == TRUE)
153 status = stat(srcName, &srcStatBuf);
154 else
155 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000156
Erik Andersenfac10d72000-02-07 05:29:42 +0000157 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 perror(srcName);
159 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000160 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161
162 if (followLinks == TRUE)
163 status = stat(destName, &dstStatBuf);
164 else
165 status = lstat(destName, &dstStatBuf);
166
167 if (status < 0) {
168 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 */
181 status = mkdir(destName, 0777777 ^ umask(0));
182 if (status < 0 && errno != EEXIST) {
183 perror(destName);
184 return FALSE;
185 }
186 } else if (S_ISLNK(srcStatBuf.st_mode)) {
187 char link_val[PATH_MAX + 1];
188 int link_size;
189
190 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
191 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
192 link_size = readlink(srcName, &link_val[0], PATH_MAX);
193 if (link_size < 0) {
194 perror(srcName);
195 return FALSE;
196 }
197 link_val[link_size] = '\0';
198 status = symlink(link_val, destName);
199 if (status < 0) {
200 perror(destName);
201 return FALSE;
202 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000203#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 if (setModes == TRUE) {
205 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
206 perror(destName);
207 return FALSE;
208 }
209 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000210#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 return TRUE;
212 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
213 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
214 if (mkfifo(destName, 0644) < 0) {
215 perror(destName);
216 return FALSE;
217 }
218 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
219 || S_ISSOCK(srcStatBuf.st_mode)) {
220 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
221 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
222 perror(destName);
223 return FALSE;
224 }
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
233 wfd =
234 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
235 srcStatBuf.st_mode);
236 if (wfd < 0) {
237 perror(destName);
238 close(rfd);
239 return FALSE;
240 }
241
242 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
243 if (fullWrite(wfd, buf, rcc) < 0)
244 goto error_exit;
245 }
246 if (rcc < 0) {
247 goto error_exit;
248 }
249
250 close(rfd);
251 if (close(wfd) < 0) {
252 return FALSE;
253 }
254 }
255
256 if (setModes == TRUE) {
257 /* This is fine, since symlinks never get here */
258 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
259 perror(destName);
260 exit FALSE;
261 }
262 if (chmod(destName, srcStatBuf.st_mode) < 0) {
263 perror(destName);
264 exit FALSE;
265 }
266 times.actime = srcStatBuf.st_atime;
267 times.modtime = srcStatBuf.st_mtime;
268 if (utime(destName, &times) < 0) {
269 perror(destName);
270 exit FALSE;
271 }
272 }
273
Erik Andersenfac10d72000-02-07 05:29:42 +0000274 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000275
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 error_exit:
277 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000278 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000280
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000282}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000284
285
286
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000287#if defined BB_TAR || defined BB_LS
288
289#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
290#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
291
292/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
293static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 0, 0, S_ISUID,
295 0, 0, S_ISGID,
296 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000297};
298
299/* The 9 mode bits to test */
300static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 S_IRUSR, S_IWUSR, S_IXUSR,
302 S_IRGRP, S_IWGRP, S_IXGRP,
303 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000304};
305
306#define MODE1 "rwxrwxrwx"
307#define MODE0 "---------"
308#define SMODE1 "..s..s..t"
309#define SMODE0 "..S..S..T"
310
Eric Andersencc8ed391999-10-05 16:24:54 +0000311/*
312 * Return the standard ls-like mode string from a file mode.
313 * This is static and so is overwritten on each call.
314 */
Eric Andersenf811e071999-10-09 00:25:00 +0000315const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000316{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000318
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 int i;
320
321 buf[0] = TYPECHAR(mode);
322 for (i = 0; i < 9; i++) {
323 if (mode & SBIT[i])
324 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
325 else
326 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
327 }
328 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000329}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000331
332
Eric Andersen1792f8c1999-12-09 06:11:36 +0000333#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000334/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000335 * Return the standard ls-like time string from a time_t
336 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000337 */
Eric Andersenf811e071999-10-09 00:25:00 +0000338const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000339{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 time_t now;
341 char *str;
342 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000343
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000345
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000347
Erik Andersene49d5ec2000-02-08 19:58:47 +0000348 strcpy(buf, &str[4]);
349 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000350
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
352 strcpy(&buf[7], &str[20]);
353 buf[11] = '\0';
354 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000355
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000357}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000359
Erik Andersenfac10d72000-02-07 05:29:42 +0000360#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000361/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000362 * Write all of the supplied buffer out to a file.
363 * This does multiple writes as necessary.
364 * Returns the amount written, or -1 on an error.
365 */
Eric Andersenf811e071999-10-09 00:25:00 +0000366int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000367{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 int cc;
369 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000370
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000372
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 while (len > 0) {
374 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000375
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 if (cc < 0)
377 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000378
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 buf += cc;
380 total += cc;
381 len -= cc;
382 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000383
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000385}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
388
Eric Andersen1792f8c1999-12-09 06:11:36 +0000389#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000390/*
391 * Read all of the supplied buffer from a file.
392 * This does multiple reads as necessary.
393 * Returns the amount read, or -1 on an error.
394 * A short read is returned on an end of file.
395 */
Eric Andersenf811e071999-10-09 00:25:00 +0000396int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000397{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 int cc;
399 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000400
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000402
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 while (len > 0) {
404 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000405
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 if (cc < 0)
407 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000408
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 if (cc == 0)
410 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000411
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 buf += cc;
413 total += cc;
414 len -= cc;
415 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000416
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000418}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000420
421
Erik Andersenfac10d72000-02-07 05:29:42 +0000422#if defined (BB_CHMOD_CHOWN_CHGRP) \
423 || defined (BB_CP_MV) \
424 || defined (BB_FIND) \
425 || defined (BB_LS) \
426 || defined (BB_INSMOD)
Eric Andersencc8ed391999-10-05 16:24:54 +0000427/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000428 * Walk down all the directories under the specified
429 * location, and do something (something specified
430 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000431 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000432 * Unfortunatly, while nftw(3) could replace this and reduce
433 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000434 * and so isn't sufficiently portable to take over since glibc2.1
435 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000436 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000437int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 int recurse, int followLinks, int depthFirst,
439 int (*fileAction) (const char *fileName,
440 struct stat * statbuf),
441 int (*dirAction) (const char *fileName,
442 struct stat * statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000443{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 int status;
445 struct stat statbuf;
446 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000447
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 if (followLinks == TRUE)
449 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000450 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000452
Eric Andersen2b69c401999-10-05 22:58:32 +0000453 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454#ifdef BB_DEBUG_PRINT_SCAFFOLD
455 fprintf(stderr,
456 "status=%d followLinks=%d TRUE=%d\n",
457 status, followLinks, TRUE);
458#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000459 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000460 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000461 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462
463 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
464 if (fileAction == NULL)
465 return TRUE;
466 else
467 return fileAction(fileName, &statbuf);
468 }
469
470 if (recurse == FALSE) {
471 if (S_ISDIR(statbuf.st_mode)) {
472 if (dirAction != NULL)
473 return (dirAction(fileName, &statbuf));
474 else
475 return TRUE;
476 }
477 }
478
479 if (S_ISDIR(statbuf.st_mode)) {
480 DIR *dir;
481
482 dir = opendir(fileName);
483 if (!dir) {
484 perror(fileName);
485 return FALSE;
486 }
487 if (dirAction != NULL && depthFirst == FALSE) {
488 status = dirAction(fileName, &statbuf);
489 if (status == FALSE) {
490 perror(fileName);
491 return FALSE;
492 }
493 }
494 while ((next = readdir(dir)) != NULL) {
495 char nextFile[PATH_MAX + 1];
496
497 if ((strcmp(next->d_name, "..") == 0)
498 || (strcmp(next->d_name, ".") == 0)) {
499 continue;
500 }
501 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
502 fprintf(stderr, name_too_long, "ftw");
503 return FALSE;
504 }
505 sprintf(nextFile, "%s/%s", fileName, next->d_name);
506 status =
507 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
508 fileAction, dirAction);
509 if (status < 0) {
510 closedir(dir);
511 return FALSE;
512 }
513 }
514 status = closedir(dir);
515 if (status < 0) {
516 perror(fileName);
517 return FALSE;
518 }
519 if (dirAction != NULL && depthFirst == TRUE) {
520 status = dirAction(fileName, &statbuf);
521 if (status == FALSE) {
522 perror(fileName);
523 return FALSE;
524 }
525 }
526 } else {
527 if (fileAction == NULL)
528 return TRUE;
529 else
530 return fileAction(fileName, &statbuf);
531 }
532 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000533}
534
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000536
Eric Andersenf6be9441999-10-13 21:12:06 +0000537
538
Erik Andersena8991081999-12-29 03:34:00 +0000539#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000540/*
541 * Attempt to create the directories along the specified path, except for
542 * the final component. The mode is given for the final directory only,
543 * while all previous ones get default protections. Errors are not reported
544 * here, as failures to restore files can be reported later.
545 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000547{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 char *cp;
549 char *cpOld;
550 char buf[PATH_MAX + 1];
551 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 strcpy(buf, name);
554 for (cp = buf; *cp == '/'; cp++);
555 cp = strchr(cp, '/');
556 while (cp) {
557 cpOld = cp;
558 cp = strchr(cp + 1, '/');
559 *cpOld = '\0';
560 retVal = mkdir(buf, cp ? 0777 : mode);
561 if (retVal != 0 && errno != EEXIST) {
562 perror(buf);
563 return FALSE;
564 }
565 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000566 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000568}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000570
571
572
573#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000574/* [ugoa]{+|-|=}[rwxst] */
575
576
577
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000579{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 mode_t andMode =
581
582 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000583 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 mode_t mode = 0;
585 mode_t groups = 0;
586 char type;
587 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000588
589 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 for (;;) {
591 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000592 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000593 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000594 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000596 continue;
597 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000599 continue;
600 case 'o':
601 groups |= S_IRWXO;
602 continue;
603 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000605 continue;
606 case '+':
607 case '=':
608 case '-':
609 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000610 if (groups == 0) /* The default is "all" */
611 groups |=
612 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000613 break;
614 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615 if (isdigit(c) && c >= '0' && c <= '7' &&
616 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000617 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000618 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000619 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000620 return (FALSE);
621 }
622 break;
623 }
624
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 while ((c = *s++) != '\0') {
626 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000627 case ',':
628 break;
629 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000630 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000631 continue;
632 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000634 continue;
635 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000637 continue;
638 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000640 continue;
641 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000642 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000643 continue;
644 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000645 *theMode &= andMode;
646 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000648 }
649 break;
650 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000652 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000653 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000654 /* fall through */
655 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000656 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000657 break;
658 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000659 andMode &= ~(mode & groups);
660 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000661 break;
662 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000663 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000664 *theMode &= andMode;
665 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000666 return (TRUE);
667}
Eric Andersence8f3b91999-10-20 07:03:36 +0000668
669
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000671
Eric Andersene674eb71999-10-19 20:52:57 +0000672
673
Eric Andersend23f9ba1999-10-20 19:18:15 +0000674
675
676
677
678#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
679
680/* Use this to avoid needing the glibc NSS stuff
681 * This uses storage buf to hold things.
682 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000683uid_t my_getid(const char *filename, char *name, uid_t id)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000684{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000685 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000686 char *rname, *start, *end, buf[128];
687 uid_t rid;
688
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000690 if (file == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 perror(filename);
692 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000693 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000694
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000696 if (buf[0] == '#')
697 continue;
698
699 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000701 if (end == NULL)
702 continue;
703 *end = '\0';
704 rname = start;
705
706 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000708 if (end == NULL)
709 continue;
710
711 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712 rid = (uid_t) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000713 if (end == start)
714 continue;
715
716 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 if (0 == strcmp(rname, name)) {
718 fclose(file);
719 return (rid);
720 }
721 }
722 if (id != -1 && id == rid) {
723 strncpy(name, rname, 8);
724 fclose(file);
725 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000726 }
727 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000728 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000729 return (-1);
730}
731
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732uid_t my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000733{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000734 return my_getid("/etc/passwd", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000735}
736
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737gid_t my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000738{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000739 return my_getid("/etc/group", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000740}
741
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742void my_getpwuid(char *name, uid_t uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000743{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 my_getid("/etc/passwd", name, uid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000745}
746
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747void my_getgrgid(char *group, gid_t gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000748{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 my_getid("/etc/group", group, gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000750}
751
752
Erik Andersene49d5ec2000-02-08 19:58:47 +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
Erik Andersene49d5ec2000-02-08 19:58:47 +0000764int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000765{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 char arg;
767
768 arg = 0;
769 return (ioctl(fd, KDGKBTYPE, &arg) == 0
770 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000771}
772
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000774{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000775 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 close(fd);
795 return -1;
796 }
797
798 /* success */
799 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000800}
801
802/*
803 * Get an fd for use with kbd/console ioctls.
804 * We try several things because opening /dev/console will fail
805 * if someone else used X (which does a chown on /dev/console).
806 *
807 * if tty_name is non-NULL, try this one instead.
808 */
809
Erik Andersene49d5ec2000-02-08 19:58:47 +0000810int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000811{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000812 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000813
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 if (tty_name) {
815 if (-1 == (fd = open_a_console(tty_name)))
816 return -1;
817 else
818 return fd;
819 }
820
821 fd = open_a_console("/dev/tty");
822 if (fd >= 0)
823 return fd;
824
825 fd = open_a_console("/dev/tty0");
826 if (fd >= 0)
827 return fd;
828
829 fd = open_a_console("/dev/console");
830 if (fd >= 0)
831 return fd;
832
833 for (fd = 0; fd < 3; fd++)
834 if (is_a_console(fd))
835 return fd;
836
837 fprintf(stderr,
838 "Couldnt get a file descriptor referring to the console\n");
839 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000840}
841
842
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000844
845
Erik Andersene49d5ec2000-02-08 19:58:47 +0000846#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000847
848/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000850{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000852
Erik Andersene49d5ec2000-02-08 19:58:47 +0000853 while (*haystack) {
854 if (!strncasecmp(haystack, needle, len))
855 break;
856 haystack++;
857 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000858
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859 if (!(*haystack))
860 haystack = NULL;
861
862 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000863}
864
Eric Andersenc1525e81999-10-29 00:07:31 +0000865/* This tries to find a needle in a haystack, but does so by
866 * only trying to match literal strings (look 'ma, no regexps!)
867 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000868 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000869 * -Erik Andersen
870 */
871extern int find_match(char *haystack, char *needle, int ignoreCase)
872{
873
Erik Andersene49d5ec2000-02-08 19:58:47 +0000874 if (ignoreCase == FALSE)
875 haystack = strstr(haystack, needle);
876 else
877 haystack = stristr(haystack, needle);
878 if (haystack == NULL)
879 return FALSE;
880 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000881}
882
883
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000884/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000885extern int replace_match(char *haystack, char *needle, char *newNeedle,
886 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +0000887{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000888 int foundOne = 0;
889 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000890
Erik Andersene49d5ec2000-02-08 19:58:47 +0000891 if (ignoreCase == FALSE)
892 where = strstr(haystack, needle);
893 else
894 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000895
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 if (strcmp(needle, newNeedle) == 0)
897 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000898
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899 oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
900 while (where != NULL) {
901 foundOne++;
902 strcpy(oldhayStack, haystack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000903#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 if (strlen(newNeedle) > strlen(needle)) {
905 haystack =
906 (char *) realloc(haystack,
907 (unsigned) (strlen(haystack) -
908 strlen(needle) +
909 strlen(newNeedle)));
910 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000911#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000912 for (slider = haystack, slider1 = oldhayStack; slider != where;
913 slider++, slider1++);
914 *slider = 0;
915 haystack = strcat(haystack, newNeedle);
916 slider1 += strlen(needle);
917 haystack = strcat(haystack, slider1);
918 where = strstr(slider, needle);
919 }
920 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000921
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 if (foundOne > 0)
923 return TRUE;
924 else
925 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000926}
927
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +0000929
930
Eric Andersenb186d981999-12-03 09:19:54 +0000931#if defined BB_FIND
932/*
933 * Routine to see if a text string is matched by a wildcard pattern.
934 * Returns TRUE if the text is matched, or FALSE if it is not matched
935 * or if the pattern is invalid.
936 * * matches zero or more characters
937 * ? matches a single character
938 * [abc] matches 'a', 'b' or 'c'
939 * \c quotes character c
940 * Adapted from code written by Ingo Wilken, and
941 * then taken from sash, Copyright (c) 1999 by David I. Bell
942 * Permission is granted to use, distribute, or modify this source,
943 * provided that this copyright notice remains intact.
944 * Permission to distribute this code under the GPL has been granted.
945 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000946extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +0000947{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948 const char *retryPat;
949 const char *retryText;
950 int ch;
951 int found;
Eric Andersenb186d981999-12-03 09:19:54 +0000952
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953 retryPat = NULL;
954 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +0000955
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +0000957 ch = *pattern++;
958
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959 switch (ch) {
960 case '*':
961 retryPat = pattern;
962 retryText = text;
963 break;
964
965 case '[':
966 found = FALSE;
967
968 while ((ch = *pattern++) != ']') {
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 pattern = retryPat;
982 text = ++retryText;
983 }
984
985 /* fall into next case */
986
987 case '?':
988 if (*text++ == '\0')
989 return FALSE;
990
991 break;
992
993 case '\\':
994 ch = *pattern++;
995
996 if (ch == '\0')
997 return FALSE;
998
999 /* fall into next case */
1000
1001 default:
1002 if (*text == ch) {
1003 if (*text)
1004 text++;
1005 break;
1006 }
1007
1008 if (*text) {
1009 pattern = retryPat;
1010 text = ++retryText;
1011 break;
1012 }
1013
Eric Andersenb186d981999-12-03 09:19:54 +00001014 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001015 }
1016
Erik Andersene49d5ec2000-02-08 19:58:47 +00001017 if (pattern == NULL)
1018 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001019 }
1020
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001022}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001024
1025
Eric Andersen29d2e361999-11-06 06:07:27 +00001026
1027
Erik Andersen7dc16072000-01-04 01:10:25 +00001028#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001029/*
1030 * Given a block device, find the mount table entry if that block device
1031 * is mounted.
1032 *
1033 * Given any other file (or directory), find the mount table entry for its
1034 * filesystem.
1035 */
1036extern struct mntent *findMountPoint(const char *name, const char *table)
1037{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 struct stat s;
1039 dev_t mountDevice;
1040 FILE *mountTable;
1041 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001042
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043 if (stat(name, &s) != 0)
1044 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001045
Erik Andersene49d5ec2000-02-08 19:58:47 +00001046 if ((s.st_mode & S_IFMT) == S_IFBLK)
1047 mountDevice = s.st_rdev;
1048 else
1049 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001050
1051
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052 if ((mountTable = setmntent(table, "r")) == 0)
1053 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001054
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 while ((mountEntry = getmntent(mountTable)) != 0) {
1056 if (strcmp(name, mountEntry->mnt_dir) == 0
1057 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1058 break;
1059 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1060 break;
1061 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1062 break;
1063 }
1064 endmntent(mountTable);
1065 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001066}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001067#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001068
1069
1070
Eric Andersen1792f8c1999-12-09 06:11:36 +00001071#if defined BB_DD || defined BB_TAIL
1072/*
1073 * Read a number with a possible multiplier.
1074 * Returns -1 if the number format is illegal.
1075 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001076extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001077{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001079
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 if (!isDecimal(*cp))
1081 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001082
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001084
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085 while (isDecimal(*cp))
1086 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001087
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 switch (*cp++) {
1089 case 'M':
1090 case 'm': /* `tail' uses it traditionally */
1091 value *= 1048576;
1092 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001093
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 case 'k':
1095 value *= 1024;
1096 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001097
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 case 'b':
1099 value *= 512;
1100 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001101
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 case 'w':
1103 value *= 2;
1104 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001105
Erik Andersene49d5ec2000-02-08 19:58:47 +00001106 case '\0':
1107 return value;
1108
1109 default:
1110 return -1;
1111 }
1112
1113 if (*cp)
1114 return -1;
1115
Eric Andersen1792f8c1999-12-09 06:11:36 +00001116 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001117}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001118#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001119
Eric Andersen6805d5d1999-12-09 22:39:55 +00001120
Erik Andersen31cf8e02000-02-07 20:31:19 +00001121#if defined BB_INIT || defined BB_SYSLOGD
1122/* try to open up the specified device */
1123extern int device_open(char *device, int mode)
1124{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001125 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001126
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001128
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 /* Retry up to 5 times */
1130 for (f = 0; f < 5; f++)
1131 if ((fd = open(device, m, 0600)) >= 0)
1132 break;
1133 if (fd < 0)
1134 return fd;
1135 /* Reset original flags. */
1136 if (m != mode)
1137 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001138 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001139}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001140#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001141
1142
Erik Andersene49d5ec2000-02-08 19:58:47 +00001143#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
Eric Andersen6805d5d1999-12-09 22:39:55 +00001144
1145#if ! defined BB_FEATURE_USE_PROCFS
1146#error Sorry, I depend on the /proc filesystem right now.
1147#endif
John Beppuf95ca971999-12-09 22:10:18 +00001148/* findInitPid()
1149 *
1150 * This finds the pid of init (which is not always 1).
1151 * Currently, it's implemented by rummaging through the proc filesystem.
1152 *
1153 * [return]
1154 * 0 failure
1155 * pid when init's pid is found.
1156 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157extern pid_t findInitPid()
John Beppuf95ca971999-12-09 22:10:18 +00001158{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001159 pid_t init_pid;
1160 char filename[256];
1161 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 /* no need to opendir ;) */
1164 for (init_pid = 1; init_pid < 65536; init_pid++) {
1165 FILE *status;
John Beppuf95ca971999-12-09 22:10:18 +00001166
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 sprintf(filename, "/proc/%d/status", init_pid);
1168 status = fopen(filename, "r");
1169 if (!status) {
1170 continue;
1171 }
1172 fgets(buffer, 256, status);
1173 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001174
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175 if ((strstr(buffer, "init\n") != NULL)) {
1176 return init_pid;
1177 }
John Beppuf95ca971999-12-09 22:10:18 +00001178 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 return 0;
John Beppuf95ca971999-12-09 22:10:18 +00001180}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181#endif /* BB_INIT || BB_HALT || BB_REBOOT */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001182
Erik Andersenfac10d72000-02-07 05:29:42 +00001183#if defined BB_GUNZIP \
1184 || defined BB_GZIP \
1185 || defined BB_PRINTF \
1186 || defined BB_TAIL
Erik Andersene49d5ec2000-02-08 19:58:47 +00001187extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001188{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001190
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 if (cp == NULL) {
1192 error("out of memory");
1193 }
1194 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001195}
1196
1197extern void error(char *msg)
1198{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 fprintf(stderr, "\n%s\n", msg);
1200 exit(1);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001201}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001202#endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001203
Erik Andersen7dc16072000-01-04 01:10:25 +00001204#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1205extern int vdprintf(int d, const char *format, va_list ap)
1206{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001207 char buf[BUF_SIZE];
1208 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001209
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210 len = vsprintf(buf, format, ap);
1211 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001212}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001214
Erik Andersen5cbdd712000-01-26 20:06:48 +00001215#if defined BB_FEATURE_MOUNT_LOOP
1216extern int del_loop(const char *device)
1217{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001218 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001219
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 if ((fd = open(device, O_RDONLY)) < 0) {
1221 perror(device);
1222 return (FALSE);
1223 }
1224 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1225 perror("ioctl: LOOP_CLR_FD");
1226 return (FALSE);
1227 }
1228 close(fd);
1229 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001230}
Erik Andersende7965c2000-01-26 23:49:21 +00001231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232extern int set_loop(const char *device, const char *file, int offset,
1233 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001234{
1235 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 int fd, ffd, mode;
1237
Erik Andersende7965c2000-01-26 23:49:21 +00001238 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001239 if ((ffd = open(file, mode)) < 0 && !*loopro
1240 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1241 perror(file);
1242 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001243 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 if ((fd = open(device, mode)) < 0) {
1245 close(ffd);
1246 perror(device);
1247 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001248 }
1249 *loopro = (mode == O_RDONLY);
1250
1251 memset(&loopinfo, 0, sizeof(loopinfo));
1252 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001253 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001254
1255 loopinfo.lo_offset = offset;
1256
1257 loopinfo.lo_encrypt_key_size = 0;
1258 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1259 perror("ioctl: LOOP_SET_FD");
1260 close(fd);
1261 close(ffd);
1262 return 1;
1263 }
1264 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1265 (void) ioctl(fd, LOOP_CLR_FD, 0);
1266 perror("ioctl: LOOP_SET_STATUS");
1267 close(fd);
1268 close(ffd);
1269 return 1;
1270 }
1271 close(fd);
1272 close(ffd);
1273 return 0;
1274}
1275
Erik Andersene49d5ec2000-02-08 19:58:47 +00001276extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001277{
1278 char dev[20];
1279 int i, fd;
1280 struct stat statbuf;
1281 struct loop_info loopinfo;
1282
Erik Andersene49d5ec2000-02-08 19:58:47 +00001283 for (i = 0; i <= 7; i++) {
1284 sprintf(dev, "/dev/loop%d", i);
1285 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1286 if ((fd = open(dev, O_RDONLY)) >= 0) {
1287 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1288 if (errno == ENXIO) { /* probably free */
1289 close(fd);
1290 return strdup(dev);
1291 }
1292 }
1293 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001294 }
Erik Andersende7965c2000-01-26 23:49:21 +00001295 }
Erik Andersende7965c2000-01-26 23:49:21 +00001296 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001297 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001298}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001299#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001300
Erik Andersen5cbdd712000-01-26 20:06:48 +00001301
Eric Andersencc8ed391999-10-05 16:24:54 +00001302/* END CODE */