blob: 277e27c706bd5b4906b8cb6a84454e6761a82331 [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
36#if 0
37
Eric Andersenf811e071999-10-09 00:25:00 +000038extern char *join_paths(char *buffer, const char *a, const char *b)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Eric Andersenf811e071999-10-09 00:25:00 +000040 int length = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Eric Andersenf811e071999-10-09 00:25:00 +000042 if (a && *a) {
43 length = strlen(a);
44 memcpy(buffer, a, length);
45 }
46 if (b && *b) {
47 if (length > 0 && buffer[length - 1] != '/')
48 buffer[length++] = '/';
49 if (*b == '/')
50 b++;
51 strcpy(&buffer[length], b);
52 }
53 return buffer;
Eric Andersencc8ed391999-10-05 16:24:54 +000054}
55
56#endif
57
58
59
60
61
62
Eric Andersenf811e071999-10-09 00:25:00 +000063static CHUNK *chunkList;
Eric Andersencc8ed391999-10-05 16:24:54 +000064
Eric Andersencc8ed391999-10-05 16:24:54 +000065
66/*
67 * Return the standard ls-like mode string from a file mode.
68 * This is static and so is overwritten on each call.
69 */
Eric Andersenf811e071999-10-09 00:25:00 +000070const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000071{
Eric Andersenf811e071999-10-09 00:25:00 +000072 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +000073
Eric Andersenf811e071999-10-09 00:25:00 +000074 strcpy(buf, "----------");
Eric Andersencc8ed391999-10-05 16:24:54 +000075
Eric Andersenf811e071999-10-09 00:25:00 +000076 /*
77 * Fill in the file type.
78 */
79 if (S_ISDIR(mode))
80 buf[0] = 'd';
81 if (S_ISCHR(mode))
82 buf[0] = 'c';
83 if (S_ISBLK(mode))
84 buf[0] = 'b';
85 if (S_ISFIFO(mode))
86 buf[0] = 'p';
Eric Andersencc8ed391999-10-05 16:24:54 +000087#ifdef S_ISLNK
Eric Andersenf811e071999-10-09 00:25:00 +000088 if (S_ISLNK(mode))
89 buf[0] = 'l';
Eric Andersencc8ed391999-10-05 16:24:54 +000090#endif
91#ifdef S_ISSOCK
Eric Andersenf811e071999-10-09 00:25:00 +000092 if (S_ISSOCK(mode))
93 buf[0] = 's';
Eric Andersencc8ed391999-10-05 16:24:54 +000094#endif
95
Eric Andersenf811e071999-10-09 00:25:00 +000096 /*
97 * Now fill in the normal file permissions.
98 */
99 if (mode & S_IRUSR)
100 buf[1] = 'r';
101 if (mode & S_IWUSR)
102 buf[2] = 'w';
103 if (mode & S_IXUSR)
104 buf[3] = 'x';
105 if (mode & S_IRGRP)
106 buf[4] = 'r';
107 if (mode & S_IWGRP)
108 buf[5] = 'w';
109 if (mode & S_IXGRP)
110 buf[6] = 'x';
111 if (mode & S_IROTH)
112 buf[7] = 'r';
113 if (mode & S_IWOTH)
114 buf[8] = 'w';
115 if (mode & S_IXOTH)
116 buf[9] = 'x';
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Eric Andersenf811e071999-10-09 00:25:00 +0000118 /*
119 * Finally fill in magic stuff like suid and sticky text.
120 */
121 if (mode & S_ISUID)
122 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
123 if (mode & S_ISGID)
124 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
125 if (mode & S_ISVTX)
126 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
Eric Andersencc8ed391999-10-05 16:24:54 +0000127
Eric Andersenf811e071999-10-09 00:25:00 +0000128 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000129}
130
131
132/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000133 * Return TRUE if a fileName is a directory.
134 * Nonexistant files return FALSE.
135 */
Eric Andersenf811e071999-10-09 00:25:00 +0000136int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +0000137{
Eric Andersenf811e071999-10-09 00:25:00 +0000138 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000139
Eric Andersenf811e071999-10-09 00:25:00 +0000140 if (stat(name, &statBuf) < 0)
141 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000142
Eric Andersenf811e071999-10-09 00:25:00 +0000143 return S_ISDIR(statBuf.st_mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000144}
145
146
147/*
148 * Return TRUE if a filename is a block or character device.
149 * Nonexistant files return FALSE.
150 */
Eric Andersenf811e071999-10-09 00:25:00 +0000151int isDevice(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +0000152{
Eric Andersenf811e071999-10-09 00:25:00 +0000153 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000154
Eric Andersenf811e071999-10-09 00:25:00 +0000155 if (stat(name, &statBuf) < 0)
156 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Eric Andersenf811e071999-10-09 00:25:00 +0000158 return S_ISBLK(statBuf.st_mode) || S_ISCHR(statBuf.st_mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000159}
160
161
162/*
163 * Copy one file to another, while possibly preserving its modes, times,
164 * and modes. Returns TRUE if successful, or FALSE on a failure with an
165 * error message output. (Failure is not indicted if the attributes cannot
166 * be set.)
167 */
Eric Andersenf811e071999-10-09 00:25:00 +0000168int
Eric Andersencc8ed391999-10-05 16:24:54 +0000169copyFile(
Eric Andersenf811e071999-10-09 00:25:00 +0000170 const char *srcName,
171 const char *destName, int setModes, int followLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000172{
Eric Andersenf811e071999-10-09 00:25:00 +0000173 int rfd;
174 int wfd;
175 int rcc;
176 char buf[BUF_SIZE];
177 struct stat statBuf1;
178 struct stat statBuf2;
179 struct utimbuf times;
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Eric Andersenf811e071999-10-09 00:25:00 +0000181 if (stat(srcName, &statBuf1) < 0) {
182 perror(srcName);
183 return FALSE;
184 }
185
186 if (stat(destName, &statBuf2) < 0) {
187 statBuf2.st_ino = -1;
188 statBuf2.st_dev = -1;
189 }
190
191 if ((statBuf1.st_dev == statBuf2.st_dev) &&
192 (statBuf1.st_ino == statBuf2.st_ino)) {
193 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
194 return FALSE;
195 }
196
197 if (S_ISDIR(statBuf1.st_mode)) {
198 /* Make sure the directory is writable */
199 if (mkdir(destName, 0777777 ^ umask(0))) {
200 perror(destName);
201 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000202 }
Eric Andersenf811e071999-10-09 00:25:00 +0000203 } else if (S_ISFIFO(statBuf1.st_mode)) {
204 if (mkfifo(destName, 644)) {
205 perror(destName);
206 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000207 }
Eric Andersenf811e071999-10-09 00:25:00 +0000208 } else if (S_ISBLK(statBuf1.st_mode) || S_ISCHR(statBuf1.st_mode)) {
209 if (mknod(destName, 644, statBuf1.st_rdev)) {
210 perror(destName);
211 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000212 }
Eric Andersenf811e071999-10-09 00:25:00 +0000213 } else if (S_ISLNK(statBuf1.st_mode)) {
214 char *link_val;
215 int link_size;
Eric Andersencc8ed391999-10-05 16:24:54 +0000216
Eric Andersenf811e071999-10-09 00:25:00 +0000217 link_val = (char *) alloca(PATH_MAX + 2);
218 link_size = readlink(srcName, link_val, PATH_MAX + 1);
219 if (link_size < 0) {
220 perror(srcName);
221 return (FALSE);
222 }
223 link_val[link_size] = '\0';
224 if (symlink(link_val, destName)) {
225 perror(srcName);
226 return (FALSE);
227 }
228 } else {
Eric Andersencc8ed391999-10-05 16:24:54 +0000229 rfd = open(srcName, O_RDONLY);
Eric Andersenf811e071999-10-09 00:25:00 +0000230 if (rfd < 0) {
231 perror(srcName);
232 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000233 }
234
235 wfd = creat(destName, statBuf1.st_mode);
Eric Andersenf811e071999-10-09 00:25:00 +0000236 if (wfd < 0) {
237 perror(destName);
238 close(rfd);
239 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000240 }
241
Eric Andersenf811e071999-10-09 00:25:00 +0000242 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
243 if (fullWrite(wfd, buf, rcc) < 0)
Eric Andersencc8ed391999-10-05 16:24:54 +0000244 goto error_exit;
245 }
Eric Andersenf811e071999-10-09 00:25:00 +0000246 if (rcc < 0) {
247 perror(srcName);
248 goto error_exit;
Eric Andersencc8ed391999-10-05 16:24:54 +0000249 }
250
Eric Andersencc8ed391999-10-05 16:24:54 +0000251 close(rfd);
Eric Andersencc8ed391999-10-05 16:24:54 +0000252
Eric Andersenf811e071999-10-09 00:25:00 +0000253 if (close(wfd) < 0) {
254 perror(destName);
255 return FALSE;
256 }
257 }
258
259 if (setModes == TRUE) {
260 chmod(destName, statBuf1.st_mode);
261 if (followLinks == TRUE)
262 chown(destName, statBuf1.st_uid, statBuf1.st_gid);
263 else
264 lchown(destName, statBuf1.st_uid, statBuf1.st_gid);
265
266 times.actime = statBuf1.st_atime;
267 times.modtime = statBuf1.st_mtime;
268
269 utime(destName, &times);
270 }
271
272 return TRUE;
273
274
275 error_exit:
276 close(rfd);
277 close(wfd);
278
279 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000280}
281
282
283/*
284 * Build a path name from the specified directory name and file name.
285 * If the directory name is NULL, then the original fileName is returned.
286 * The built path is in a static area, and is overwritten for each call.
287 */
Eric Andersenf811e071999-10-09 00:25:00 +0000288char *buildName(const char *dirName, const char *fileName)
Eric Andersencc8ed391999-10-05 16:24:54 +0000289{
Eric Andersenf811e071999-10-09 00:25:00 +0000290 const char *cp;
291 static char buf[PATH_LEN];
Eric Andersencc8ed391999-10-05 16:24:54 +0000292
Eric Andersenf811e071999-10-09 00:25:00 +0000293 if ((dirName == NULL) || (*dirName == '\0')) {
294 strcpy(buf, fileName);
Eric Andersencc8ed391999-10-05 16:24:54 +0000295 return buf;
Eric Andersenf811e071999-10-09 00:25:00 +0000296 }
297
298 cp = strrchr(fileName, '/');
299
300 if (cp)
301 fileName = cp + 1;
302
303 strcpy(buf, dirName);
304 strcat(buf, "/");
305
306 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000307}
308
309
310
311/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000312 * Make a NULL-terminated string out of an argc, argv pair.
313 * Returns TRUE if successful, or FALSE if the string is too long,
314 * with an error message given. This does not handle spaces within
315 * arguments correctly.
316 */
Eric Andersenf811e071999-10-09 00:25:00 +0000317int makeString( int argc, const char **argv, char *buf, int bufLen)
Eric Andersencc8ed391999-10-05 16:24:54 +0000318{
Eric Andersenf811e071999-10-09 00:25:00 +0000319 int len;
Eric Andersencc8ed391999-10-05 16:24:54 +0000320
Eric Andersenf811e071999-10-09 00:25:00 +0000321 while (argc-- > 0) {
322 len = strlen(*argv);
Eric Andersencc8ed391999-10-05 16:24:54 +0000323
Eric Andersenf811e071999-10-09 00:25:00 +0000324 if (len >= bufLen) {
325 fprintf(stderr, "Argument string too long\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000326
Eric Andersenf811e071999-10-09 00:25:00 +0000327 return FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000328 }
329
Eric Andersenf811e071999-10-09 00:25:00 +0000330 strcpy(buf, *argv++);
Eric Andersencc8ed391999-10-05 16:24:54 +0000331
Eric Andersenf811e071999-10-09 00:25:00 +0000332 buf += len;
333 bufLen -= len;
334
335 if (argc)
336 *buf++ = ' ';
337
338 bufLen--;
339 }
340
341 *buf = '\0';
342
343 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000344}
345
346
347/*
348 * Allocate a chunk of memory (like malloc).
349 * The difference, though, is that the memory allocated is put on a
350 * list of chunks which can be freed all at one time. You CAN NOT free
351 * an individual chunk.
352 */
Eric Andersenf811e071999-10-09 00:25:00 +0000353char *getChunk(int size)
Eric Andersencc8ed391999-10-05 16:24:54 +0000354{
Eric Andersenf811e071999-10-09 00:25:00 +0000355 CHUNK *chunk;
Eric Andersencc8ed391999-10-05 16:24:54 +0000356
Eric Andersenf811e071999-10-09 00:25:00 +0000357 if (size < CHUNK_INIT_SIZE)
358 size = CHUNK_INIT_SIZE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000359
Eric Andersenf811e071999-10-09 00:25:00 +0000360 chunk = (CHUNK *) malloc(size + sizeof(CHUNK) - CHUNK_INIT_SIZE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000361
Eric Andersenf811e071999-10-09 00:25:00 +0000362 if (chunk == NULL)
363 return NULL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000364
Eric Andersenf811e071999-10-09 00:25:00 +0000365 chunk->next = chunkList;
366 chunkList = chunk;
Eric Andersencc8ed391999-10-05 16:24:54 +0000367
Eric Andersenf811e071999-10-09 00:25:00 +0000368 return chunk->data;
Eric Andersencc8ed391999-10-05 16:24:54 +0000369}
370
371
372/*
373 * Duplicate a string value using the chunk allocator.
374 * The returned string cannot be individually freed, but can only be freed
375 * with other strings when freeChunks is called. Returns NULL on failure.
376 */
Eric Andersenf811e071999-10-09 00:25:00 +0000377char *chunkstrdup(const char *str)
Eric Andersencc8ed391999-10-05 16:24:54 +0000378{
Eric Andersenf811e071999-10-09 00:25:00 +0000379 int len;
380 char *newStr;
Eric Andersencc8ed391999-10-05 16:24:54 +0000381
Eric Andersenf811e071999-10-09 00:25:00 +0000382 len = strlen(str) + 1;
383 newStr = getChunk(len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000384
Eric Andersenf811e071999-10-09 00:25:00 +0000385 if (newStr)
386 memcpy(newStr, str, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
Eric Andersenf811e071999-10-09 00:25:00 +0000388 return newStr;
Eric Andersencc8ed391999-10-05 16:24:54 +0000389}
390
391
392/*
393 * Free all chunks of memory that had been allocated since the last
394 * call to this routine.
395 */
Eric Andersenf811e071999-10-09 00:25:00 +0000396void freeChunks(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000397{
Eric Andersenf811e071999-10-09 00:25:00 +0000398 CHUNK *chunk;
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Eric Andersenf811e071999-10-09 00:25:00 +0000400 while (chunkList) {
401 chunk = chunkList;
402 chunkList = chunk->next;
403 free((char *) chunk);
404 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000405}
406
407
408/*
Eric Andersen17d49ef1999-10-06 20:25:32 +0000409 * Get the time string to be used for a file.
410 * This is down to the minute for new files, but only the date for old files.
411 * The string is returned from a static buffer, and so is overwritten for
412 * each call.
413 */
Eric Andersenf811e071999-10-09 00:25:00 +0000414const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000415{
Eric Andersenf811e071999-10-09 00:25:00 +0000416 time_t now;
417 char *str;
418 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000419
Eric Andersenf811e071999-10-09 00:25:00 +0000420 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000421
Eric Andersenf811e071999-10-09 00:25:00 +0000422 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000423
Eric Andersenf811e071999-10-09 00:25:00 +0000424 strcpy(buf, &str[4]);
425 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000426
Eric Andersenf811e071999-10-09 00:25:00 +0000427 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
428 strcpy(&buf[7], &str[20]);
429 buf[11] = '\0';
430 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000431
Eric Andersenf811e071999-10-09 00:25:00 +0000432 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000433}
434
435
436/*
437 * Routine to see if a text string is matched by a wildcard pattern.
438 * Returns TRUE if the text is matched, or FALSE if it is not matched
439 * or if the pattern is invalid.
440 * * matches zero or more characters
441 * ? matches a single character
442 * [abc] matches 'a', 'b' or 'c'
443 * \c quotes character c
444 * Adapted from code written by Ingo Wilken.
445 */
Eric Andersenf811e071999-10-09 00:25:00 +0000446int match(const char *text, const char *pattern)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000447{
Eric Andersenf811e071999-10-09 00:25:00 +0000448 const char *retryPat;
449 const char *retryText;
450 int ch;
451 int found;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000452
Eric Andersenf811e071999-10-09 00:25:00 +0000453 retryPat = NULL;
454 retryText = NULL;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000455
Eric Andersenf811e071999-10-09 00:25:00 +0000456 while (*text || *pattern) {
457 ch = *pattern++;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000458
Eric Andersenf811e071999-10-09 00:25:00 +0000459 switch (ch) {
460 case '*':
461 retryPat = pattern;
462 retryText = text;
463 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000464
Eric Andersenf811e071999-10-09 00:25:00 +0000465 case '[':
466 found = FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000467
Eric Andersenf811e071999-10-09 00:25:00 +0000468 while ((ch = *pattern++) != ']') {
469 if (ch == '\\')
470 ch = *pattern++;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000471
Eric Andersenf811e071999-10-09 00:25:00 +0000472 if (ch == '\0')
473 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000474
Eric Andersenf811e071999-10-09 00:25:00 +0000475 if (*text == ch)
476 found = TRUE;
477 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000478
Eric Andersenf811e071999-10-09 00:25:00 +0000479 if (!found) {
480 pattern = retryPat;
481 text = ++retryText;
482 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000483
Eric Andersenf811e071999-10-09 00:25:00 +0000484 /* fall into next case */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000485
Eric Andersenf811e071999-10-09 00:25:00 +0000486 case '?':
487 if (*text++ == '\0')
488 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000489
Eric Andersenf811e071999-10-09 00:25:00 +0000490 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000491
Eric Andersenf811e071999-10-09 00:25:00 +0000492 case '\\':
493 ch = *pattern++;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000494
Eric Andersenf811e071999-10-09 00:25:00 +0000495 if (ch == '\0')
496 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000497
Eric Andersenf811e071999-10-09 00:25:00 +0000498 /* fall into next case */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000499
Eric Andersenf811e071999-10-09 00:25:00 +0000500 default:
501 if (*text == ch) {
502 if (*text)
503 text++;
504 break;
505 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000506
Eric Andersenf811e071999-10-09 00:25:00 +0000507 if (*text) {
508 pattern = retryPat;
509 text = ++retryText;
510 break;
511 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000512
Eric Andersenf811e071999-10-09 00:25:00 +0000513 return FALSE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000514 }
515
Eric Andersenf811e071999-10-09 00:25:00 +0000516 if (pattern == NULL)
517 return FALSE;
518 }
519
520 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000521}
522
523
524/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000525 * Write all of the supplied buffer out to a file.
526 * This does multiple writes as necessary.
527 * Returns the amount written, or -1 on an error.
528 */
Eric Andersenf811e071999-10-09 00:25:00 +0000529int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000530{
Eric Andersenf811e071999-10-09 00:25:00 +0000531 int cc;
532 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000533
Eric Andersenf811e071999-10-09 00:25:00 +0000534 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000535
Eric Andersenf811e071999-10-09 00:25:00 +0000536 while (len > 0) {
537 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000538
Eric Andersenf811e071999-10-09 00:25:00 +0000539 if (cc < 0)
540 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000541
Eric Andersenf811e071999-10-09 00:25:00 +0000542 buf += cc;
543 total += cc;
544 len -= cc;
545 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000546
Eric Andersenf811e071999-10-09 00:25:00 +0000547 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000548}
549
550
551/*
552 * Read all of the supplied buffer from a file.
553 * This does multiple reads as necessary.
554 * Returns the amount read, or -1 on an error.
555 * A short read is returned on an end of file.
556 */
Eric Andersenf811e071999-10-09 00:25:00 +0000557int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000558{
Eric Andersenf811e071999-10-09 00:25:00 +0000559 int cc;
560 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000561
Eric Andersenf811e071999-10-09 00:25:00 +0000562 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000563
Eric Andersenf811e071999-10-09 00:25:00 +0000564 while (len > 0) {
565 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000566
Eric Andersenf811e071999-10-09 00:25:00 +0000567 if (cc < 0)
568 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000569
Eric Andersenf811e071999-10-09 00:25:00 +0000570 if (cc == 0)
571 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000572
Eric Andersenf811e071999-10-09 00:25:00 +0000573 buf += cc;
574 total += cc;
575 len -= cc;
576 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000577
Eric Andersenf811e071999-10-09 00:25:00 +0000578 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000579}
580
581
582/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000583 * Walk down all the directories under the specified
584 * location, and do something (something specified
585 * by the fileAction and dirAction function pointers).
Eric Andersencc8ed391999-10-05 16:24:54 +0000586 */
587int
Eric Andersenf811e071999-10-09 00:25:00 +0000588recursiveAction(const char *fileName, int recurse, int followLinks,
589 int (*fileAction) (const char *fileName),
590 int (*dirAction) (const char *fileName))
Eric Andersencc8ed391999-10-05 16:24:54 +0000591{
Eric Andersenf811e071999-10-09 00:25:00 +0000592 int status;
593 struct stat statbuf;
594 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000595
Eric Andersencc8ed391999-10-05 16:24:54 +0000596 if (followLinks)
Eric Andersencc8ed391999-10-05 16:24:54 +0000597 status = lstat(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000598 else
599 status = stat(fileName, &statbuf);
Eric Andersencc8ed391999-10-05 16:24:54 +0000600 if (status < 0) {
601 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000602 return (FALSE);
603 }
604
605 if (recurse == FALSE) {
606 if (S_ISDIR(statbuf.st_mode)) {
607 if (dirAction == NULL)
608 return (TRUE);
609 else
610 return (dirAction(fileName));
611 } else {
612 if (fileAction == NULL)
613 return (TRUE);
614 else
615 return (fileAction(fileName));
616 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000617 }
618
Eric Andersen2b69c401999-10-05 22:58:32 +0000619 if (S_ISDIR(statbuf.st_mode)) {
620 DIR *dir;
Eric Andersenf811e071999-10-09 00:25:00 +0000621 fprintf(stderr, "Dir: %s\n", fileName);
Eric Andersen2b69c401999-10-05 22:58:32 +0000622 dir = opendir(fileName);
623 if (!dir) {
624 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000625 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000626 }
Eric Andersenf811e071999-10-09 00:25:00 +0000627 if (dirAction != NULL) {
628 status = dirAction(fileName);
629 if (status == FALSE) {
630 perror("cp");
631 return (FALSE);
632 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000633 }
Eric Andersenf811e071999-10-09 00:25:00 +0000634 while ((next = readdir(dir)) != NULL) {
635 char nextFile[NAME_MAX];
636 if ((strcmp(next->d_name, "..") == 0)
637 || (strcmp(next->d_name, ".") == 0)) {
638 continue;
639 }
640 sprintf(nextFile, "%s/%s", fileName, next->d_name);
641 status =
642 recursiveAction(nextFile, TRUE, followLinks, fileAction,
643 dirAction);
644 if (status < 0) {
645 closedir(dir);
646 return (FALSE);
647 }
648 }
649 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000650 if (status < 0) {
651 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000652 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000653 }
Eric Andersenf811e071999-10-09 00:25:00 +0000654 } else {
655 fprintf(stderr, "File: %s\n", fileName);
656 if (fileAction == NULL)
657 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000658 else
Eric Andersenf811e071999-10-09 00:25:00 +0000659 return (fileAction(fileName));
Eric Andersencc8ed391999-10-05 16:24:54 +0000660 }
Eric Andersenf811e071999-10-09 00:25:00 +0000661 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000662}
663
664
665
666/* END CODE */