4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
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.
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.
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
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.
30 //#include <sys/types.h>
31 //#include <sys/stat.h>
39 join_paths(char * buffer, const char * a, const char * b)
45 memcpy(buffer, a, length);
48 if ( length > 0 && buffer[length - 1] != '/' )
49 buffer[length++] = '/';
52 strcpy(&buffer[length], b);
64 static CHUNK * chunkList;
68 * Return the standard ls-like mode string from a file mode.
69 * This is static and so is overwritten on each call.
76 strcpy(buf, "----------");
79 * Fill in the file type.
99 * Now fill in the normal file permissions.
121 * Finally fill in magic stuff like suid and sticky text.
124 buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
126 buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
128 buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
135 * Return TRUE if a fileName is a directory.
136 * Nonexistant files return FALSE.
139 isDirectory(const char * name)
143 if (stat(name, &statBuf) < 0)
146 return S_ISDIR(statBuf.st_mode);
151 * Return TRUE if a filename is a block or character device.
152 * Nonexistant files return FALSE.
155 isDevice(const char * name)
159 if (stat(name, &statBuf) < 0)
162 return S_ISBLK(statBuf.st_mode) || S_ISCHR(statBuf.st_mode);
167 * Copy one file to another, while possibly preserving its modes, times,
168 * and modes. Returns TRUE if successful, or FALSE on a failure with an
169 * error message output. (Failure is not indicted if the attributes cannot
174 const char * srcName,
175 const char * destName,
183 struct stat statBuf1;
184 struct stat statBuf2;
185 struct utimbuf times;
187 if (stat(srcName, &statBuf1) < 0)
194 if (stat(destName, &statBuf2) < 0)
196 statBuf2.st_ino = -1;
197 statBuf2.st_dev = -1;
200 if ((statBuf1.st_dev == statBuf2.st_dev) &&
201 (statBuf1.st_ino == statBuf2.st_ino))
203 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
208 rfd = open(srcName, O_RDONLY);
217 wfd = creat(destName, statBuf1.st_mode);
227 while ((rcc = read(rfd, buf, sizeof(buf))) > 0)
229 if (fullWrite(wfd, buf, rcc) < 0)
250 (void) chmod(destName, statBuf1.st_mode);
252 (void) chown(destName, statBuf1.st_uid, statBuf1.st_gid);
254 times.actime = statBuf1.st_atime;
255 times.modtime = statBuf1.st_mtime;
257 (void) utime(destName, ×);
272 * Build a path name from the specified directory name and file name.
273 * If the directory name is NULL, then the original fileName is returned.
274 * The built path is in a static area, and is overwritten for each call.
277 buildName(const char * dirName, const char * fileName)
280 static char buf[PATH_LEN];
282 if ((dirName == NULL) || (*dirName == '\0'))
285 cp = strrchr(fileName, '/');
290 strcpy(buf, dirName);
292 strcat(buf, fileName);
300 * Make a NULL-terminated string out of an argc, argv pair.
301 * Returns TRUE if successful, or FALSE if the string is too long,
302 * with an error message given. This does not handle spaces within
303 * arguments correctly.
321 fprintf(stderr, "Argument string too long\n");
326 strcpy(buf, *argv++);
344 * Allocate a chunk of memory (like malloc).
345 * The difference, though, is that the memory allocated is put on a
346 * list of chunks which can be freed all at one time. You CAN NOT free
347 * an individual chunk.
354 if (size < CHUNK_INIT_SIZE)
355 size = CHUNK_INIT_SIZE;
357 chunk = (CHUNK *) malloc(size + sizeof(CHUNK) - CHUNK_INIT_SIZE);
362 chunk->next = chunkList;
370 * Duplicate a string value using the chunk allocator.
371 * The returned string cannot be individually freed, but can only be freed
372 * with other strings when freeChunks is called. Returns NULL on failure.
375 chunkstrdup(const char * str)
380 len = strlen(str) + 1;
381 newStr = getChunk(len);
384 memcpy(newStr, str, len);
391 * Free all chunks of memory that had been allocated since the last
392 * call to this routine.
402 chunkList = chunk->next;
403 free((char *) chunk);
409 * 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
415 timeString(time_t timeVal)
423 str = ctime(&timeVal);
425 strcpy(buf, &str[4]);
428 if ((timeVal > now) || (timeVal < now - 365*24*60*60L))
430 strcpy(&buf[7], &str[20]);
439 * Routine to see if a text string is matched by a wildcard pattern.
440 * Returns TRUE if the text is matched, or FALSE if it is not matched
441 * or if the pattern is invalid.
442 * * matches zero or more characters
443 * ? matches a single character
444 * [abc] matches 'a', 'b' or 'c'
445 * \c quotes character c
446 * Adapted from code written by Ingo Wilken.
449 match(const char * text, const char * pattern)
451 const char * retryPat;
452 const char * retryText;
459 while (*text || *pattern)
473 while ((ch = *pattern++) != ']')
491 /* fall into next case */
505 /* fall into next case */
534 * Write all of the supplied buffer out to a file.
535 * This does multiple writes as necessary.
536 * Returns the amount written, or -1 on an error.
539 fullWrite(int fd, const char * buf, int len)
548 cc = write(fd, buf, len);
563 * Read all of the supplied buffer from a file.
564 * This does multiple reads as necessary.
565 * Returns the amount read, or -1 on an error.
566 * A short read is returned on an end of file.
569 fullRead(int fd, char * buf, int len)
578 cc = read(fd, buf, len);
596 * Walk down all the directories under the specified
597 * location, and do something (something specified
598 * by the fileAction and dirAction function pointers).
601 recursiveAction( const char *fileName, BOOL recurse, BOOL followLinks,
602 int (*fileAction)(const char* fileName),
603 int (*dirAction)(const char* fileName))
609 if (!recurse && S_ISDIR(statbuf.st_mode)) {
613 return(dirAction(fileName));
615 if (fileAction==NULL)
618 return(fileAction(fileName));
622 status = stat(fileName, &statbuf);
624 status = lstat(fileName, &statbuf);
631 if (S_ISDIR(statbuf.st_mode)) {
633 dir = opendir(fileName);
638 while ((next = readdir (dir)) != NULL) {
639 char nextFile[NAME_MAX];
640 if ( (strcmp(next->d_name, "..") == 0) || (strcmp(next->d_name, ".") == 0) ) {
643 sprintf(nextFile, "%s/%s", fileName, next->d_name);
644 status = recursiveAction(nextFile, TRUE, followLinks, fileAction, dirAction);
650 status = closedir (dir);
658 return(dirAction(fileName));
661 if (fileAction==NULL)
664 return(fileAction(fileName));