blob: b4dd0ccc3e79e9166b322fe3f6678f785a0c22f1 [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#ifdef BB_MTAB
40const char mtab_file[] = "/etc/mtab";
41#else
42#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
43const char mtab_file[] = "/proc/mounts";
44#endif
45#endif
46
47
Eric Andersenb0e9a701999-10-18 22:28:26 +000048/* volatile so gcc knows this is the enod of the line */
Eric Andersend73dc5b1999-11-10 23:13:02 +000049extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000050{
Eric Andersena07f0b01999-10-22 19:49:09 +000051 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
Eric Andersenb0e9a701999-10-18 22:28:26 +000052 fprintf(stderr, "Usage: %s\n", usage);
53 exit(FALSE);
54}
55
56
Eric Andersend23f9ba1999-10-20 19:18:15 +000057#if defined (BB_INIT) || defined (BB_PS)
58
59/* Returns kernel version encoded as major*65536 + minor*256 + patch,
60 * so, for example, to check if the kernel is greater than 2.2.11:
61 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
62 */
63int
64get_kernel_revision()
65{
Eric Andersenaa0765e1999-10-22 04:30:20 +000066 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +000067 int major=0, minor=0, patch=0;
Eric Andersenaa0765e1999-10-22 04:30:20 +000068 char* filename="/proc/sys/kernel/osrelease";
Eric Andersend23f9ba1999-10-20 19:18:15 +000069
Eric Andersenaa0765e1999-10-22 04:30:20 +000070 file = fopen(filename,"r");
71 if (file == NULL) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +000072 /* bummer, /proc must not be mounted... */
Eric Andersenaa0765e1999-10-22 04:30:20 +000073 return( 0);
74 }
75 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
76 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +000077 return major*65536 + minor*256 + patch;
78}
79
80#endif
81
82
Eric Andersenc6cb79d1999-10-13 18:01:10 +000083
84#if defined (BB_CP) || defined (BB_MV)
85/*
86 * Return TRUE if a fileName is a directory.
87 * Nonexistant files return FALSE.
88 */
89int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +000090{
Eric Andersenc6cb79d1999-10-13 18:01:10 +000091 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +000092
Eric Andersenc6cb79d1999-10-13 18:01:10 +000093 if (stat(name, &statBuf) < 0)
94 return FALSE;
Eric Andersen9b587181999-10-17 05:43:39 +000095 if (S_ISDIR(statBuf.st_mode))
96 return TRUE;
97 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000098}
99
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000100
101/*
102 * Copy one file to another, while possibly preserving its modes, times,
103 * and modes. Returns TRUE if successful, or FALSE on a failure with an
104 * error message output. (Failure is not indicted if the attributes cannot
105 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000106 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000107 */
108int
Eric Andersen3c163821999-10-14 22:16:57 +0000109copyFile( const char *srcName, const char *destName,
110 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000111{
112 int rfd;
113 int wfd;
114 int rcc;
115 int result;
116 char buf[BUF_SIZE];
117 struct stat srcStatBuf;
118 struct stat dstStatBuf;
119 struct utimbuf times;
120
121 if (followLinks == FALSE)
122 result = stat(srcName, &srcStatBuf);
123 else
124 result = lstat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000125 if (result < 0) {
126 perror(srcName);
127 return FALSE;
128 }
129
130 if (followLinks == FALSE)
131 result = stat(destName, &dstStatBuf);
132 else
133 result = lstat(destName, &dstStatBuf);
134 if (result < 0) {
135 dstStatBuf.st_ino = -1;
136 dstStatBuf.st_dev = -1;
137 }
138
139 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
140 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
141 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
142 return FALSE;
143 }
144
145 if (S_ISDIR(srcStatBuf.st_mode)) {
146 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
147 /* Make sure the directory is writable */
148 if (mkdir(destName, 0777777 ^ umask(0))) {
149 perror(destName);
150 return (FALSE);
151 }
152 } else if (S_ISLNK(srcStatBuf.st_mode)) {
153 char *link_val;
154 int link_size;
155
156 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
157 link_val = (char *) alloca(PATH_MAX + 2);
158 link_size = readlink(srcName, link_val, PATH_MAX + 1);
159 if (link_size < 0) {
160 perror(srcName);
161 return (FALSE);
162 }
163 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000164 link_size = symlink(link_val, destName);
165 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000166 perror(destName);
167 return (FALSE);
168 }
169 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
170 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
171 if (mkfifo(destName, 644)) {
172 perror(destName);
173 return (FALSE);
174 }
175 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
176 || S_ISSOCK (srcStatBuf.st_mode)) {
177 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
178 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
179 perror(destName);
180 return (FALSE);
181 }
182 } else if (S_ISREG(srcStatBuf.st_mode)) {
183 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
184 rfd = open(srcName, O_RDONLY);
185 if (rfd < 0) {
186 perror(srcName);
187 return FALSE;
188 }
189
190 wfd = creat(destName, srcStatBuf.st_mode);
191 if (wfd < 0) {
192 perror(destName);
193 close(rfd);
194 return FALSE;
195 }
196
197 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
198 if (fullWrite(wfd, buf, rcc) < 0)
199 goto error_exit;
200 }
201 if (rcc < 0) {
202 goto error_exit;
203 }
204
205 close(rfd);
206 if (close(wfd) < 0) {
207 return FALSE;
208 }
209 }
210
211 if (setModes == TRUE) {
212 //fprintf(stderr, "Setting permissions for %s\n", destName);
213 chmod(destName, srcStatBuf.st_mode);
214 if (followLinks == TRUE)
215 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
216 else
217 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
218
219 times.actime = srcStatBuf.st_atime;
220 times.modtime = srcStatBuf.st_mtime;
221
222 utime(destName, &times);
223 }
224
225 return TRUE;
226
227
228 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000229 perror(destName);
230 close(rfd);
231 close(wfd);
232
233 return FALSE;
234}
Eric Andersencc8ed391999-10-05 16:24:54 +0000235#endif
236
237
238
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000239#if defined BB_TAR || defined BB_LS
240
241#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
242#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
243
244/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
245static const mode_t SBIT[] = {
246 0, 0, S_ISUID,
247 0, 0, S_ISGID,
248 0, 0, S_ISVTX
249};
250
251/* The 9 mode bits to test */
252static const mode_t MBIT[] = {
253 S_IRUSR, S_IWUSR, S_IXUSR,
254 S_IRGRP, S_IWGRP, S_IXGRP,
255 S_IROTH, S_IWOTH, S_IXOTH
256};
257
258#define MODE1 "rwxrwxrwx"
259#define MODE0 "---------"
260#define SMODE1 "..s..s..t"
261#define SMODE0 "..S..S..T"
262
Eric Andersencc8ed391999-10-05 16:24:54 +0000263/*
264 * Return the standard ls-like mode string from a file mode.
265 * This is static and so is overwritten on each call.
266 */
Eric Andersenf811e071999-10-09 00:25:00 +0000267const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000268{
Eric Andersenf811e071999-10-09 00:25:00 +0000269 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000270
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000271 int i;
272 buf[0] = TYPECHAR(mode);
273 for (i=0; i<9; i++) {
274 if (mode & SBIT[i])
275 buf[i+1] = (mode & MBIT[i])?
276 SMODE1[i] : SMODE0[i];
277 else
278 buf[i+1] = (mode & MBIT[i])?
279 MODE1[i] : MODE0[i];
280 }
Eric Andersenf811e071999-10-09 00:25:00 +0000281 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000282}
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000283#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000284
285
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000286#ifdef BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000287/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000288 * Return the standard ls-like time string from a time_t
289 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000290 */
Eric Andersenf811e071999-10-09 00:25:00 +0000291const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000292{
Eric Andersenf811e071999-10-09 00:25:00 +0000293 time_t now;
294 char *str;
295 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000296
Eric Andersenf811e071999-10-09 00:25:00 +0000297 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000298
Eric Andersenf811e071999-10-09 00:25:00 +0000299 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000300
Eric Andersenf811e071999-10-09 00:25:00 +0000301 strcpy(buf, &str[4]);
302 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000303
Eric Andersenf811e071999-10-09 00:25:00 +0000304 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
305 strcpy(&buf[7], &str[20]);
306 buf[11] = '\0';
307 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000308
Eric Andersenf811e071999-10-09 00:25:00 +0000309 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000310}
311
Eric Andersen17d49ef1999-10-06 20:25:32 +0000312/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000313 * Write all of the supplied buffer out to a file.
314 * This does multiple writes as necessary.
315 * Returns the amount written, or -1 on an error.
316 */
Eric Andersenf811e071999-10-09 00:25:00 +0000317int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000318{
Eric Andersenf811e071999-10-09 00:25:00 +0000319 int cc;
320 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000321
Eric Andersenf811e071999-10-09 00:25:00 +0000322 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000323
Eric Andersenf811e071999-10-09 00:25:00 +0000324 while (len > 0) {
325 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000326
Eric Andersenf811e071999-10-09 00:25:00 +0000327 if (cc < 0)
328 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000329
Eric Andersenf811e071999-10-09 00:25:00 +0000330 buf += cc;
331 total += cc;
332 len -= cc;
333 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000334
Eric Andersenf811e071999-10-09 00:25:00 +0000335 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000336}
337
338
339/*
340 * Read all of the supplied buffer from a file.
341 * This does multiple reads as necessary.
342 * Returns the amount read, or -1 on an error.
343 * A short read is returned on an end of file.
344 */
Eric Andersenf811e071999-10-09 00:25:00 +0000345int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000346{
Eric Andersenf811e071999-10-09 00:25:00 +0000347 int cc;
348 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000349
Eric Andersenf811e071999-10-09 00:25:00 +0000350 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000351
Eric Andersenf811e071999-10-09 00:25:00 +0000352 while (len > 0) {
353 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000354
Eric Andersenf811e071999-10-09 00:25:00 +0000355 if (cc < 0)
356 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000357
Eric Andersenf811e071999-10-09 00:25:00 +0000358 if (cc == 0)
359 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000360
Eric Andersenf811e071999-10-09 00:25:00 +0000361 buf += cc;
362 total += cc;
363 len -= cc;
364 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000365
Eric Andersenf811e071999-10-09 00:25:00 +0000366 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000367}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000368#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000369
370
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000371#if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
Eric Andersencc8ed391999-10-05 16:24:54 +0000372/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000373 * Walk down all the directories under the specified
374 * location, and do something (something specified
375 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000376 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000377 * Unfortunatly, while nftw(3) could replace this and reduce
378 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000379 * and so isn't sufficiently portable to take over since glibc2.1
380 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000381 */
382int
Eric Andersen63a0e531999-10-22 05:12:14 +0000383recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersen9b587181999-10-17 05:43:39 +0000384 int (*fileAction) (const char *fileName, struct stat* statbuf),
385 int (*dirAction) (const char *fileName, struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000386{
Eric Andersenf811e071999-10-09 00:25:00 +0000387 int status;
388 struct stat statbuf;
389 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000390
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000391 if (followLinks == TRUE)
Eric Andersenf811e071999-10-09 00:25:00 +0000392 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000393 else
394 status = lstat(fileName, &statbuf);
395
Eric Andersencc8ed391999-10-05 16:24:54 +0000396 if (status < 0) {
397 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000398 return (FALSE);
399 }
400
Eric Andersen50d63601999-11-09 01:47:36 +0000401 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
402 if (fileAction == NULL)
403 return (TRUE);
404 else
405 return (fileAction(fileName, &statbuf));
406 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000407
Eric Andersenf811e071999-10-09 00:25:00 +0000408 if (recurse == FALSE) {
409 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000410 if (dirAction != NULL)
Eric Andersen9b587181999-10-17 05:43:39 +0000411 return (dirAction(fileName, &statbuf));
Eric Andersenf811e071999-10-09 00:25:00 +0000412 else
Eric Andersen3c163821999-10-14 22:16:57 +0000413 return (TRUE);
414 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000415 }
416
Eric Andersen2b69c401999-10-05 22:58:32 +0000417 if (S_ISDIR(statbuf.st_mode)) {
418 DIR *dir;
419 dir = opendir(fileName);
420 if (!dir) {
421 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000422 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000423 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000424 if (dirAction != NULL && depthFirst == FALSE) {
Eric Andersen9b587181999-10-17 05:43:39 +0000425 status = dirAction(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000426 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000427 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000428 return (FALSE);
429 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000430 }
Eric Andersenf811e071999-10-09 00:25:00 +0000431 while ((next = readdir(dir)) != NULL) {
432 char nextFile[NAME_MAX];
433 if ((strcmp(next->d_name, "..") == 0)
434 || (strcmp(next->d_name, ".") == 0)) {
435 continue;
436 }
437 sprintf(nextFile, "%s/%s", fileName, next->d_name);
438 status =
Eric Andersen63a0e531999-10-22 05:12:14 +0000439 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Eric Andersenbed30e91999-10-18 19:02:32 +0000440 fileAction, dirAction);
Eric Andersenf811e071999-10-09 00:25:00 +0000441 if (status < 0) {
442 closedir(dir);
443 return (FALSE);
444 }
445 }
446 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000447 if (status < 0) {
448 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000449 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000450 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000451 if (dirAction != NULL && depthFirst == TRUE) {
Eric Andersenbed30e91999-10-18 19:02:32 +0000452 status = dirAction(fileName, &statbuf);
453 if (status == FALSE) {
454 perror(fileName);
455 return (FALSE);
456 }
457 }
Eric Andersenf811e071999-10-09 00:25:00 +0000458 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000459 if (fileAction == NULL)
460 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000461 else
Eric Andersen9b587181999-10-17 05:43:39 +0000462 return (fileAction(fileName, &statbuf));
Eric Andersencc8ed391999-10-05 16:24:54 +0000463 }
Eric Andersenf811e071999-10-09 00:25:00 +0000464 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000465}
466
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000467#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000468
Eric Andersenf6be9441999-10-13 21:12:06 +0000469
470
471#if defined (BB_TAR) || defined (BB_MKDIR)
472/*
473 * Attempt to create the directories along the specified path, except for
474 * the final component. The mode is given for the final directory only,
475 * while all previous ones get default protections. Errors are not reported
476 * here, as failures to restore files can be reported later.
477 */
478extern void createPath (const char *name, int mode)
479{
480 char *cp;
481 char *cpOld;
482 char buf[NAME_MAX];
483
484 strcpy (buf, name);
485
486 cp = strchr (buf, '/');
487
488 while (cp) {
489 cpOld = cp;
490 cp = strchr (cp + 1, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000491 *cpOld = '\0';
Eric Andersenb6a44b81999-11-13 04:47:09 +0000492 mkdir (buf, cp ? 0777 : mode);
Eric Andersenf6be9441999-10-13 21:12:06 +0000493 *cpOld = '/';
494 }
495}
496#endif
497
498
499
500#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000501/* [ugoa]{+|-|=}[rwxst] */
502
503
504
505extern int
506parse_mode( const char* s, mode_t* theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000507{
Eric Andersence8f3b91999-10-20 07:03:36 +0000508 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
509 mode_t orMode = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000510 mode_t mode = 0;
Eric Andersence8f3b91999-10-20 07:03:36 +0000511 mode_t groups = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000512 char type;
513 char c;
514
515 do {
516 for ( ; ; ) {
517 switch ( c = *s++ ) {
518 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000519 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000520 case 'u':
521 groups |= S_ISUID|S_IRWXU;
522 continue;
523 case 'g':
524 groups |= S_ISGID|S_IRWXG;
525 continue;
526 case 'o':
527 groups |= S_IRWXO;
528 continue;
529 case 'a':
530 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
531 continue;
532 case '+':
533 case '=':
534 case '-':
535 type = c;
Eric Andersence8f3b91999-10-20 07:03:36 +0000536 if ( groups == 0 ) /* The default is "all" */
Eric Andersenf6be9441999-10-13 21:12:06 +0000537 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
538 break;
539 default:
Eric Andersenfa0540f1999-10-22 18:18:31 +0000540 if ( isdigit(c) && c >= '0' && c <= '7' &&
541 mode == 0 && groups == 0 ) {
542 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000543 return (TRUE);
544 }
545 else
546 return (FALSE);
547 }
548 break;
549 }
550
551 while ( (c = *s++) != '\0' ) {
552 switch ( c ) {
553 case ',':
554 break;
555 case 'r':
556 mode |= S_IRUSR|S_IRGRP|S_IROTH;
557 continue;
558 case 'w':
559 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
560 continue;
561 case 'x':
562 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
563 continue;
564 case 's':
565 mode |= S_IXGRP|S_ISUID|S_ISGID;
566 continue;
567 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000568 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000569 continue;
570 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000571 *theMode &= andMode;
572 *theMode |= orMode;
573 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000574 }
575 break;
576 }
577 switch ( type ) {
578 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000579 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000580 /* fall through */
581 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000582 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000583 break;
584 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000585 andMode &= ~(mode & groups);
586 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000587 break;
588 }
589 } while ( c == ',' );
Eric Andersence8f3b91999-10-20 07:03:36 +0000590 *theMode &= andMode;
591 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000592 return (TRUE);
593}
Eric Andersence8f3b91999-10-20 07:03:36 +0000594
595
Eric Andersenf6be9441999-10-13 21:12:06 +0000596#endif
597
Eric Andersene674eb71999-10-19 20:52:57 +0000598
599
Eric Andersend23f9ba1999-10-20 19:18:15 +0000600
601
602
603
604#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
605
606/* Use this to avoid needing the glibc NSS stuff
607 * This uses storage buf to hold things.
608 * */
609uid_t
610my_getid(const char *filename, char *name, uid_t id)
611{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000612 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000613 char *rname, *start, *end, buf[128];
614 uid_t rid;
615
Eric Andersenaa0765e1999-10-22 04:30:20 +0000616 file=fopen(filename,"r");
617 if (file == NULL) {
618 perror(filename);
619 return (-1);
620 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000621
Eric Andersenaa0765e1999-10-22 04:30:20 +0000622 while (fgets (buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000623 if (buf[0] == '#')
624 continue;
625
626 start = buf;
627 end = strchr (start, ':');
628 if (end == NULL)
629 continue;
630 *end = '\0';
631 rname = start;
632
633 start = end + 1;
634 end = strchr (start, ':');
635 if (end == NULL)
636 continue;
637
638 start = end + 1;
639 rid = (uid_t) strtol (start, &end, 10);
640 if (end == start)
641 continue;
642
643 if (name) {
644 if (0 == strcmp(rname, name))
645 return( rid);
646 }
647 if ( id != -1 && id == rid ) {
648 strncpy(name, rname, 8);
649 return( TRUE);
650 }
651 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000652 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000653 return (-1);
654}
655
656uid_t
657my_getpwnam(char *name)
658{
659 return my_getid("/etc/passwd", name, -1);
660}
661
662gid_t
663my_getgrnam(char *name)
664{
665 return my_getid("/etc/group", name, -1);
666}
667
668void
669my_getpwuid(char* name, uid_t uid)
670{
671 my_getid("/etc/passwd", name, uid);
672}
673
674void
675my_getgrgid(char* group, gid_t gid)
676{
677 my_getid("/etc/group", group, gid);
678}
679
680
681#endif
682
683
Eric Andersenaa0765e1999-10-22 04:30:20 +0000684
Eric Andersen0460ff21999-10-25 23:32:44 +0000685
686#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
687
688
689#include <linux/kd.h>
690#include <sys/ioctl.h>
691
692int is_a_console(int fd)
693{
694 char arg;
695
696 arg = 0;
697 return (ioctl(fd, KDGKBTYPE, &arg) == 0
698 && ((arg == KB_101) || (arg == KB_84)));
699}
700
701static int open_a_console(char *fnam)
702{
703 int fd;
704
705 /* try read-only */
706 fd = open(fnam, O_RDWR);
707
708 /* if failed, try read-only */
709 if (fd < 0 && errno == EACCES)
710 fd = open(fnam, O_RDONLY);
711
712 /* if failed, try write-only */
713 if (fd < 0 && errno == EACCES)
714 fd = open(fnam, O_WRONLY);
715
716 /* if failed, fail */
717 if (fd < 0)
718 return -1;
719
720 /* if not a console, fail */
721 if (! is_a_console(fd))
722 {
723 close(fd);
724 return -1;
725 }
726
727 /* success */
728 return fd;
729}
730
731/*
732 * Get an fd for use with kbd/console ioctls.
733 * We try several things because opening /dev/console will fail
734 * if someone else used X (which does a chown on /dev/console).
735 *
736 * if tty_name is non-NULL, try this one instead.
737 */
738
739int get_console_fd(char* tty_name)
740{
741 int fd;
742
743 if (tty_name)
744 {
745 if (-1 == (fd = open_a_console(tty_name)))
746 return -1;
747 else
748 return fd;
749 }
750
751 fd = open_a_console("/dev/tty");
752 if (fd >= 0)
753 return fd;
754
755 fd = open_a_console("/dev/tty0");
756 if (fd >= 0)
757 return fd;
758
759 fd = open_a_console("/dev/console");
760 if (fd >= 0)
761 return fd;
762
763 for (fd = 0; fd < 3; fd++)
764 if (is_a_console(fd))
765 return fd;
766
767 fprintf(stderr,
768 "Couldnt get a file descriptor referring to the console\n");
769 return -1; /* total failure */
770}
771
772
773#endif
774
775
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000776#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000777
778/* Do a case insensitive strstr() */
779char* stristr(char *haystack, const char *needle)
780{
781 int len = strlen( needle );
782 while( *haystack ) {
783 if( !strncasecmp( haystack, needle, len ) )
784 break;
785 haystack++;
786 }
787
788 if( !(*haystack) )
789 haystack = NULL;
790
791 return haystack;
792}
793
Eric Andersenc1525e81999-10-29 00:07:31 +0000794/* This tries to find a needle in a haystack, but does so by
795 * only trying to match literal strings (look 'ma, no regexps!)
796 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000797 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000798 * -Erik Andersen
799 */
800extern int find_match(char *haystack, char *needle, int ignoreCase)
801{
802
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000803 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000804 haystack = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000805 else
806 haystack = stristr (haystack, needle);
807 if (haystack == NULL)
808 return FALSE;
809 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000810}
811
812
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000813/* This performs substitutions after a string match has been found. */
Eric Andersenc1525e81999-10-29 00:07:31 +0000814extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
815{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000816 int foundOne=0;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000817 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000818
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000819 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000820 where = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000821 else
822 where = stristr (haystack, needle);
823
824 if (strcmp(needle, newNeedle)==0)
825 return FALSE;
826
827 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
828 while(where!=NULL) {
829 foundOne++;
830 strcpy(oldhayStack, haystack);
831#if 0
832 if ( strlen(newNeedle) > strlen(needle)) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000833 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
834 strlen(needle) + strlen(newNeedle)));
Eric Andersenc1525e81999-10-29 00:07:31 +0000835 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000836#endif
837 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
838 *slider=0;
839 haystack=strcat(haystack, newNeedle);
840 slider1+=strlen(needle);
841 haystack = strcat(haystack, slider1);
842 where = strstr (slider, needle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000843 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000844 free( oldhayStack);
845
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000846 if (foundOne > 0)
Eric Andersenc1525e81999-10-29 00:07:31 +0000847 return TRUE;
848 else
849 return FALSE;
850}
851
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000852
Eric Andersenc1525e81999-10-29 00:07:31 +0000853#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000854
855
856
857
858#if defined BB_DF | defined BB_MTAB
859/*
860 * Given a block device, find the mount table entry if that block device
861 * is mounted.
862 *
863 * Given any other file (or directory), find the mount table entry for its
864 * filesystem.
865 */
866extern struct mntent *findMountPoint(const char *name, const char *table)
867{
868 struct stat s;
869 dev_t mountDevice;
870 FILE *mountTable;
871 struct mntent *mountEntry;
872
873 if (stat(name, &s) != 0)
874 return 0;
875
876 if ((s.st_mode & S_IFMT) == S_IFBLK)
877 mountDevice = s.st_rdev;
878 else
879 mountDevice = s.st_dev;
880
881
882 if ((mountTable = setmntent(table, "r")) == 0)
883 return 0;
884
885 while ((mountEntry = getmntent(mountTable)) != 0) {
886 if (strcmp(name, mountEntry->mnt_dir) == 0
887 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
888 break;
889 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
890 break;
891 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
892 break;
893 }
894 endmntent(mountTable);
895 return mountEntry;
896}
897
898#endif
899
900
901
902#if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
903extern void whine_if_fstab_is_missing()
904{
905 struct stat statBuf;
906 if (stat("/etc/fstab", &statBuf) < 0)
907 fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
908}
909#endif
910
911
Eric Andersencc8ed391999-10-05 16:24:54 +0000912/* END CODE */
Eric Andersenaa0765e1999-10-22 04:30:20 +0000913
Eric Andersen29d2e361999-11-06 06:07:27 +0000914