blob: eb24de2e691da08b583eacf2f13ddf786ac12f49 [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{
42 fprintf(stderr, "Usage: %s\n", usage);
43 exit(FALSE);
44}
45
46
Eric Andersend23f9ba1999-10-20 19:18:15 +000047#if defined (BB_INIT) || defined (BB_PS)
48
49/* Returns kernel version encoded as major*65536 + minor*256 + patch,
50 * so, for example, to check if the kernel is greater than 2.2.11:
51 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
52 */
53int
54get_kernel_revision()
55{
Eric Andersenaa0765e1999-10-22 04:30:20 +000056 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +000057 int major=0, minor=0, patch=0;
Eric Andersenaa0765e1999-10-22 04:30:20 +000058 char* filename="/proc/sys/kernel/osrelease";
Eric Andersend23f9ba1999-10-20 19:18:15 +000059
Eric Andersenaa0765e1999-10-22 04:30:20 +000060 file = fopen(filename,"r");
61 if (file == NULL) {
62 perror(filename);
63 return( 0);
64 }
65 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
66 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +000067 return major*65536 + minor*256 + patch;
68}
69
70#endif
71
72
Eric Andersenc6cb79d1999-10-13 18:01:10 +000073
74#if defined (BB_CP) || defined (BB_MV)
75/*
76 * Return TRUE if a fileName is a directory.
77 * Nonexistant files return FALSE.
78 */
79int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +000080{
Eric Andersenc6cb79d1999-10-13 18:01:10 +000081 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +000082
Eric Andersenc6cb79d1999-10-13 18:01:10 +000083 if (stat(name, &statBuf) < 0)
84 return FALSE;
Eric Andersen9b587181999-10-17 05:43:39 +000085 if (S_ISDIR(statBuf.st_mode))
86 return TRUE;
87 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000088}
89
Eric Andersenc6cb79d1999-10-13 18:01:10 +000090
91/*
92 * Copy one file to another, while possibly preserving its modes, times,
93 * and modes. Returns TRUE if successful, or FALSE on a failure with an
94 * error message output. (Failure is not indicted if the attributes cannot
95 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +000096 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +000097 */
98int
Eric Andersen3c163821999-10-14 22:16:57 +000099copyFile( const char *srcName, const char *destName,
100 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000101{
102 int rfd;
103 int wfd;
104 int rcc;
105 int result;
106 char buf[BUF_SIZE];
107 struct stat srcStatBuf;
108 struct stat dstStatBuf;
109 struct utimbuf times;
110
111 if (followLinks == FALSE)
112 result = stat(srcName, &srcStatBuf);
113 else
114 result = lstat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000115 if (result < 0) {
116 perror(srcName);
117 return FALSE;
118 }
119
120 if (followLinks == FALSE)
121 result = stat(destName, &dstStatBuf);
122 else
123 result = lstat(destName, &dstStatBuf);
124 if (result < 0) {
125 dstStatBuf.st_ino = -1;
126 dstStatBuf.st_dev = -1;
127 }
128
129 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
130 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
131 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
132 return FALSE;
133 }
134
135 if (S_ISDIR(srcStatBuf.st_mode)) {
136 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
137 /* Make sure the directory is writable */
138 if (mkdir(destName, 0777777 ^ umask(0))) {
139 perror(destName);
140 return (FALSE);
141 }
142 } else if (S_ISLNK(srcStatBuf.st_mode)) {
143 char *link_val;
144 int link_size;
145
146 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
147 link_val = (char *) alloca(PATH_MAX + 2);
148 link_size = readlink(srcName, link_val, PATH_MAX + 1);
149 if (link_size < 0) {
150 perror(srcName);
151 return (FALSE);
152 }
153 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000154 link_size = symlink(link_val, destName);
155 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000156 perror(destName);
157 return (FALSE);
158 }
159 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
160 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
161 if (mkfifo(destName, 644)) {
162 perror(destName);
163 return (FALSE);
164 }
165 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
166 || S_ISSOCK (srcStatBuf.st_mode)) {
167 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
168 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
169 perror(destName);
170 return (FALSE);
171 }
172 } else if (S_ISREG(srcStatBuf.st_mode)) {
173 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
174 rfd = open(srcName, O_RDONLY);
175 if (rfd < 0) {
176 perror(srcName);
177 return FALSE;
178 }
179
180 wfd = creat(destName, srcStatBuf.st_mode);
181 if (wfd < 0) {
182 perror(destName);
183 close(rfd);
184 return FALSE;
185 }
186
187 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
188 if (fullWrite(wfd, buf, rcc) < 0)
189 goto error_exit;
190 }
191 if (rcc < 0) {
192 goto error_exit;
193 }
194
195 close(rfd);
196 if (close(wfd) < 0) {
197 return FALSE;
198 }
199 }
200
201 if (setModes == TRUE) {
202 //fprintf(stderr, "Setting permissions for %s\n", destName);
203 chmod(destName, srcStatBuf.st_mode);
204 if (followLinks == TRUE)
205 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
206 else
207 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
208
209 times.actime = srcStatBuf.st_atime;
210 times.modtime = srcStatBuf.st_mtime;
211
212 utime(destName, &times);
213 }
214
215 return TRUE;
216
217
218 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000219 perror(destName);
220 close(rfd);
221 close(wfd);
222
223 return FALSE;
224}
Eric Andersencc8ed391999-10-05 16:24:54 +0000225#endif
226
227
228
Eric Andersenbed30e91999-10-18 19:02:32 +0000229#ifdef BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000230/*
231 * Return the standard ls-like mode string from a file mode.
232 * This is static and so is overwritten on each call.
233 */
Eric Andersenf811e071999-10-09 00:25:00 +0000234const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000235{
Eric Andersenf811e071999-10-09 00:25:00 +0000236 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000237
Eric Andersenf811e071999-10-09 00:25:00 +0000238 strcpy(buf, "----------");
Eric Andersencc8ed391999-10-05 16:24:54 +0000239
Eric Andersenf811e071999-10-09 00:25:00 +0000240 /*
241 * Fill in the file type.
242 */
243 if (S_ISDIR(mode))
244 buf[0] = 'd';
245 if (S_ISCHR(mode))
246 buf[0] = 'c';
247 if (S_ISBLK(mode))
248 buf[0] = 'b';
249 if (S_ISFIFO(mode))
250 buf[0] = 'p';
Eric Andersenf811e071999-10-09 00:25:00 +0000251 if (S_ISLNK(mode))
252 buf[0] = 'l';
Eric Andersenf811e071999-10-09 00:25:00 +0000253 if (S_ISSOCK(mode))
254 buf[0] = 's';
Eric Andersenf811e071999-10-09 00:25:00 +0000255 /*
256 * Now fill in the normal file permissions.
257 */
258 if (mode & S_IRUSR)
259 buf[1] = 'r';
260 if (mode & S_IWUSR)
261 buf[2] = 'w';
262 if (mode & S_IXUSR)
263 buf[3] = 'x';
264 if (mode & S_IRGRP)
265 buf[4] = 'r';
266 if (mode & S_IWGRP)
267 buf[5] = 'w';
268 if (mode & S_IXGRP)
269 buf[6] = 'x';
270 if (mode & S_IROTH)
271 buf[7] = 'r';
272 if (mode & S_IWOTH)
273 buf[8] = 'w';
274 if (mode & S_IXOTH)
275 buf[9] = 'x';
Eric Andersencc8ed391999-10-05 16:24:54 +0000276
Eric Andersenf811e071999-10-09 00:25:00 +0000277 /*
278 * Finally fill in magic stuff like suid and sticky text.
279 */
280 if (mode & S_ISUID)
281 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
282 if (mode & S_ISGID)
283 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
284 if (mode & S_ISVTX)
285 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
Eric Andersencc8ed391999-10-05 16:24:54 +0000286
Eric Andersenf811e071999-10-09 00:25:00 +0000287 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000288}
289
290
Eric Andersencc8ed391999-10-05 16:24:54 +0000291/*
Eric Andersen17d49ef1999-10-06 20:25:32 +0000292 * Get the time string to be used for a file.
293 * This is down to the minute for new files, but only the date for old files.
294 * The string is returned from a static buffer, and so is overwritten for
295 * each call.
296 */
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
318
319/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000320 * Write all of the supplied buffer out to a file.
321 * This does multiple writes as necessary.
322 * Returns the amount written, or -1 on an error.
323 */
Eric Andersenf811e071999-10-09 00:25:00 +0000324int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000325{
Eric Andersenf811e071999-10-09 00:25:00 +0000326 int cc;
327 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000328
Eric Andersenf811e071999-10-09 00:25:00 +0000329 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000330
Eric Andersenf811e071999-10-09 00:25:00 +0000331 while (len > 0) {
332 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000333
Eric Andersenf811e071999-10-09 00:25:00 +0000334 if (cc < 0)
335 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000336
Eric Andersenf811e071999-10-09 00:25:00 +0000337 buf += cc;
338 total += cc;
339 len -= cc;
340 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000341
Eric Andersenf811e071999-10-09 00:25:00 +0000342 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000343}
344
345
346/*
347 * Read all of the supplied buffer from a file.
348 * This does multiple reads as necessary.
349 * Returns the amount read, or -1 on an error.
350 * A short read is returned on an end of file.
351 */
Eric Andersenf811e071999-10-09 00:25:00 +0000352int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000353{
Eric Andersenf811e071999-10-09 00:25:00 +0000354 int cc;
355 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000356
Eric Andersenf811e071999-10-09 00:25:00 +0000357 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000358
Eric Andersenf811e071999-10-09 00:25:00 +0000359 while (len > 0) {
360 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000361
Eric Andersenf811e071999-10-09 00:25:00 +0000362 if (cc < 0)
363 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000364
Eric Andersenf811e071999-10-09 00:25:00 +0000365 if (cc == 0)
366 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000367
Eric Andersenf811e071999-10-09 00:25:00 +0000368 buf += cc;
369 total += cc;
370 len -= cc;
371 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000372
Eric Andersenf811e071999-10-09 00:25:00 +0000373 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000374}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000375#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000376
377
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000378#if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
Eric Andersencc8ed391999-10-05 16:24:54 +0000379/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000380 * Walk down all the directories under the specified
381 * location, and do something (something specified
382 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000383 *
384 * TODO: check if ftw(3) can replace this to reduce code size...
Eric Andersencc8ed391999-10-05 16:24:54 +0000385 */
386int
Eric Andersenbed30e91999-10-18 19:02:32 +0000387recursiveAction(const char *fileName, int recurse, int followLinks, int delayDirAction,
Eric Andersen9b587181999-10-17 05:43:39 +0000388 int (*fileAction) (const char *fileName, struct stat* statbuf),
389 int (*dirAction) (const char *fileName, struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000390{
Eric Andersenf811e071999-10-09 00:25:00 +0000391 int status;
392 struct stat statbuf;
393 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000394
Eric Andersen3c163821999-10-14 22:16:57 +0000395 if (followLinks == FALSE)
Eric Andersenf811e071999-10-09 00:25:00 +0000396 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000397 else
398 status = lstat(fileName, &statbuf);
399
Eric Andersencc8ed391999-10-05 16:24:54 +0000400 if (status < 0) {
401 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000402 return (FALSE);
403 }
404
405 if (recurse == FALSE) {
406 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000407 if (dirAction != NULL)
Eric Andersen9b587181999-10-17 05:43:39 +0000408 return (dirAction(fileName, &statbuf));
Eric Andersenf811e071999-10-09 00:25:00 +0000409 else
Eric Andersen3c163821999-10-14 22:16:57 +0000410 return (TRUE);
411 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000412 }
413
Eric Andersen2b69c401999-10-05 22:58:32 +0000414 if (S_ISDIR(statbuf.st_mode)) {
415 DIR *dir;
416 dir = opendir(fileName);
417 if (!dir) {
418 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000419 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000420 }
Eric Andersenbed30e91999-10-18 19:02:32 +0000421 if (dirAction != NULL && delayDirAction == FALSE) {
Eric Andersen9b587181999-10-17 05:43:39 +0000422 status = dirAction(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000423 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000424 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000425 return (FALSE);
426 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000427 }
Eric Andersenf811e071999-10-09 00:25:00 +0000428 while ((next = readdir(dir)) != NULL) {
429 char nextFile[NAME_MAX];
430 if ((strcmp(next->d_name, "..") == 0)
431 || (strcmp(next->d_name, ".") == 0)) {
432 continue;
433 }
434 sprintf(nextFile, "%s/%s", fileName, next->d_name);
435 status =
Eric Andersenbed30e91999-10-18 19:02:32 +0000436 recursiveAction(nextFile, TRUE, followLinks, delayDirAction,
437 fileAction, dirAction);
Eric Andersenf811e071999-10-09 00:25:00 +0000438 if (status < 0) {
439 closedir(dir);
440 return (FALSE);
441 }
442 }
443 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000444 if (status < 0) {
445 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000446 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000447 }
Eric Andersenbed30e91999-10-18 19:02:32 +0000448 if (dirAction != NULL && delayDirAction == TRUE) {
449 status = dirAction(fileName, &statbuf);
450 if (status == FALSE) {
451 perror(fileName);
452 return (FALSE);
453 }
454 }
Eric Andersenf811e071999-10-09 00:25:00 +0000455 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000456 if (fileAction == NULL)
457 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000458 else
Eric Andersen9b587181999-10-17 05:43:39 +0000459 return (fileAction(fileName, &statbuf));
Eric Andersencc8ed391999-10-05 16:24:54 +0000460 }
Eric Andersenf811e071999-10-09 00:25:00 +0000461 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000462}
463
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000464#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000465
Eric Andersenf6be9441999-10-13 21:12:06 +0000466
467
468#if defined (BB_TAR) || defined (BB_MKDIR)
469/*
470 * Attempt to create the directories along the specified path, except for
471 * the final component. The mode is given for the final directory only,
472 * while all previous ones get default protections. Errors are not reported
473 * here, as failures to restore files can be reported later.
474 */
475extern void createPath (const char *name, int mode)
476{
477 char *cp;
478 char *cpOld;
479 char buf[NAME_MAX];
480
481 strcpy (buf, name);
482
483 cp = strchr (buf, '/');
484
485 while (cp) {
486 cpOld = cp;
487 cp = strchr (cp + 1, '/');
488
489 *cpOld = '\0';
490
491 if (mkdir (buf, cp ? 0777 : mode) == 0)
492 printf ("Directory \"%s\" created\n", buf);
493
494 *cpOld = '/';
495 }
496}
497#endif
498
499
500
501#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000502/* [ugoa]{+|-|=}[rwxst] */
503
504
505
506extern int
507parse_mode( const char* s, mode_t* theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000508{
Eric Andersence8f3b91999-10-20 07:03:36 +0000509 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
510 mode_t orMode = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000511 mode_t mode = 0;
Eric Andersence8f3b91999-10-20 07:03:36 +0000512 mode_t groups = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000513 char type;
514 char c;
515
516 do {
517 for ( ; ; ) {
518 switch ( c = *s++ ) {
519 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000520 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000521 case 'u':
522 groups |= S_ISUID|S_IRWXU;
523 continue;
524 case 'g':
525 groups |= S_ISGID|S_IRWXG;
526 continue;
527 case 'o':
528 groups |= S_IRWXO;
529 continue;
530 case 'a':
531 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
532 continue;
533 case '+':
534 case '=':
535 case '-':
536 type = c;
Eric Andersence8f3b91999-10-20 07:03:36 +0000537 if ( groups == 0 ) /* The default is "all" */
Eric Andersenf6be9441999-10-13 21:12:06 +0000538 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
539 break;
540 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000541 if ( isdigit(c) && c >= '0' && c <= '7' && mode == 0 && groups == 0 ) {
542 andMode = 0;
543 orMode = strtol(--s, NULL, 8);
544 *theMode &= andMode;
545 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000546 return (TRUE);
547 }
548 else
549 return (FALSE);
550 }
551 break;
552 }
553
554 while ( (c = *s++) != '\0' ) {
555 switch ( c ) {
556 case ',':
557 break;
558 case 'r':
559 mode |= S_IRUSR|S_IRGRP|S_IROTH;
560 continue;
561 case 'w':
562 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
563 continue;
564 case 'x':
565 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
566 continue;
567 case 's':
568 mode |= S_IXGRP|S_ISUID|S_ISGID;
569 continue;
570 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000571 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000572 continue;
573 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000574 *theMode &= andMode;
575 *theMode |= orMode;
576 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000577 }
578 break;
579 }
580 switch ( type ) {
581 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000582 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000583 /* fall through */
584 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000585 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000586 break;
587 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000588 andMode &= ~(mode & groups);
589 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000590 break;
591 }
592 } while ( c == ',' );
Eric Andersence8f3b91999-10-20 07:03:36 +0000593 *theMode &= andMode;
594 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000595 return (TRUE);
596}
Eric Andersence8f3b91999-10-20 07:03:36 +0000597
598
Eric Andersenf6be9441999-10-13 21:12:06 +0000599#endif
600
Eric Andersene674eb71999-10-19 20:52:57 +0000601
602
Eric Andersend23f9ba1999-10-20 19:18:15 +0000603
604
605
606
607#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
608
609/* Use this to avoid needing the glibc NSS stuff
610 * This uses storage buf to hold things.
611 * */
612uid_t
613my_getid(const char *filename, char *name, uid_t id)
614{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000615 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000616 char *rname, *start, *end, buf[128];
617 uid_t rid;
618
Eric Andersenaa0765e1999-10-22 04:30:20 +0000619 file=fopen(filename,"r");
620 if (file == NULL) {
621 perror(filename);
622 return (-1);
623 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000624
Eric Andersenaa0765e1999-10-22 04:30:20 +0000625 while (fgets (buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000626 if (buf[0] == '#')
627 continue;
628
629 start = buf;
630 end = strchr (start, ':');
631 if (end == NULL)
632 continue;
633 *end = '\0';
634 rname = start;
635
636 start = end + 1;
637 end = strchr (start, ':');
638 if (end == NULL)
639 continue;
640
641 start = end + 1;
642 rid = (uid_t) strtol (start, &end, 10);
643 if (end == start)
644 continue;
645
646 if (name) {
647 if (0 == strcmp(rname, name))
648 return( rid);
649 }
650 if ( id != -1 && id == rid ) {
651 strncpy(name, rname, 8);
652 return( TRUE);
653 }
654 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000655 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000656 return (-1);
657}
658
659uid_t
660my_getpwnam(char *name)
661{
662 return my_getid("/etc/passwd", name, -1);
663}
664
665gid_t
666my_getgrnam(char *name)
667{
668 return my_getid("/etc/group", name, -1);
669}
670
671void
672my_getpwuid(char* name, uid_t uid)
673{
674 my_getid("/etc/passwd", name, uid);
675}
676
677void
678my_getgrgid(char* group, gid_t gid)
679{
680 my_getid("/etc/group", group, gid);
681}
682
683
684#endif
685
686
Eric Andersenaa0765e1999-10-22 04:30:20 +0000687
688#if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND )
689/* This tries to find a needle in a haystack, but does so by
690 * only trying to match literal strings (look 'ma, no regexps!)
691 * This is short, sweet, and carries _very_ little baggage,
692 * unlike its beefier cousin a few lines down...
693 * -Erik Andersen
694 */
695extern int find_match(char *haystack, char *needle, int ignoreCase)
696{
697
698 if (ignoreCase == FALSE) {
699 haystack = strstr (haystack, needle);
700 if (haystack == NULL)
701 return FALSE;
702 return TRUE;
703 } else {
704 int i;
705 char needle1[BUF_SIZE];
706 char haystack1[BUF_SIZE];
707
708 strncpy( haystack1, haystack, sizeof(haystack1));
709 strncpy( needle1, needle, sizeof(needle1));
710 for( i=0; i<sizeof(haystack1) && haystack1[i]; i++)
711 haystack1[i]=tolower( haystack1[i]);
712 for( i=0; i<sizeof(needle1) && needle1[i]; i++)
713 needle1[i]=tolower( needle1[i]);
714 haystack = strstr (haystack1, needle1);
715 if (haystack == NULL)
716 return FALSE;
717 return TRUE;
718 }
719}
720#endif
721
Eric Andersencc8ed391999-10-05 16:24:54 +0000722/* END CODE */
Eric Andersenaa0765e1999-10-22 04:30:20 +0000723