blob: 26568320fcac1874f177708ae71a2e6ec11139b5 [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 Andersenb0e9a701999-10-18 22:28:26 +000039/* volatile so gcc knows this is the enod of the line */
40volatile void usage(const char *usage)
41{
Eric Andersena07f0b01999-10-22 19:49:09 +000042 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
Eric Andersenb0e9a701999-10-18 22:28:26 +000043 fprintf(stderr, "Usage: %s\n", usage);
44 exit(FALSE);
45}
46
47
Eric Andersend23f9ba1999-10-20 19:18:15 +000048#if defined (BB_INIT) || defined (BB_PS)
49
50/* Returns kernel version encoded as major*65536 + minor*256 + patch,
51 * so, for example, to check if the kernel is greater than 2.2.11:
52 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
53 */
54int
55get_kernel_revision()
56{
Eric Andersenaa0765e1999-10-22 04:30:20 +000057 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +000058 int major=0, minor=0, patch=0;
Eric Andersenaa0765e1999-10-22 04:30:20 +000059 char* filename="/proc/sys/kernel/osrelease";
Eric Andersend23f9ba1999-10-20 19:18:15 +000060
Eric Andersenaa0765e1999-10-22 04:30:20 +000061 file = fopen(filename,"r");
62 if (file == NULL) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +000063 /* bummer, /proc must not be mounted... */
Eric Andersenaa0765e1999-10-22 04:30:20 +000064 return( 0);
65 }
66 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
67 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +000068 return major*65536 + minor*256 + patch;
69}
70
71#endif
72
73
Eric Andersenc6cb79d1999-10-13 18:01:10 +000074
75#if defined (BB_CP) || defined (BB_MV)
76/*
77 * Return TRUE if a fileName is a directory.
78 * Nonexistant files return FALSE.
79 */
80int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +000081{
Eric Andersenc6cb79d1999-10-13 18:01:10 +000082 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +000083
Eric Andersenc6cb79d1999-10-13 18:01:10 +000084 if (stat(name, &statBuf) < 0)
85 return FALSE;
Eric Andersen9b587181999-10-17 05:43:39 +000086 if (S_ISDIR(statBuf.st_mode))
87 return TRUE;
88 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000089}
90
Eric Andersenc6cb79d1999-10-13 18:01:10 +000091
92/*
93 * Copy one file to another, while possibly preserving its modes, times,
94 * and modes. Returns TRUE if successful, or FALSE on a failure with an
95 * error message output. (Failure is not indicted if the attributes cannot
96 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +000097 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +000098 */
99int
Eric Andersen3c163821999-10-14 22:16:57 +0000100copyFile( const char *srcName, const char *destName,
101 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000102{
103 int rfd;
104 int wfd;
105 int rcc;
106 int result;
107 char buf[BUF_SIZE];
108 struct stat srcStatBuf;
109 struct stat dstStatBuf;
110 struct utimbuf times;
111
112 if (followLinks == FALSE)
113 result = stat(srcName, &srcStatBuf);
114 else
115 result = lstat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000116 if (result < 0) {
117 perror(srcName);
118 return FALSE;
119 }
120
121 if (followLinks == FALSE)
122 result = stat(destName, &dstStatBuf);
123 else
124 result = lstat(destName, &dstStatBuf);
125 if (result < 0) {
126 dstStatBuf.st_ino = -1;
127 dstStatBuf.st_dev = -1;
128 }
129
130 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
131 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
132 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
133 return FALSE;
134 }
135
136 if (S_ISDIR(srcStatBuf.st_mode)) {
137 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
138 /* Make sure the directory is writable */
139 if (mkdir(destName, 0777777 ^ umask(0))) {
140 perror(destName);
141 return (FALSE);
142 }
143 } else if (S_ISLNK(srcStatBuf.st_mode)) {
144 char *link_val;
145 int link_size;
146
147 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
148 link_val = (char *) alloca(PATH_MAX + 2);
149 link_size = readlink(srcName, link_val, PATH_MAX + 1);
150 if (link_size < 0) {
151 perror(srcName);
152 return (FALSE);
153 }
154 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000155 link_size = symlink(link_val, destName);
156 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000157 perror(destName);
158 return (FALSE);
159 }
160 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
161 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
162 if (mkfifo(destName, 644)) {
163 perror(destName);
164 return (FALSE);
165 }
166 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
167 || S_ISSOCK (srcStatBuf.st_mode)) {
168 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
169 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
170 perror(destName);
171 return (FALSE);
172 }
173 } else if (S_ISREG(srcStatBuf.st_mode)) {
174 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
175 rfd = open(srcName, O_RDONLY);
176 if (rfd < 0) {
177 perror(srcName);
178 return FALSE;
179 }
180
181 wfd = creat(destName, srcStatBuf.st_mode);
182 if (wfd < 0) {
183 perror(destName);
184 close(rfd);
185 return FALSE;
186 }
187
188 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
189 if (fullWrite(wfd, buf, rcc) < 0)
190 goto error_exit;
191 }
192 if (rcc < 0) {
193 goto error_exit;
194 }
195
196 close(rfd);
197 if (close(wfd) < 0) {
198 return FALSE;
199 }
200 }
201
202 if (setModes == TRUE) {
203 //fprintf(stderr, "Setting permissions for %s\n", destName);
204 chmod(destName, srcStatBuf.st_mode);
205 if (followLinks == TRUE)
206 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
207 else
208 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
209
210 times.actime = srcStatBuf.st_atime;
211 times.modtime = srcStatBuf.st_mtime;
212
213 utime(destName, &times);
214 }
215
216 return TRUE;
217
218
219 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000220 perror(destName);
221 close(rfd);
222 close(wfd);
223
224 return FALSE;
225}
Eric Andersencc8ed391999-10-05 16:24:54 +0000226#endif
227
228
229
Eric Andersenbed30e91999-10-18 19:02:32 +0000230#ifdef BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000231/*
232 * Return the standard ls-like mode string from a file mode.
233 * This is static and so is overwritten on each call.
234 */
Eric Andersenf811e071999-10-09 00:25:00 +0000235const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000236{
Eric Andersenf811e071999-10-09 00:25:00 +0000237 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
Eric Andersenf811e071999-10-09 00:25:00 +0000239 strcpy(buf, "----------");
Eric Andersencc8ed391999-10-05 16:24:54 +0000240
Eric Andersenf811e071999-10-09 00:25:00 +0000241 /*
242 * Fill in the file type.
243 */
244 if (S_ISDIR(mode))
245 buf[0] = 'd';
246 if (S_ISCHR(mode))
247 buf[0] = 'c';
248 if (S_ISBLK(mode))
249 buf[0] = 'b';
250 if (S_ISFIFO(mode))
251 buf[0] = 'p';
Eric Andersenf811e071999-10-09 00:25:00 +0000252 if (S_ISLNK(mode))
253 buf[0] = 'l';
Eric Andersenf811e071999-10-09 00:25:00 +0000254 if (S_ISSOCK(mode))
255 buf[0] = 's';
Eric Andersenf811e071999-10-09 00:25:00 +0000256 /*
257 * Now fill in the normal file permissions.
258 */
259 if (mode & S_IRUSR)
260 buf[1] = 'r';
261 if (mode & S_IWUSR)
262 buf[2] = 'w';
263 if (mode & S_IXUSR)
264 buf[3] = 'x';
265 if (mode & S_IRGRP)
266 buf[4] = 'r';
267 if (mode & S_IWGRP)
268 buf[5] = 'w';
269 if (mode & S_IXGRP)
270 buf[6] = 'x';
271 if (mode & S_IROTH)
272 buf[7] = 'r';
273 if (mode & S_IWOTH)
274 buf[8] = 'w';
275 if (mode & S_IXOTH)
276 buf[9] = 'x';
Eric Andersencc8ed391999-10-05 16:24:54 +0000277
Eric Andersenf811e071999-10-09 00:25:00 +0000278 /*
279 * Finally fill in magic stuff like suid and sticky text.
280 */
281 if (mode & S_ISUID)
282 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
283 if (mode & S_ISGID)
284 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
285 if (mode & S_ISVTX)
286 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
Eric Andersencc8ed391999-10-05 16:24:54 +0000287
Eric Andersenf811e071999-10-09 00:25:00 +0000288 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000289}
290
291
Eric Andersencc8ed391999-10-05 16:24:54 +0000292/*
Eric Andersen17d49ef1999-10-06 20:25:32 +0000293 * Get the time string to be used for a file.
294 * This is down to the minute for new files, but only the date for old files.
295 * The string is returned from a static buffer, and so is overwritten for
296 * each call.
297 */
Eric Andersenf811e071999-10-09 00:25:00 +0000298const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000299{
Eric Andersenf811e071999-10-09 00:25:00 +0000300 time_t now;
301 char *str;
302 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000303
Eric Andersenf811e071999-10-09 00:25:00 +0000304 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000305
Eric Andersenf811e071999-10-09 00:25:00 +0000306 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000307
Eric Andersenf811e071999-10-09 00:25:00 +0000308 strcpy(buf, &str[4]);
309 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000310
Eric Andersenf811e071999-10-09 00:25:00 +0000311 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
312 strcpy(&buf[7], &str[20]);
313 buf[11] = '\0';
314 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000315
Eric Andersenf811e071999-10-09 00:25:00 +0000316 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000317}
318
319
320/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000321 * Write all of the supplied buffer out to a file.
322 * This does multiple writes as necessary.
323 * Returns the amount written, or -1 on an error.
324 */
Eric Andersenf811e071999-10-09 00:25:00 +0000325int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000326{
Eric Andersenf811e071999-10-09 00:25:00 +0000327 int cc;
328 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000329
Eric Andersenf811e071999-10-09 00:25:00 +0000330 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000331
Eric Andersenf811e071999-10-09 00:25:00 +0000332 while (len > 0) {
333 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000334
Eric Andersenf811e071999-10-09 00:25:00 +0000335 if (cc < 0)
336 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000337
Eric Andersenf811e071999-10-09 00:25:00 +0000338 buf += cc;
339 total += cc;
340 len -= cc;
341 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000342
Eric Andersenf811e071999-10-09 00:25:00 +0000343 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000344}
345
346
347/*
348 * Read all of the supplied buffer from a file.
349 * This does multiple reads as necessary.
350 * Returns the amount read, or -1 on an error.
351 * A short read is returned on an end of file.
352 */
Eric Andersenf811e071999-10-09 00:25:00 +0000353int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000354{
Eric Andersenf811e071999-10-09 00:25:00 +0000355 int cc;
356 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000357
Eric Andersenf811e071999-10-09 00:25:00 +0000358 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000359
Eric Andersenf811e071999-10-09 00:25:00 +0000360 while (len > 0) {
361 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000362
Eric Andersenf811e071999-10-09 00:25:00 +0000363 if (cc < 0)
364 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000365
Eric Andersenf811e071999-10-09 00:25:00 +0000366 if (cc == 0)
367 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000368
Eric Andersenf811e071999-10-09 00:25:00 +0000369 buf += cc;
370 total += cc;
371 len -= cc;
372 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000373
Eric Andersenf811e071999-10-09 00:25:00 +0000374 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000375}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000376#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000377
378
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000379#if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
Eric Andersencc8ed391999-10-05 16:24:54 +0000380/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000381 * Walk down all the directories under the specified
382 * location, and do something (something specified
383 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000384 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000385 * Unfortunatly, while nftw(3) could replace this and reduce
386 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
387 * and so isn't sufficiently portable to take over...
Eric Andersencc8ed391999-10-05 16:24:54 +0000388 */
389int
Eric Andersen63a0e531999-10-22 05:12:14 +0000390recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersen9b587181999-10-17 05:43:39 +0000391 int (*fileAction) (const char *fileName, struct stat* statbuf),
392 int (*dirAction) (const char *fileName, struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000393{
Eric Andersenf811e071999-10-09 00:25:00 +0000394 int status;
395 struct stat statbuf;
396 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000397
Eric Andersen3c163821999-10-14 22:16:57 +0000398 if (followLinks == FALSE)
Eric Andersenf811e071999-10-09 00:25:00 +0000399 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000400 else
401 status = lstat(fileName, &statbuf);
402
Eric Andersencc8ed391999-10-05 16:24:54 +0000403 if (status < 0) {
404 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000405 return (FALSE);
406 }
407
408 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, '/');
491
492 *cpOld = '\0';
493
494 if (mkdir (buf, cp ? 0777 : mode) == 0)
495 printf ("Directory \"%s\" created\n", buf);
496
497 *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) {
648 if (0 == strcmp(rname, name))
649 return( rid);
650 }
651 if ( id != -1 && id == rid ) {
652 strncpy(name, rname, 8);
653 return( TRUE);
654 }
655 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000656 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000657 return (-1);
658}
659
660uid_t
661my_getpwnam(char *name)
662{
663 return my_getid("/etc/passwd", name, -1);
664}
665
666gid_t
667my_getgrnam(char *name)
668{
669 return my_getid("/etc/group", name, -1);
670}
671
672void
673my_getpwuid(char* name, uid_t uid)
674{
675 my_getid("/etc/passwd", name, uid);
676}
677
678void
679my_getgrgid(char* group, gid_t gid)
680{
681 my_getid("/etc/group", group, gid);
682}
683
684
685#endif
686
687
Eric Andersenaa0765e1999-10-22 04:30:20 +0000688
Eric Andersen0460ff21999-10-25 23:32:44 +0000689
690#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
691
692
693#include <linux/kd.h>
694#include <sys/ioctl.h>
695
696int is_a_console(int fd)
697{
698 char arg;
699
700 arg = 0;
701 return (ioctl(fd, KDGKBTYPE, &arg) == 0
702 && ((arg == KB_101) || (arg == KB_84)));
703}
704
705static int open_a_console(char *fnam)
706{
707 int fd;
708
709 /* try read-only */
710 fd = open(fnam, O_RDWR);
711
712 /* if failed, try read-only */
713 if (fd < 0 && errno == EACCES)
714 fd = open(fnam, O_RDONLY);
715
716 /* if failed, try write-only */
717 if (fd < 0 && errno == EACCES)
718 fd = open(fnam, O_WRONLY);
719
720 /* if failed, fail */
721 if (fd < 0)
722 return -1;
723
724 /* if not a console, fail */
725 if (! is_a_console(fd))
726 {
727 close(fd);
728 return -1;
729 }
730
731 /* success */
732 return fd;
733}
734
735/*
736 * Get an fd for use with kbd/console ioctls.
737 * We try several things because opening /dev/console will fail
738 * if someone else used X (which does a chown on /dev/console).
739 *
740 * if tty_name is non-NULL, try this one instead.
741 */
742
743int get_console_fd(char* tty_name)
744{
745 int fd;
746
747 if (tty_name)
748 {
749 if (-1 == (fd = open_a_console(tty_name)))
750 return -1;
751 else
752 return fd;
753 }
754
755 fd = open_a_console("/dev/tty");
756 if (fd >= 0)
757 return fd;
758
759 fd = open_a_console("/dev/tty0");
760 if (fd >= 0)
761 return fd;
762
763 fd = open_a_console("/dev/console");
764 if (fd >= 0)
765 return fd;
766
767 for (fd = 0; fd < 3; fd++)
768 if (is_a_console(fd))
769 return fd;
770
771 fprintf(stderr,
772 "Couldnt get a file descriptor referring to the console\n");
773 return -1; /* total failure */
774}
775
776
777#endif
778
779
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000780#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000781
782/* Do a case insensitive strstr() */
783char* stristr(char *haystack, const char *needle)
784{
785 int len = strlen( needle );
786 while( *haystack ) {
787 if( !strncasecmp( haystack, needle, len ) )
788 break;
789 haystack++;
790 }
791
792 if( !(*haystack) )
793 haystack = NULL;
794
795 return haystack;
796}
797
Eric Andersenc1525e81999-10-29 00:07:31 +0000798/* This tries to find a needle in a haystack, but does so by
799 * only trying to match literal strings (look 'ma, no regexps!)
800 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000801 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000802 * -Erik Andersen
803 */
804extern int find_match(char *haystack, char *needle, int ignoreCase)
805{
806
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000807 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000808 haystack = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000809 else
810 haystack = stristr (haystack, needle);
811 if (haystack == NULL)
812 return FALSE;
813 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000814}
815
816
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000817/* This performs substitutions after a string match has been found. */
Eric Andersenc1525e81999-10-29 00:07:31 +0000818extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
819{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000820 int foundOne=0;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000821 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000822
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000823 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000824 where = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000825 else
826 where = stristr (haystack, needle);
827
828 if (strcmp(needle, newNeedle)==0)
829 return FALSE;
830
831 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
832 while(where!=NULL) {
833 foundOne++;
834 strcpy(oldhayStack, haystack);
835#if 0
836 if ( strlen(newNeedle) > strlen(needle)) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000837 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
838 strlen(needle) + strlen(newNeedle)));
Eric Andersenc1525e81999-10-29 00:07:31 +0000839 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000840#endif
841 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
842 *slider=0;
843 haystack=strcat(haystack, newNeedle);
844 slider1+=strlen(needle);
845 haystack = strcat(haystack, slider1);
846 where = strstr (slider, needle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000847 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000848 free( oldhayStack);
849
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000850 if (foundOne > 0)
Eric Andersenc1525e81999-10-29 00:07:31 +0000851 return TRUE;
852 else
853 return FALSE;
854}
855
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000856
Eric Andersenc1525e81999-10-29 00:07:31 +0000857#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000858/* END CODE */
Eric Andersenaa0765e1999-10-22 04:30:20 +0000859