blob: cf90f37d17cb178b04657969b44a86fab63472f3 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
2 * Utility routines.
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
21 * Permission has been granted to redistribute this code under the GPL.
22 *
23 */
24
Eric Andersen2b69c401999-10-05 22:58:32 +000025#include "internal.h"
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000030#include <dirent.h>
31#include <time.h>
32#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000033#include <sys/stat.h>
34#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersenc6cb79d1999-10-13 18:01:10 +000037
38#if defined (BB_CP) || defined (BB_MV)
39/*
40 * Return TRUE if a fileName is a directory.
41 * Nonexistant files return FALSE.
42 */
43int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +000044{
Eric Andersenc6cb79d1999-10-13 18:01:10 +000045 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Eric Andersenc6cb79d1999-10-13 18:01:10 +000047 if (stat(name, &statBuf) < 0)
48 return FALSE;
49
50 return S_ISDIR(statBuf.st_mode);
Eric Andersencc8ed391999-10-05 16:24:54 +000051}
52
Eric Andersenc6cb79d1999-10-13 18:01:10 +000053
54/*
55 * Copy one file to another, while possibly preserving its modes, times,
56 * and modes. Returns TRUE if successful, or FALSE on a failure with an
57 * error message output. (Failure is not indicted if the attributes cannot
58 * be set.)
59 */
60int
Eric Andersen3c163821999-10-14 22:16:57 +000061copyFile( const char *srcName, const char *destName,
62 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +000063{
64 int rfd;
65 int wfd;
66 int rcc;
67 int result;
68 char buf[BUF_SIZE];
69 struct stat srcStatBuf;
70 struct stat dstStatBuf;
71 struct utimbuf times;
72
73 if (followLinks == FALSE)
74 result = stat(srcName, &srcStatBuf);
75 else
76 result = lstat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +000077 if (result < 0) {
78 perror(srcName);
79 return FALSE;
80 }
81
82 if (followLinks == FALSE)
83 result = stat(destName, &dstStatBuf);
84 else
85 result = lstat(destName, &dstStatBuf);
86 if (result < 0) {
87 dstStatBuf.st_ino = -1;
88 dstStatBuf.st_dev = -1;
89 }
90
91 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
92 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
93 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
94 return FALSE;
95 }
96
97 if (S_ISDIR(srcStatBuf.st_mode)) {
98 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
99 /* Make sure the directory is writable */
100 if (mkdir(destName, 0777777 ^ umask(0))) {
101 perror(destName);
102 return (FALSE);
103 }
104 } else if (S_ISLNK(srcStatBuf.st_mode)) {
105 char *link_val;
106 int link_size;
107
108 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
109 link_val = (char *) alloca(PATH_MAX + 2);
110 link_size = readlink(srcName, link_val, PATH_MAX + 1);
111 if (link_size < 0) {
112 perror(srcName);
113 return (FALSE);
114 }
115 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000116 link_size = symlink(link_val, destName);
117 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000118 perror(destName);
119 return (FALSE);
120 }
121 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
122 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
123 if (mkfifo(destName, 644)) {
124 perror(destName);
125 return (FALSE);
126 }
127 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
128 || S_ISSOCK (srcStatBuf.st_mode)) {
129 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
130 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
131 perror(destName);
132 return (FALSE);
133 }
134 } else if (S_ISREG(srcStatBuf.st_mode)) {
135 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
136 rfd = open(srcName, O_RDONLY);
137 if (rfd < 0) {
138 perror(srcName);
139 return FALSE;
140 }
141
142 wfd = creat(destName, srcStatBuf.st_mode);
143 if (wfd < 0) {
144 perror(destName);
145 close(rfd);
146 return FALSE;
147 }
148
149 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
150 if (fullWrite(wfd, buf, rcc) < 0)
151 goto error_exit;
152 }
153 if (rcc < 0) {
154 goto error_exit;
155 }
156
157 close(rfd);
158 if (close(wfd) < 0) {
159 return FALSE;
160 }
161 }
162
163 if (setModes == TRUE) {
164 //fprintf(stderr, "Setting permissions for %s\n", destName);
165 chmod(destName, srcStatBuf.st_mode);
166 if (followLinks == TRUE)
167 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
168 else
169 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
170
171 times.actime = srcStatBuf.st_atime;
172 times.modtime = srcStatBuf.st_mtime;
173
174 utime(destName, &times);
175 }
176
177 return TRUE;
178
179
180 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000181 perror(destName);
182 close(rfd);
183 close(wfd);
184
185 return FALSE;
186}
Eric Andersencc8ed391999-10-05 16:24:54 +0000187#endif
188
189
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000190#ifdef BB_MV
191/*
192 * Build a path name from the specified directory name and file name.
193 * If the directory name is NULL, then the original fileName is returned.
194 * The built path is in a static area, and is overwritten for each call.
195 */
196char *buildName(const char *dirName, const char *fileName)
197{
198 const char *cp;
199 static char buf[PATH_LEN];
Eric Andersencc8ed391999-10-05 16:24:54 +0000200
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000201 if ((dirName == NULL) || (*dirName == '\0')) {
202 strcpy(buf, fileName);
203 return buf;
204 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000205
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000206 cp = strrchr(fileName, '/');
Eric Andersencc8ed391999-10-05 16:24:54 +0000207
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000208 if (cp)
209 fileName = cp + 1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000210
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000211 strcpy(buf, dirName);
212 strcat(buf, "/");
213
214 return buf;
215}
216#endif
217
Eric Andersencc8ed391999-10-05 16:24:54 +0000218
Eric Andersencc8ed391999-10-05 16:24:54 +0000219
220/*
221 * Return the standard ls-like mode string from a file mode.
222 * This is static and so is overwritten on each call.
223 */
Eric Andersenf811e071999-10-09 00:25:00 +0000224const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000225{
Eric Andersenf811e071999-10-09 00:25:00 +0000226 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000227
Eric Andersenf811e071999-10-09 00:25:00 +0000228 strcpy(buf, "----------");
Eric Andersencc8ed391999-10-05 16:24:54 +0000229
Eric Andersenf811e071999-10-09 00:25:00 +0000230 /*
231 * Fill in the file type.
232 */
233 if (S_ISDIR(mode))
234 buf[0] = 'd';
235 if (S_ISCHR(mode))
236 buf[0] = 'c';
237 if (S_ISBLK(mode))
238 buf[0] = 'b';
239 if (S_ISFIFO(mode))
240 buf[0] = 'p';
Eric Andersencc8ed391999-10-05 16:24:54 +0000241#ifdef S_ISLNK
Eric Andersenf811e071999-10-09 00:25:00 +0000242 if (S_ISLNK(mode))
243 buf[0] = 'l';
Eric Andersencc8ed391999-10-05 16:24:54 +0000244#endif
245#ifdef S_ISSOCK
Eric Andersenf811e071999-10-09 00:25:00 +0000246 if (S_ISSOCK(mode))
247 buf[0] = 's';
Eric Andersencc8ed391999-10-05 16:24:54 +0000248#endif
249
Eric Andersenf811e071999-10-09 00:25:00 +0000250 /*
251 * Now fill in the normal file permissions.
252 */
253 if (mode & S_IRUSR)
254 buf[1] = 'r';
255 if (mode & S_IWUSR)
256 buf[2] = 'w';
257 if (mode & S_IXUSR)
258 buf[3] = 'x';
259 if (mode & S_IRGRP)
260 buf[4] = 'r';
261 if (mode & S_IWGRP)
262 buf[5] = 'w';
263 if (mode & S_IXGRP)
264 buf[6] = 'x';
265 if (mode & S_IROTH)
266 buf[7] = 'r';
267 if (mode & S_IWOTH)
268 buf[8] = 'w';
269 if (mode & S_IXOTH)
270 buf[9] = 'x';
Eric Andersencc8ed391999-10-05 16:24:54 +0000271
Eric Andersenf811e071999-10-09 00:25:00 +0000272 /*
273 * Finally fill in magic stuff like suid and sticky text.
274 */
275 if (mode & S_ISUID)
276 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
277 if (mode & S_ISGID)
278 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
279 if (mode & S_ISVTX)
280 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
Eric Andersencc8ed391999-10-05 16:24:54 +0000281
Eric Andersenf811e071999-10-09 00:25:00 +0000282 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000283}
284
285
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000286#ifdef BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000287/*
Eric Andersen17d49ef1999-10-06 20:25:32 +0000288 * Get the time string to be used for a file.
289 * This is down to the minute for new files, but only the date for old files.
290 * The string is returned from a static buffer, and so is overwritten for
291 * each call.
292 */
Eric Andersenf811e071999-10-09 00:25:00 +0000293const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000294{
Eric Andersenf811e071999-10-09 00:25:00 +0000295 time_t now;
296 char *str;
297 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000298
Eric Andersenf811e071999-10-09 00:25:00 +0000299 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000300
Eric Andersenf811e071999-10-09 00:25:00 +0000301 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000302
Eric Andersenf811e071999-10-09 00:25:00 +0000303 strcpy(buf, &str[4]);
304 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000305
Eric Andersenf811e071999-10-09 00:25:00 +0000306 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
307 strcpy(&buf[7], &str[20]);
308 buf[11] = '\0';
309 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000310
Eric Andersenf811e071999-10-09 00:25:00 +0000311 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000312}
313
314
315/*
316 * Routine to see if a text string is matched by a wildcard pattern.
317 * Returns TRUE if the text is matched, or FALSE if it is not matched
318 * or if the pattern is invalid.
319 * * matches zero or more characters
320 * ? matches a single character
321 * [abc] matches 'a', 'b' or 'c'
322 * \c quotes character c
323 * Adapted from code written by Ingo Wilken.
324 */
Eric Andersenf811e071999-10-09 00:25:00 +0000325int match(const char *text, const char *pattern)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000326{
Eric Andersenf811e071999-10-09 00:25:00 +0000327 const char *retryPat;
328 const char *retryText;
329 int ch;
330 int found;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000331
Eric Andersenf811e071999-10-09 00:25:00 +0000332 retryPat = NULL;
333 retryText = NULL;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000334
Eric Andersenf811e071999-10-09 00:25:00 +0000335 while (*text || *pattern) {
336 ch = *pattern++;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000337
Eric Andersenf811e071999-10-09 00:25:00 +0000338 switch (ch) {
339 case '*':
340 retryPat = pattern;
341 retryText = text;
342 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000343
Eric Andersenf811e071999-10-09 00:25:00 +0000344 case '[':
345 found = FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000346
Eric Andersenf811e071999-10-09 00:25:00 +0000347 while ((ch = *pattern++) != ']') {
348 if (ch == '\\')
349 ch = *pattern++;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000350
Eric Andersenf811e071999-10-09 00:25:00 +0000351 if (ch == '\0')
352 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000353
Eric Andersenf811e071999-10-09 00:25:00 +0000354 if (*text == ch)
355 found = TRUE;
356 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000357
Eric Andersenf811e071999-10-09 00:25:00 +0000358 if (!found) {
359 pattern = retryPat;
360 text = ++retryText;
361 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000362
Eric Andersenf811e071999-10-09 00:25:00 +0000363 /* fall into next case */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000364
Eric Andersenf811e071999-10-09 00:25:00 +0000365 case '?':
366 if (*text++ == '\0')
367 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000368
Eric Andersenf811e071999-10-09 00:25:00 +0000369 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000370
Eric Andersenf811e071999-10-09 00:25:00 +0000371 case '\\':
372 ch = *pattern++;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000373
Eric Andersenf811e071999-10-09 00:25:00 +0000374 if (ch == '\0')
375 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000376
Eric Andersenf811e071999-10-09 00:25:00 +0000377 /* fall into next case */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000378
Eric Andersenf811e071999-10-09 00:25:00 +0000379 default:
380 if (*text == ch) {
381 if (*text)
382 text++;
383 break;
384 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000385
Eric Andersenf811e071999-10-09 00:25:00 +0000386 if (*text) {
387 pattern = retryPat;
388 text = ++retryText;
389 break;
390 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000391
Eric Andersenf811e071999-10-09 00:25:00 +0000392 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000393 }
394
Eric Andersenf811e071999-10-09 00:25:00 +0000395 if (pattern == NULL)
396 return FALSE;
397 }
398
399 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000400}
401
402
403/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000404 * Write all of the supplied buffer out to a file.
405 * This does multiple writes as necessary.
406 * Returns the amount written, or -1 on an error.
407 */
Eric Andersenf811e071999-10-09 00:25:00 +0000408int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000409{
Eric Andersenf811e071999-10-09 00:25:00 +0000410 int cc;
411 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000412
Eric Andersenf811e071999-10-09 00:25:00 +0000413 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000414
Eric Andersenf811e071999-10-09 00:25:00 +0000415 while (len > 0) {
416 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000417
Eric Andersenf811e071999-10-09 00:25:00 +0000418 if (cc < 0)
419 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000420
Eric Andersenf811e071999-10-09 00:25:00 +0000421 buf += cc;
422 total += cc;
423 len -= cc;
424 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000425
Eric Andersenf811e071999-10-09 00:25:00 +0000426 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000427}
428
429
430/*
431 * Read all of the supplied buffer from a file.
432 * This does multiple reads as necessary.
433 * Returns the amount read, or -1 on an error.
434 * A short read is returned on an end of file.
435 */
Eric Andersenf811e071999-10-09 00:25:00 +0000436int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000437{
Eric Andersenf811e071999-10-09 00:25:00 +0000438 int cc;
439 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000440
Eric Andersenf811e071999-10-09 00:25:00 +0000441 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000442
Eric Andersenf811e071999-10-09 00:25:00 +0000443 while (len > 0) {
444 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000445
Eric Andersenf811e071999-10-09 00:25:00 +0000446 if (cc < 0)
447 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000448
Eric Andersenf811e071999-10-09 00:25:00 +0000449 if (cc == 0)
450 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000451
Eric Andersenf811e071999-10-09 00:25:00 +0000452 buf += cc;
453 total += cc;
454 len -= cc;
455 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000456
Eric Andersenf811e071999-10-09 00:25:00 +0000457 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000458}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000459#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000460
461
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000462#if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
Eric Andersencc8ed391999-10-05 16:24:54 +0000463/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000464 * Walk down all the directories under the specified
465 * location, and do something (something specified
466 * by the fileAction and dirAction function pointers).
Eric Andersencc8ed391999-10-05 16:24:54 +0000467 */
468int
Eric Andersenf811e071999-10-09 00:25:00 +0000469recursiveAction(const char *fileName, int recurse, int followLinks,
470 int (*fileAction) (const char *fileName),
471 int (*dirAction) (const char *fileName))
Eric Andersencc8ed391999-10-05 16:24:54 +0000472{
Eric Andersenf811e071999-10-09 00:25:00 +0000473 int status;
474 struct stat statbuf;
475 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000476
Eric Andersen3c163821999-10-14 22:16:57 +0000477 if (followLinks == FALSE)
Eric Andersenf811e071999-10-09 00:25:00 +0000478 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000479 else
480 status = lstat(fileName, &statbuf);
481
Eric Andersencc8ed391999-10-05 16:24:54 +0000482 if (status < 0) {
483 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000484 return (FALSE);
485 }
486
487 if (recurse == FALSE) {
488 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000489 if (dirAction != NULL)
Eric Andersenf811e071999-10-09 00:25:00 +0000490 return (dirAction(fileName));
Eric Andersenf811e071999-10-09 00:25:00 +0000491 else
Eric Andersen3c163821999-10-14 22:16:57 +0000492 return (TRUE);
493 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000494 }
495
Eric Andersen2b69c401999-10-05 22:58:32 +0000496 if (S_ISDIR(statbuf.st_mode)) {
497 DIR *dir;
498 dir = opendir(fileName);
499 if (!dir) {
500 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000501 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000502 }
Eric Andersenf811e071999-10-09 00:25:00 +0000503 if (dirAction != NULL) {
504 status = dirAction(fileName);
505 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000506 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000507 return (FALSE);
508 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000509 }
Eric Andersenf811e071999-10-09 00:25:00 +0000510 while ((next = readdir(dir)) != NULL) {
511 char nextFile[NAME_MAX];
512 if ((strcmp(next->d_name, "..") == 0)
513 || (strcmp(next->d_name, ".") == 0)) {
514 continue;
515 }
516 sprintf(nextFile, "%s/%s", fileName, next->d_name);
517 status =
518 recursiveAction(nextFile, TRUE, followLinks, fileAction,
519 dirAction);
520 if (status < 0) {
521 closedir(dir);
522 return (FALSE);
523 }
524 }
525 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000526 if (status < 0) {
527 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000528 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000529 }
Eric Andersenf811e071999-10-09 00:25:00 +0000530 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000531 if (fileAction == NULL)
532 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000533 else
Eric Andersenf811e071999-10-09 00:25:00 +0000534 return (fileAction(fileName));
Eric Andersencc8ed391999-10-05 16:24:54 +0000535 }
Eric Andersenf811e071999-10-09 00:25:00 +0000536 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000537}
538
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000539#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000540
Eric Andersenf6be9441999-10-13 21:12:06 +0000541
542
543#if defined (BB_TAR) || defined (BB_MKDIR)
544/*
545 * Attempt to create the directories along the specified path, except for
546 * the final component. The mode is given for the final directory only,
547 * while all previous ones get default protections. Errors are not reported
548 * here, as failures to restore files can be reported later.
549 */
550extern void createPath (const char *name, int mode)
551{
552 char *cp;
553 char *cpOld;
554 char buf[NAME_MAX];
555
556 strcpy (buf, name);
557
558 cp = strchr (buf, '/');
559
560 while (cp) {
561 cpOld = cp;
562 cp = strchr (cp + 1, '/');
563
564 *cpOld = '\0';
565
566 if (mkdir (buf, cp ? 0777 : mode) == 0)
567 printf ("Directory \"%s\" created\n", buf);
568
569 *cpOld = '/';
570 }
571}
572#endif
573
574
575
576#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
577/* [ugoa]{+|-|=}[rwxstl] */
578extern int parse_mode( const char* s, mode_t* theMode)
579{
580 mode_t or;
581 mode_t and;
582 mode_t mode = 0;
583 mode_t groups = S_ISVTX;
584 char type;
585 char c;
586
587 do {
588 for ( ; ; ) {
589 switch ( c = *s++ ) {
590 case '\0':
591 return (FALSE);
592 case 'u':
593 groups |= S_ISUID|S_IRWXU;
594 continue;
595 case 'g':
596 groups |= S_ISGID|S_IRWXG;
597 continue;
598 case 'o':
599 groups |= S_IRWXO;
600 continue;
601 case 'a':
602 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
603 continue;
604 case '+':
605 case '=':
606 case '-':
607 type = c;
608 if ( groups == S_ISVTX ) /* The default is "all" */
609 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
610 break;
611 default:
612 if ( c >= '0' && c <= '7' && mode == 0 && groups == S_ISVTX ) {
613 and = 0;
614 or = strtol(--s, 0, 010);
615 return (TRUE);
616 }
617 else
618 return (FALSE);
619 }
620 break;
621 }
622
623 while ( (c = *s++) != '\0' ) {
624 switch ( c ) {
625 case ',':
626 break;
627 case 'r':
628 mode |= S_IRUSR|S_IRGRP|S_IROTH;
629 continue;
630 case 'w':
631 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
632 continue;
633 case 'x':
634 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
635 continue;
636 case 's':
637 mode |= S_IXGRP|S_ISUID|S_ISGID;
638 continue;
639 case 't':
640 mode |= S_ISVTX;
641 continue;
642 default:
643 return (FALSE);
644 }
645 break;
646 }
647 switch ( type ) {
648 case '=':
649 and &= ~(groups);
650 /* fall through */
651 case '+':
652 or |= mode & groups;
653 break;
654 case '-':
655 and &= ~(mode & groups);
656 or &= and;
657 break;
658 }
659 } while ( c == ',' );
660 return (TRUE);
661}
662#endif
663
Eric Andersencc8ed391999-10-05 16:24:54 +0000664/* END CODE */