blob: 469275f2d6884f524023aaaabdc063fc5ba450bb [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 */
154 if (mkdir(destName, 0777777 ^ umask(0))) {
155 perror(destName);
156 return (FALSE);
157 }
158 } else if (S_ISLNK(srcStatBuf.st_mode)) {
159 char *link_val;
160 int link_size;
161
162 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
163 link_val = (char *) alloca(PATH_MAX + 2);
164 link_size = readlink(srcName, link_val, PATH_MAX + 1);
165 if (link_size < 0) {
166 perror(srcName);
167 return (FALSE);
168 }
169 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000170 link_size = symlink(link_val, destName);
171 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000172 perror(destName);
173 return (FALSE);
174 }
175 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
176 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
177 if (mkfifo(destName, 644)) {
178 perror(destName);
179 return (FALSE);
180 }
181 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
182 || S_ISSOCK (srcStatBuf.st_mode)) {
183 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
184 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
185 perror(destName);
186 return (FALSE);
187 }
188 } else if (S_ISREG(srcStatBuf.st_mode)) {
189 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
190 rfd = open(srcName, O_RDONLY);
191 if (rfd < 0) {
192 perror(srcName);
193 return FALSE;
194 }
195
196 wfd = creat(destName, srcStatBuf.st_mode);
197 if (wfd < 0) {
198 perror(destName);
199 close(rfd);
200 return FALSE;
201 }
202
203 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
204 if (fullWrite(wfd, buf, rcc) < 0)
205 goto error_exit;
206 }
207 if (rcc < 0) {
208 goto error_exit;
209 }
210
211 close(rfd);
212 if (close(wfd) < 0) {
213 return FALSE;
214 }
215 }
216
217 if (setModes == TRUE) {
218 //fprintf(stderr, "Setting permissions for %s\n", destName);
219 chmod(destName, srcStatBuf.st_mode);
220 if (followLinks == TRUE)
221 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
222 else
223 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
224
225 times.actime = srcStatBuf.st_atime;
226 times.modtime = srcStatBuf.st_mtime;
227
228 utime(destName, &times);
229 }
230
231 return TRUE;
232
233
234 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000235 perror(destName);
236 close(rfd);
237 close(wfd);
238
239 return FALSE;
240}
Eric Andersencc8ed391999-10-05 16:24:54 +0000241#endif
242
243
244
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000245#if defined BB_TAR || defined BB_LS
246
247#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
248#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
249
250/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
251static const mode_t SBIT[] = {
252 0, 0, S_ISUID,
253 0, 0, S_ISGID,
254 0, 0, S_ISVTX
255};
256
257/* The 9 mode bits to test */
258static const mode_t MBIT[] = {
259 S_IRUSR, S_IWUSR, S_IXUSR,
260 S_IRGRP, S_IWGRP, S_IXGRP,
261 S_IROTH, S_IWOTH, S_IXOTH
262};
263
264#define MODE1 "rwxrwxrwx"
265#define MODE0 "---------"
266#define SMODE1 "..s..s..t"
267#define SMODE0 "..S..S..T"
268
Eric Andersencc8ed391999-10-05 16:24:54 +0000269/*
270 * Return the standard ls-like mode string from a file mode.
271 * This is static and so is overwritten on each call.
272 */
Eric Andersenf811e071999-10-09 00:25:00 +0000273const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000274{
Eric Andersenf811e071999-10-09 00:25:00 +0000275 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000276
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000277 int i;
278 buf[0] = TYPECHAR(mode);
279 for (i=0; i<9; i++) {
280 if (mode & SBIT[i])
281 buf[i+1] = (mode & MBIT[i])?
282 SMODE1[i] : SMODE0[i];
283 else
284 buf[i+1] = (mode & MBIT[i])?
285 MODE1[i] : MODE0[i];
286 }
Eric Andersenf811e071999-10-09 00:25:00 +0000287 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000288}
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000289#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000290
291
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000292#ifdef BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000293/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000294 * Return the standard ls-like time string from a time_t
295 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000296 */
Eric Andersenf811e071999-10-09 00:25:00 +0000297const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000298{
Eric Andersenf811e071999-10-09 00:25:00 +0000299 time_t now;
300 char *str;
301 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000302
Eric Andersenf811e071999-10-09 00:25:00 +0000303 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000304
Eric Andersenf811e071999-10-09 00:25:00 +0000305 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000306
Eric Andersenf811e071999-10-09 00:25:00 +0000307 strcpy(buf, &str[4]);
308 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000309
Eric Andersenf811e071999-10-09 00:25:00 +0000310 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
311 strcpy(&buf[7], &str[20]);
312 buf[11] = '\0';
313 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000314
Eric Andersenf811e071999-10-09 00:25:00 +0000315 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000316}
317
Eric Andersen17d49ef1999-10-06 20:25:32 +0000318/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000319 * Write all of the supplied buffer out to a file.
320 * This does multiple writes as necessary.
321 * Returns the amount written, or -1 on an error.
322 */
Eric Andersenf811e071999-10-09 00:25:00 +0000323int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000324{
Eric Andersenf811e071999-10-09 00:25:00 +0000325 int cc;
326 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000327
Eric Andersenf811e071999-10-09 00:25:00 +0000328 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000329
Eric Andersenf811e071999-10-09 00:25:00 +0000330 while (len > 0) {
331 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000332
Eric Andersenf811e071999-10-09 00:25:00 +0000333 if (cc < 0)
334 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000335
Eric Andersenf811e071999-10-09 00:25:00 +0000336 buf += cc;
337 total += cc;
338 len -= cc;
339 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000340
Eric Andersenf811e071999-10-09 00:25:00 +0000341 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000342}
343
344
345/*
346 * Read all of the supplied buffer from a file.
347 * This does multiple reads as necessary.
348 * Returns the amount read, or -1 on an error.
349 * A short read is returned on an end of file.
350 */
Eric Andersenf811e071999-10-09 00:25:00 +0000351int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000352{
Eric Andersenf811e071999-10-09 00:25:00 +0000353 int cc;
354 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000355
Eric Andersenf811e071999-10-09 00:25:00 +0000356 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000357
Eric Andersenf811e071999-10-09 00:25:00 +0000358 while (len > 0) {
359 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000360
Eric Andersenf811e071999-10-09 00:25:00 +0000361 if (cc < 0)
362 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000363
Eric Andersenf811e071999-10-09 00:25:00 +0000364 if (cc == 0)
365 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000366
Eric Andersenf811e071999-10-09 00:25:00 +0000367 buf += cc;
368 total += cc;
369 len -= cc;
370 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000371
Eric Andersenf811e071999-10-09 00:25:00 +0000372 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000373}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000374#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000375
376
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000377#if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
Eric Andersencc8ed391999-10-05 16:24:54 +0000378/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000379 * Walk down all the directories under the specified
380 * location, and do something (something specified
381 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000382 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000383 * Unfortunatly, while nftw(3) could replace this and reduce
384 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000385 * and so isn't sufficiently portable to take over since glibc2.1
386 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000387 */
388int
Eric Andersen63a0e531999-10-22 05:12:14 +0000389recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersen9b587181999-10-17 05:43:39 +0000390 int (*fileAction) (const char *fileName, struct stat* statbuf),
391 int (*dirAction) (const char *fileName, struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000392{
Eric Andersenf811e071999-10-09 00:25:00 +0000393 int status;
394 struct stat statbuf;
395 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000396
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000397 if (followLinks == TRUE)
Eric Andersenf811e071999-10-09 00:25:00 +0000398 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000399 else
400 status = lstat(fileName, &statbuf);
401
Eric Andersencc8ed391999-10-05 16:24:54 +0000402 if (status < 0) {
403 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000404 return (FALSE);
405 }
406
Eric Andersen50d63601999-11-09 01:47:36 +0000407 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
408 if (fileAction == NULL)
409 return (TRUE);
410 else
411 return (fileAction(fileName, &statbuf));
412 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000413
Eric Andersenf811e071999-10-09 00:25:00 +0000414 if (recurse == FALSE) {
415 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000416 if (dirAction != NULL)
Eric Andersen9b587181999-10-17 05:43:39 +0000417 return (dirAction(fileName, &statbuf));
Eric Andersenf811e071999-10-09 00:25:00 +0000418 else
Eric Andersen3c163821999-10-14 22:16:57 +0000419 return (TRUE);
420 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000421 }
422
Eric Andersen2b69c401999-10-05 22:58:32 +0000423 if (S_ISDIR(statbuf.st_mode)) {
424 DIR *dir;
425 dir = opendir(fileName);
426 if (!dir) {
427 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000428 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000429 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000430 if (dirAction != NULL && depthFirst == FALSE) {
Eric Andersen9b587181999-10-17 05:43:39 +0000431 status = dirAction(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000432 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000433 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000434 return (FALSE);
435 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000436 }
Eric Andersenf811e071999-10-09 00:25:00 +0000437 while ((next = readdir(dir)) != NULL) {
438 char nextFile[NAME_MAX];
439 if ((strcmp(next->d_name, "..") == 0)
440 || (strcmp(next->d_name, ".") == 0)) {
441 continue;
442 }
443 sprintf(nextFile, "%s/%s", fileName, next->d_name);
444 status =
Eric Andersen63a0e531999-10-22 05:12:14 +0000445 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Eric Andersenbed30e91999-10-18 19:02:32 +0000446 fileAction, dirAction);
Eric Andersenf811e071999-10-09 00:25:00 +0000447 if (status < 0) {
448 closedir(dir);
449 return (FALSE);
450 }
451 }
452 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000453 if (status < 0) {
454 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000455 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000456 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000457 if (dirAction != NULL && depthFirst == TRUE) {
Eric Andersenbed30e91999-10-18 19:02:32 +0000458 status = dirAction(fileName, &statbuf);
459 if (status == FALSE) {
460 perror(fileName);
461 return (FALSE);
462 }
463 }
Eric Andersenf811e071999-10-09 00:25:00 +0000464 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000465 if (fileAction == NULL)
466 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000467 else
Eric Andersen9b587181999-10-17 05:43:39 +0000468 return (fileAction(fileName, &statbuf));
Eric Andersencc8ed391999-10-05 16:24:54 +0000469 }
Eric Andersenf811e071999-10-09 00:25:00 +0000470 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000471}
472
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000473#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000474
Eric Andersenf6be9441999-10-13 21:12:06 +0000475
476
477#if defined (BB_TAR) || defined (BB_MKDIR)
478/*
479 * Attempt to create the directories along the specified path, except for
480 * the final component. The mode is given for the final directory only,
481 * while all previous ones get default protections. Errors are not reported
482 * here, as failures to restore files can be reported later.
483 */
484extern void createPath (const char *name, int mode)
485{
486 char *cp;
487 char *cpOld;
488 char buf[NAME_MAX];
489
Eric Andersencb41c2e1999-11-22 07:41:00 +0000490 strcpy( buf, name);
Eric Andersenf6be9441999-10-13 21:12:06 +0000491 cp = strchr (buf, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000492 while (cp) {
493 cpOld = cp;
494 cp = strchr (cp + 1, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000495 *cpOld = '\0';
Eric Andersenb6a44b81999-11-13 04:47:09 +0000496 mkdir (buf, cp ? 0777 : mode);
Eric Andersenf6be9441999-10-13 21:12:06 +0000497 *cpOld = '/';
498 }
499}
500#endif
501
502
503
504#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000505/* [ugoa]{+|-|=}[rwxst] */
506
507
508
509extern int
510parse_mode( const char* s, mode_t* theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000511{
Eric Andersence8f3b91999-10-20 07:03:36 +0000512 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
513 mode_t orMode = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000514 mode_t mode = 0;
Eric Andersence8f3b91999-10-20 07:03:36 +0000515 mode_t groups = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000516 char type;
517 char c;
518
519 do {
520 for ( ; ; ) {
521 switch ( c = *s++ ) {
522 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000523 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000524 case 'u':
525 groups |= S_ISUID|S_IRWXU;
526 continue;
527 case 'g':
528 groups |= S_ISGID|S_IRWXG;
529 continue;
530 case 'o':
531 groups |= S_IRWXO;
532 continue;
533 case 'a':
534 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
535 continue;
536 case '+':
537 case '=':
538 case '-':
539 type = c;
Eric Andersence8f3b91999-10-20 07:03:36 +0000540 if ( groups == 0 ) /* The default is "all" */
Eric Andersenf6be9441999-10-13 21:12:06 +0000541 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
542 break;
543 default:
Eric Andersenfa0540f1999-10-22 18:18:31 +0000544 if ( isdigit(c) && c >= '0' && c <= '7' &&
545 mode == 0 && groups == 0 ) {
546 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000547 return (TRUE);
548 }
549 else
550 return (FALSE);
551 }
552 break;
553 }
554
555 while ( (c = *s++) != '\0' ) {
556 switch ( c ) {
557 case ',':
558 break;
559 case 'r':
560 mode |= S_IRUSR|S_IRGRP|S_IROTH;
561 continue;
562 case 'w':
563 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
564 continue;
565 case 'x':
566 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
567 continue;
568 case 's':
569 mode |= S_IXGRP|S_ISUID|S_ISGID;
570 continue;
571 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000572 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000573 continue;
574 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000575 *theMode &= andMode;
576 *theMode |= orMode;
577 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000578 }
579 break;
580 }
581 switch ( type ) {
582 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000583 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000584 /* fall through */
585 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000586 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000587 break;
588 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000589 andMode &= ~(mode & groups);
590 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000591 break;
592 }
593 } while ( c == ',' );
Eric Andersence8f3b91999-10-20 07:03:36 +0000594 *theMode &= andMode;
595 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000596 return (TRUE);
597}
Eric Andersence8f3b91999-10-20 07:03:36 +0000598
599
Eric Andersenf6be9441999-10-13 21:12:06 +0000600#endif
601
Eric Andersene674eb71999-10-19 20:52:57 +0000602
603
Eric Andersend23f9ba1999-10-20 19:18:15 +0000604
605
606
607
608#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
609
610/* Use this to avoid needing the glibc NSS stuff
611 * This uses storage buf to hold things.
612 * */
613uid_t
614my_getid(const char *filename, char *name, uid_t id)
615{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000616 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000617 char *rname, *start, *end, buf[128];
618 uid_t rid;
619
Eric Andersenaa0765e1999-10-22 04:30:20 +0000620 file=fopen(filename,"r");
621 if (file == NULL) {
622 perror(filename);
623 return (-1);
624 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000625
Eric Andersenaa0765e1999-10-22 04:30:20 +0000626 while (fgets (buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000627 if (buf[0] == '#')
628 continue;
629
630 start = buf;
631 end = strchr (start, ':');
632 if (end == NULL)
633 continue;
634 *end = '\0';
635 rname = start;
636
637 start = end + 1;
638 end = strchr (start, ':');
639 if (end == NULL)
640 continue;
641
642 start = end + 1;
643 rid = (uid_t) strtol (start, &end, 10);
644 if (end == start)
645 continue;
646
647 if (name) {
Eric Andersen48091fb1999-12-09 01:15:52 +0000648 if (0 == strcmp(rname, name)) {
649 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000650 return( rid);
Eric Andersen48091fb1999-12-09 01:15:52 +0000651 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000652 }
653 if ( id != -1 && id == rid ) {
654 strncpy(name, rname, 8);
Eric Andersen48091fb1999-12-09 01:15:52 +0000655 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000656 return( TRUE);
657 }
658 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000659 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000660 return (-1);
661}
662
663uid_t
664my_getpwnam(char *name)
665{
666 return my_getid("/etc/passwd", name, -1);
667}
668
669gid_t
670my_getgrnam(char *name)
671{
672 return my_getid("/etc/group", name, -1);
673}
674
675void
676my_getpwuid(char* name, uid_t uid)
677{
678 my_getid("/etc/passwd", name, uid);
679}
680
681void
682my_getgrgid(char* group, gid_t gid)
683{
684 my_getid("/etc/group", group, gid);
685}
686
687
688#endif
689
690
Eric Andersenaa0765e1999-10-22 04:30:20 +0000691
Eric Andersen0460ff21999-10-25 23:32:44 +0000692
693#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
694
695
696#include <linux/kd.h>
697#include <sys/ioctl.h>
698
699int is_a_console(int fd)
700{
701 char arg;
702
703 arg = 0;
704 return (ioctl(fd, KDGKBTYPE, &arg) == 0
705 && ((arg == KB_101) || (arg == KB_84)));
706}
707
708static int open_a_console(char *fnam)
709{
710 int fd;
711
712 /* try read-only */
713 fd = open(fnam, O_RDWR);
714
715 /* if failed, try read-only */
716 if (fd < 0 && errno == EACCES)
717 fd = open(fnam, O_RDONLY);
718
719 /* if failed, try write-only */
720 if (fd < 0 && errno == EACCES)
721 fd = open(fnam, O_WRONLY);
722
723 /* if failed, fail */
724 if (fd < 0)
725 return -1;
726
727 /* if not a console, fail */
728 if (! is_a_console(fd))
729 {
730 close(fd);
731 return -1;
732 }
733
734 /* success */
735 return fd;
736}
737
738/*
739 * Get an fd for use with kbd/console ioctls.
740 * We try several things because opening /dev/console will fail
741 * if someone else used X (which does a chown on /dev/console).
742 *
743 * if tty_name is non-NULL, try this one instead.
744 */
745
746int get_console_fd(char* tty_name)
747{
748 int fd;
749
750 if (tty_name)
751 {
752 if (-1 == (fd = open_a_console(tty_name)))
753 return -1;
754 else
755 return fd;
756 }
757
758 fd = open_a_console("/dev/tty");
759 if (fd >= 0)
760 return fd;
761
762 fd = open_a_console("/dev/tty0");
763 if (fd >= 0)
764 return fd;
765
766 fd = open_a_console("/dev/console");
767 if (fd >= 0)
768 return fd;
769
770 for (fd = 0; fd < 3; fd++)
771 if (is_a_console(fd))
772 return fd;
773
774 fprintf(stderr,
775 "Couldnt get a file descriptor referring to the console\n");
776 return -1; /* total failure */
777}
778
779
780#endif
781
782
Eric Andersenb186d981999-12-03 09:19:54 +0000783#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000784
785/* Do a case insensitive strstr() */
786char* stristr(char *haystack, const char *needle)
787{
788 int len = strlen( needle );
789 while( *haystack ) {
790 if( !strncasecmp( haystack, needle, len ) )
791 break;
792 haystack++;
793 }
794
795 if( !(*haystack) )
796 haystack = NULL;
797
798 return haystack;
799}
800
Eric Andersenc1525e81999-10-29 00:07:31 +0000801/* This tries to find a needle in a haystack, but does so by
802 * only trying to match literal strings (look 'ma, no regexps!)
803 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000804 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000805 * -Erik Andersen
806 */
807extern int find_match(char *haystack, char *needle, int ignoreCase)
808{
809
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000810 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000811 haystack = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000812 else
813 haystack = stristr (haystack, needle);
814 if (haystack == NULL)
815 return FALSE;
816 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000817}
818
819
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000820/* This performs substitutions after a string match has been found. */
Eric Andersenc1525e81999-10-29 00:07:31 +0000821extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
822{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000823 int foundOne=0;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000824 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000825
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000826 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000827 where = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000828 else
829 where = stristr (haystack, needle);
830
831 if (strcmp(needle, newNeedle)==0)
832 return FALSE;
833
834 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
835 while(where!=NULL) {
836 foundOne++;
837 strcpy(oldhayStack, haystack);
838#if 0
839 if ( strlen(newNeedle) > strlen(needle)) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000840 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
841 strlen(needle) + strlen(newNeedle)));
Eric Andersenc1525e81999-10-29 00:07:31 +0000842 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000843#endif
844 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
845 *slider=0;
846 haystack=strcat(haystack, newNeedle);
847 slider1+=strlen(needle);
848 haystack = strcat(haystack, slider1);
849 where = strstr (slider, needle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000850 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000851 free( oldhayStack);
852
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000853 if (foundOne > 0)
Eric Andersenc1525e81999-10-29 00:07:31 +0000854 return TRUE;
855 else
856 return FALSE;
857}
858
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000859
Eric Andersenc1525e81999-10-29 00:07:31 +0000860#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000861
862
Eric Andersenb186d981999-12-03 09:19:54 +0000863#if defined BB_FIND
864/*
865 * Routine to see if a text string is matched by a wildcard pattern.
866 * Returns TRUE if the text is matched, or FALSE if it is not matched
867 * or if the pattern is invalid.
868 * * matches zero or more characters
869 * ? matches a single character
870 * [abc] matches 'a', 'b' or 'c'
871 * \c quotes character c
872 * Adapted from code written by Ingo Wilken, and
873 * then taken from sash, Copyright (c) 1999 by David I. Bell
874 * Permission is granted to use, distribute, or modify this source,
875 * provided that this copyright notice remains intact.
876 * Permission to distribute this code under the GPL has been granted.
877 */
878extern int
879check_wildcard_match(const char* text, const char* pattern)
880{
881 const char* retryPat;
882 const char* retryText;
883 int ch;
884 int found;
885
886 retryPat = NULL;
887 retryText = NULL;
888
889 while (*text || *pattern)
890 {
891 ch = *pattern++;
892
893 switch (ch)
894 {
895 case '*':
896 retryPat = pattern;
897 retryText = text;
898 break;
899
900 case '[':
901 found = FALSE;
902
903 while ((ch = *pattern++) != ']')
904 {
905 if (ch == '\\')
906 ch = *pattern++;
907
908 if (ch == '\0')
909 return FALSE;
910
911 if (*text == ch)
912 found = TRUE;
913 }
914
915 //if (!found)
916 if (found==TRUE)
917 {
918 pattern = retryPat;
919 text = ++retryText;
920 }
921
922 /* fall into next case */
923
924 case '?':
925 if (*text++ == '\0')
926 return FALSE;
927
928 break;
929
930 case '\\':
931 ch = *pattern++;
932
933 if (ch == '\0')
934 return FALSE;
935
936 /* fall into next case */
937
938 default:
939 if (*text == ch)
940 {
941 if (*text)
942 text++;
943 break;
944 }
945
946 if (*text)
947 {
948 pattern = retryPat;
949 text = ++retryText;
950 break;
951 }
952
953 return FALSE;
954 }
955
956 if (pattern == NULL)
957 return FALSE;
958 }
959
960 return TRUE;
961}
962#endif
963
964
Eric Andersen29d2e361999-11-06 06:07:27 +0000965
966
967#if defined BB_DF | defined BB_MTAB
968/*
969 * Given a block device, find the mount table entry if that block device
970 * is mounted.
971 *
972 * Given any other file (or directory), find the mount table entry for its
973 * filesystem.
974 */
975extern struct mntent *findMountPoint(const char *name, const char *table)
976{
977 struct stat s;
978 dev_t mountDevice;
979 FILE *mountTable;
980 struct mntent *mountEntry;
981
982 if (stat(name, &s) != 0)
983 return 0;
984
985 if ((s.st_mode & S_IFMT) == S_IFBLK)
986 mountDevice = s.st_rdev;
987 else
988 mountDevice = s.st_dev;
989
990
991 if ((mountTable = setmntent(table, "r")) == 0)
992 return 0;
993
994 while ((mountEntry = getmntent(mountTable)) != 0) {
995 if (strcmp(name, mountEntry->mnt_dir) == 0
996 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
997 break;
998 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
999 break;
1000 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1001 break;
1002 }
1003 endmntent(mountTable);
1004 return mountEntry;
1005}
1006
1007#endif
1008
1009
1010
1011#if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
1012extern void whine_if_fstab_is_missing()
1013{
1014 struct stat statBuf;
1015 if (stat("/etc/fstab", &statBuf) < 0)
1016 fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1017}
1018#endif
1019
1020
Eric Andersencc8ed391999-10-05 16:24:54 +00001021/* END CODE */
Eric Andersenaa0765e1999-10-22 04:30:20 +00001022
Eric Andersen29d2e361999-11-06 06:07:27 +00001023
Eric Andersenb186d981999-12-03 09:19:54 +00001024
1025
1026
1027
1028
1029
1030