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 * Write all of the supplied buffer out to a file.
410 * This does multiple writes as necessary.
411 * Returns the amount written, or -1 on an error.
414 fullWrite(int fd, const char * buf, int len)
423 cc = write(fd, buf, len);
438 * Read all of the supplied buffer from a file.
439 * This does multiple reads as necessary.
440 * Returns the amount read, or -1 on an error.
441 * A short read is returned on an end of file.
444 fullRead(int fd, char * buf, int len)
453 cc = read(fd, buf, len);
471 * Walk down all the directories under the specified
472 * location, and do something (something specified
473 * by the fileAction and dirAction function pointers).
476 recursiveAction( const char *fileName, BOOL followLinks,
477 int (*fileAction)(const char* fileName),
478 int (*dirAction)(const char* fileName))
485 status = stat(fileName, &statbuf);
487 status = lstat(fileName, &statbuf);
494 if (S_ISDIR(statbuf.st_mode)) {
496 dir = opendir(fileName);
501 while ((next = readdir (dir)) != NULL) {
502 char nextFile[NAME_MAX];
503 if ( (strcmp(next->d_name, "..") == 0) || (strcmp(next->d_name, ".") == 0) ) {
506 sprintf(nextFile, "%s/%s", fileName, next->d_name);
507 status = recursiveAction(nextFile, followLinks, fileAction, dirAction);
513 status = closedir (dir);
521 return(dirAction(fileName));
524 if (fileAction==NULL)
527 return(fileAction(fileName));