More stuff.

This commit is contained in:
Eric Andersen 1999-10-09 00:25:00 +00:00
parent 8341a15653
commit f811e07b07
17 changed files with 883 additions and 873 deletions

View File

@ -30,7 +30,7 @@ static const struct Applet applets[] = {
{"clear", clear_main}, {"clear", clear_main},
#endif #endif
#ifdef BB_CP //bin #ifdef BB_CP //bin
{"cp", dyadic_main}, {"cp", cp_main},
#endif #endif
#ifdef BB_DATE //bin #ifdef BB_DATE //bin
{"date", date_main}, {"date", date_main},

View File

@ -73,18 +73,18 @@ typedef struct
/* /*
* Static data. * Static data.
*/ */
static BOOL listFlag; static int listFlag;
static BOOL extractFlag; static int extractFlag;
static BOOL createFlag; static int createFlag;
static BOOL verboseFlag; static int verboseFlag;
static BOOL tostdoutFlag; static int tostdoutFlag;
static BOOL inHeader; static int inHeader;
static BOOL badHeader; static int badHeader;
static BOOL errorFlag; static int errorFlag;
static BOOL skipFileFlag; static int skipFileFlag;
static BOOL warnedRoot; static int warnedRoot;
static BOOL eofFlag; static int eofFlag;
static long dataCc; static long dataCc;
static int outFd; static int outFd;
static char outName[TAR_NAME_SIZE]; static char outName[TAR_NAME_SIZE];
@ -114,7 +114,7 @@ static void readHeader(const TarHeader * hp,
/* /*
* Local procedures to save files into a tar file. * Local procedures to save files into a tar file.
*/ */
static void saveFile(const char * fileName, BOOL seeLinks); static void saveFile(const char * fileName, int seeLinks);
static void saveRegularFile(const char * fileName, static void saveRegularFile(const char * fileName,
const struct stat * statbuf); const struct stat * statbuf);
@ -122,7 +122,7 @@ static void saveRegularFile(const char * fileName,
static void saveDirectory(const char * fileName, static void saveDirectory(const char * fileName,
const struct stat * statbuf); const struct stat * statbuf);
static BOOL wantFileName(const char * fileName, static int wantFileName(const char * fileName,
int fileCount, char ** fileTable); int fileCount, char ** fileTable);
static void writeHeader(const char * fileName, static void writeHeader(const char * fileName,
@ -130,7 +130,7 @@ static void writeHeader(const char * fileName,
static void writeTarFile(int fileCount, char ** fileTable); static void writeTarFile(int fileCount, char ** fileTable);
static void writeTarBlock(const char * buf, int len); static void writeTarBlock(const char * buf, int len);
static BOOL putOctal(char * cp, int len, long value); static int putOctal(char * cp, int len, long value);
extern int extern int
@ -383,8 +383,8 @@ readHeader(const TarHeader * hp, int fileCount, char ** fileTable)
time_t mtime; time_t mtime;
const char * name; const char * name;
int cc; int cc;
BOOL hardLink; int hardLink;
BOOL softLink; int softLink;
/* /*
* If the block is completely empty, then this is the end of the * If the block is completely empty, then this is the end of the
@ -710,7 +710,7 @@ done:
* they really are, instead of blindly following them. * they really are, instead of blindly following them.
*/ */
static void static void
saveFile(const char * fileName, BOOL seeLinks) saveFile(const char * fileName, int seeLinks)
{ {
int status; int status;
int mode; int mode;
@ -788,7 +788,7 @@ saveFile(const char * fileName, BOOL seeLinks)
static void static void
saveRegularFile(const char * fileName, const struct stat * statbuf) saveRegularFile(const char * fileName, const struct stat * statbuf)
{ {
BOOL sawEof; int sawEof;
int fileFd; int fileFd;
int cc; int cc;
int dataCount; int dataCount;
@ -897,7 +897,7 @@ saveDirectory(const char * dirName, const struct stat * statbuf)
{ {
DIR * dir; DIR * dir;
struct dirent * entry; struct dirent * entry;
BOOL needSlash; int needSlash;
char fullName[PATH_LEN]; char fullName[PATH_LEN];
/* /*
@ -1160,7 +1160,7 @@ getOctal(const char * cp, int len)
* The number is zero and space padded and possibly null padded. * The number is zero and space padded and possibly null padded.
* Returns TRUE if successful. * Returns TRUE if successful.
*/ */
static BOOL static int
putOctal(char * cp, int len, long value) putOctal(char * cp, int len, long value)
{ {
int tempLength; int tempLength;
@ -1212,7 +1212,7 @@ putOctal(char * cp, int len, long value)
* of path prefixes. An empty list implies that all files are wanted. * of path prefixes. An empty list implies that all files are wanted.
* Returns TRUE if the file is selected. * Returns TRUE if the file is selected.
*/ */
static BOOL static int
wantFileName(const char * fileName, int fileCount, char ** fileTable) wantFileName(const char * fileName, int fileCount, char ** fileTable)
{ {
const char * pathName; const char * pathName;

View File

@ -30,7 +30,7 @@ static const struct Applet applets[] = {
{"clear", clear_main}, {"clear", clear_main},
#endif #endif
#ifdef BB_CP //bin #ifdef BB_CP //bin
{"cp", dyadic_main}, {"cp", cp_main},
#endif #endif
#ifdef BB_DATE //bin #ifdef BB_DATE //bin
{"date", date_main}, {"date", date_main},

View File

@ -10,7 +10,7 @@
#define BB_CHOWN #define BB_CHOWN
#define BB_CHROOT #define BB_CHROOT
#define BB_CLEAR #define BB_CLEAR
//#define BB_CP #define BB_CP
#define BB_DATE #define BB_DATE
#define BB_DD #define BB_DD
//#define BB_DESCEND //#define BB_DESCEND
@ -54,7 +54,7 @@
#define BB_TAR #define BB_TAR
#define BB_TOUCH #define BB_TOUCH
#define BB_TRUE #define BB_TRUE
//#define BB_UMOUNT #define BB_UMOUNT
//#define BB_UPDATE //#define BB_UPDATE
#define BB_UTILITY #define BB_UTILITY
#define BB_ZCAT #define BB_ZCAT

View File

@ -1,89 +1,141 @@
/*
* Mini cp implementation for busybox
*
* Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <time.h>
#include <sys/fcntl.h> #include <utime.h>
#include <sys/param.h> #include <dirent.h>
#include <errno.h>
const char cp_usage[] = "cp [-r] source-file destination-file\n" const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n"
"\t\tcp [-r] source-file [source-file ...] destination-directory\n" " or: cp [OPTION]... SOURCE... DIRECTORY\n"
"\n" "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
"\tCopy the source files to the destination.\n" "\n"
"\n" "\t-a\tsame as -dpR\n"
"\t-r:\tRecursively copy all files and directories\n" "\t-d\tpreserve links\n"
"\t\tunder the argument directory."; "\t-p\tpreserve file attributes if possable\n"
"\t-R\tcopy directories recursively\n";
extern int
cp_fn(const struct FileInfo * i) static int recursiveFlag = FALSE;
static int followLinks = FALSE;
static int preserveFlag = FALSE;
static const char *srcName;
static const char *destName;
static int fileAction(const char *fileName)
{ {
int sourceFd; char newdestName[NAME_MAX];
int destinationFd; strcpy(newdestName, destName);
const char * destination = i->destination; strcat(newdestName, fileName+(strlen(srcName)));
struct stat destination_stat; fprintf(stderr, "A: copying %s to %s\n", fileName, newdestName);
int status; return (copyFile(fileName, newdestName, preserveFlag, followLinks));
char buf[8192]; }
char d[PATH_MAX];
static int dirAction(const char *fileName)
if ( (i->stat.st_mode & S_IFMT) == S_IFDIR ) { {
if ( mkdir(destination, i->stat.st_mode & ~S_IFMT) char newdestName[NAME_MAX];
!= 0 && errno != EEXIST ) { struct stat statBuf;
name_and_error(destination); struct utimbuf times;
return 1;
} strcpy(newdestName, destName);
return 0; strcat(newdestName, fileName+(strlen(srcName)));
} if (stat(newdestName, &statBuf)) {
if ( (sourceFd = open(i->source, O_RDONLY)) < 0 ) { if (mkdir( newdestName, 0777777 ^ umask (0))) {
name_and_error(i->source); perror(newdestName);
return 1; return( FALSE);
} }
if ( stat(destination, &destination_stat) == 0 ) { }
if ( i->stat.st_ino == destination_stat.st_ino else if (!S_ISDIR (statBuf.st_mode)) {
&& i->stat.st_dev == destination_stat.st_dev ) { fprintf(stderr, "`%s' exists but is not a directory", newdestName);
fprintf(stderr return( FALSE);
,"copy of %s to %s would copy file to itself.\n" }
,i->source if (preserveFlag==TRUE) {
,destination); /* Try to preserve premissions, but don't whine on failure */
close(sourceFd); if (stat(newdestName, &statBuf)) {
return 1; perror(newdestName);
} return( FALSE);
} }
/* chmod(newdestName, statBuf.st_mode);
* If the destination is a directory, create a file within it. chown(newdestName, statBuf.st_uid, statBuf.st_gid);
*/ times.actime = statBuf.st_atime;
if ( (destination_stat.st_mode & S_IFMT) == S_IFDIR ) { times.modtime = statBuf.st_mtime;
destination = join_paths( utime(newdestName, &times);
d }
,i->destination return TRUE;
,&i->source[i->directoryLength]); }
if ( stat(destination, &destination_stat) == 0 ) { extern int cp_main(int argc, char **argv)
if ( i->stat.st_ino == destination_stat.st_ino {
&& i->stat.st_dev == destination_stat.st_dev ) {
fprintf(stderr int dirFlag;
,"copy of %s to %s would copy file to itself.\n"
,i->source if (argc < 3) {
,destination); fprintf(stderr, "Usage: %s", cp_usage);
close(sourceFd); return (FALSE);
return 1; }
} argc--;
} argv++;
}
/* Parse any options */
destinationFd = creat(destination, i->stat.st_mode & 07777); while (**argv == '-') {
while (*++(*argv))
while ( (status = read(sourceFd, buf, sizeof(buf))) > 0 ) { switch (**argv) {
if ( write(destinationFd, buf, status) != status ) { case 'a':
name_and_error(destination); followLinks = TRUE;
close(sourceFd); preserveFlag = TRUE;
close(destinationFd); recursiveFlag = TRUE;
return 1; break;
} case 'd':
} followLinks = TRUE;
close(sourceFd); break;
close(destinationFd); case 'p':
if ( status < 0 ) { preserveFlag = TRUE;
name_and_error(i->source); break;
return 1; case 'R':
} recursiveFlag = TRUE;
return 0; break;
default:
fprintf(stderr, "Usage: %s\n", cp_usage);
exit(FALSE);
}
argc--;
argv++;
}
destName = argv[argc - 1];
dirFlag = isDirectory(destName);
if ((argc > 3) && !dirFlag) {
fprintf(stderr, "%s: not a directory\n", destName);
return (FALSE);
}
while (argc-- >= 2) {
srcName = *(argv++);
return recursiveAction(srcName, recursiveFlag, followLinks,
fileAction, fileAction);
}
return( TRUE);
} }

View File

@ -37,7 +37,7 @@ extern int mv_main (int argc, char **argv)
const char *srcName; const char *srcName;
const char *destName; const char *destName;
const char *lastArg; const char *lastArg;
BOOL dirFlag; int dirFlag;
if (argc < 3) { if (argc < 3) {
fprintf (stderr, "Usage: %s %s", *argv, mv_usage); fprintf (stderr, "Usage: %s %s", *argv, mv_usage);
@ -63,7 +63,7 @@ extern int mv_main (int argc, char **argv)
destName = lastArg; destName = lastArg;
if (dirFlag) if (dirFlag==TRUE)
destName = buildName (destName, srcName); destName = buildName (destName, srcName);
if (rename (srcName, destName) >= 0) if (rename (srcName, destName) >= 0)
@ -74,7 +74,7 @@ extern int mv_main (int argc, char **argv)
continue; continue;
} }
if (!copyFile (srcName, destName, TRUE)) if (!copyFile (srcName, destName, TRUE, FALSE))
continue; continue;
if (unlink (srcName) < 0) if (unlink (srcName) < 0)

218
cp.c
View File

@ -1,89 +1,141 @@
/*
* Mini cp implementation for busybox
*
* Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <time.h>
#include <sys/fcntl.h> #include <utime.h>
#include <sys/param.h> #include <dirent.h>
#include <errno.h>
const char cp_usage[] = "cp [-r] source-file destination-file\n" const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n"
"\t\tcp [-r] source-file [source-file ...] destination-directory\n" " or: cp [OPTION]... SOURCE... DIRECTORY\n"
"\n" "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
"\tCopy the source files to the destination.\n" "\n"
"\n" "\t-a\tsame as -dpR\n"
"\t-r:\tRecursively copy all files and directories\n" "\t-d\tpreserve links\n"
"\t\tunder the argument directory."; "\t-p\tpreserve file attributes if possable\n"
"\t-R\tcopy directories recursively\n";
extern int
cp_fn(const struct FileInfo * i) static int recursiveFlag = FALSE;
static int followLinks = FALSE;
static int preserveFlag = FALSE;
static const char *srcName;
static const char *destName;
static int fileAction(const char *fileName)
{ {
int sourceFd; char newdestName[NAME_MAX];
int destinationFd; strcpy(newdestName, destName);
const char * destination = i->destination; strcat(newdestName, fileName+(strlen(srcName)));
struct stat destination_stat; fprintf(stderr, "A: copying %s to %s\n", fileName, newdestName);
int status; return (copyFile(fileName, newdestName, preserveFlag, followLinks));
char buf[8192]; }
char d[PATH_MAX];
static int dirAction(const char *fileName)
if ( (i->stat.st_mode & S_IFMT) == S_IFDIR ) { {
if ( mkdir(destination, i->stat.st_mode & ~S_IFMT) char newdestName[NAME_MAX];
!= 0 && errno != EEXIST ) { struct stat statBuf;
name_and_error(destination); struct utimbuf times;
return 1;
} strcpy(newdestName, destName);
return 0; strcat(newdestName, fileName+(strlen(srcName)));
} if (stat(newdestName, &statBuf)) {
if ( (sourceFd = open(i->source, O_RDONLY)) < 0 ) { if (mkdir( newdestName, 0777777 ^ umask (0))) {
name_and_error(i->source); perror(newdestName);
return 1; return( FALSE);
} }
if ( stat(destination, &destination_stat) == 0 ) { }
if ( i->stat.st_ino == destination_stat.st_ino else if (!S_ISDIR (statBuf.st_mode)) {
&& i->stat.st_dev == destination_stat.st_dev ) { fprintf(stderr, "`%s' exists but is not a directory", newdestName);
fprintf(stderr return( FALSE);
,"copy of %s to %s would copy file to itself.\n" }
,i->source if (preserveFlag==TRUE) {
,destination); /* Try to preserve premissions, but don't whine on failure */
close(sourceFd); if (stat(newdestName, &statBuf)) {
return 1; perror(newdestName);
} return( FALSE);
} }
/* chmod(newdestName, statBuf.st_mode);
* If the destination is a directory, create a file within it. chown(newdestName, statBuf.st_uid, statBuf.st_gid);
*/ times.actime = statBuf.st_atime;
if ( (destination_stat.st_mode & S_IFMT) == S_IFDIR ) { times.modtime = statBuf.st_mtime;
destination = join_paths( utime(newdestName, &times);
d }
,i->destination return TRUE;
,&i->source[i->directoryLength]); }
if ( stat(destination, &destination_stat) == 0 ) { extern int cp_main(int argc, char **argv)
if ( i->stat.st_ino == destination_stat.st_ino {
&& i->stat.st_dev == destination_stat.st_dev ) {
fprintf(stderr int dirFlag;
,"copy of %s to %s would copy file to itself.\n"
,i->source if (argc < 3) {
,destination); fprintf(stderr, "Usage: %s", cp_usage);
close(sourceFd); return (FALSE);
return 1; }
} argc--;
} argv++;
}
/* Parse any options */
destinationFd = creat(destination, i->stat.st_mode & 07777); while (**argv == '-') {
while (*++(*argv))
while ( (status = read(sourceFd, buf, sizeof(buf))) > 0 ) { switch (**argv) {
if ( write(destinationFd, buf, status) != status ) { case 'a':
name_and_error(destination); followLinks = TRUE;
close(sourceFd); preserveFlag = TRUE;
close(destinationFd); recursiveFlag = TRUE;
return 1; break;
} case 'd':
} followLinks = TRUE;
close(sourceFd); break;
close(destinationFd); case 'p':
if ( status < 0 ) { preserveFlag = TRUE;
name_and_error(i->source); break;
return 1; case 'R':
} recursiveFlag = TRUE;
return 0; break;
default:
fprintf(stderr, "Usage: %s\n", cp_usage);
exit(FALSE);
}
argc--;
argv++;
}
destName = argv[argc - 1];
dirFlag = isDirectory(destName);
if ((argc > 3) && !dirFlag) {
fprintf(stderr, "%s: not a directory\n", destName);
return (FALSE);
}
while (argc-- >= 2) {
srcName = *(argv++);
return recursiveAction(srcName, recursiveFlag, followLinks,
fileAction, fileAction);
}
return( TRUE);
} }

2
find.c
View File

@ -27,7 +27,7 @@
static char* pattern=NULL; static char* pattern=NULL;
static char* directory=NULL; static char* directory=NULL;
int dereferenceFlag=FALSE; static int dereferenceFlag=FALSE;
static const char find_usage[] = "find [path...] [expression]\n" static const char find_usage[] = "find [path...] [expression]\n"
"default path is the current directory; default expression is -print\n" "default path is the current directory; default expression is -print\n"

View File

@ -27,7 +27,7 @@
static char* pattern=NULL; static char* pattern=NULL;
static char* directory=NULL; static char* directory=NULL;
int dereferenceFlag=FALSE; static int dereferenceFlag=FALSE;
static const char find_usage[] = "find [path...] [expression]\n" static const char find_usage[] = "find [path...] [expression]\n"
"default path is the current directory; default expression is -print\n" "default path is the current directory; default expression is -print\n"

View File

@ -31,7 +31,7 @@ const char grep_usage[] =
static BOOL search (const char *string, const char *word, BOOL ignoreCase); static int search (const char *string, const char *word, int ignoreCase);
extern int grep_main (int argc, char **argv) extern int grep_main (int argc, char **argv)
@ -40,9 +40,9 @@ extern int grep_main (int argc, char **argv)
const char *word; const char *word;
const char *name; const char *name;
const char *cp; const char *cp;
BOOL tellName; int tellName;
BOOL ignoreCase; int ignoreCase;
BOOL tellLine; int tellLine;
long line; long line;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
@ -125,7 +125,7 @@ extern int grep_main (int argc, char **argv)
/* /*
* See if the specified word is found in the specified string. * See if the specified word is found in the specified string.
*/ */
static BOOL search (const char *string, const char *word, BOOL ignoreCase) static int search (const char *string, const char *word, int ignoreCase)
{ {
const char *cp1; const char *cp1;
const char *cp2; const char *cp2;

10
grep.c
View File

@ -31,7 +31,7 @@ const char grep_usage[] =
static BOOL search (const char *string, const char *word, BOOL ignoreCase); static int search (const char *string, const char *word, int ignoreCase);
extern int grep_main (int argc, char **argv) extern int grep_main (int argc, char **argv)
@ -40,9 +40,9 @@ extern int grep_main (int argc, char **argv)
const char *word; const char *word;
const char *name; const char *name;
const char *cp; const char *cp;
BOOL tellName; int tellName;
BOOL ignoreCase; int ignoreCase;
BOOL tellLine; int tellLine;
long line; long line;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
@ -125,7 +125,7 @@ extern int grep_main (int argc, char **argv)
/* /*
* See if the specified word is found in the specified string. * See if the specified word is found in the specified string.
*/ */
static BOOL search (const char *string, const char *word, BOOL ignoreCase) static int search (const char *string, const char *word, int ignoreCase)
{ {
const char *cp1; const char *cp1;
const char *cp2; const char *cp2;

View File

@ -33,11 +33,10 @@
/* Some useful definitions */ /* Some useful definitions */
typedef int BOOL;
#define STDIN 0 #define STDIN 0
#define STDOUT 1 #define STDOUT 1
#define FALSE ((BOOL) 1) #define FALSE ((int) 1)
#define TRUE ((BOOL) 0) #define TRUE ((int) 0)
#define PATH_LEN 1024 #define PATH_LEN 1024
#define BUF_SIZE 8192 #define BUF_SIZE 8192
@ -97,6 +96,7 @@ extern int busybox_main(int argc, char** argv);
extern int block_device_main(int argc, char** argv); extern int block_device_main(int argc, char** argv);
extern int cat_more_main(int argc, char** argv); extern int cat_more_main(int argc, char** argv);
extern int more_main(int argc, char** argv); extern int more_main(int argc, char** argv);
extern int cp_main(int argc, char** argv);
extern int chgrp_main(int argc, char** argv); extern int chgrp_main(int argc, char** argv);
extern int chmod_main(int argc, char** argv); extern int chmod_main(int argc, char** argv);
extern int chown_main(int argc, char** argv); extern int chown_main(int argc, char** argv);
@ -172,20 +172,21 @@ struct chunk {
const char *modeString(int mode); const char *modeString(int mode);
const char *timeString(time_t timeVal); const char *timeString(time_t timeVal);
BOOL isDirectory(const char *name); int isDirectory(const char *name);
BOOL isDevice(const char *name); int isDevice(const char *name);
BOOL copyFile(const char *srcName, const char *destName, BOOL setModes); int copyFile(const char *srcName, const char *destName, int setModes,
const char *buildName(const char *dirName, const char *fileName); int followLinks);
BOOL makeString(int argc, const char **argv, char *buf, int bufLen); char *buildName(const char *dirName, const char *fileName);
int makeString(int argc, const char **argv, char *buf, int bufLen);
char *getChunk(int size); char *getChunk(int size);
char *chunkstrdup(const char *str); char *chunkstrdup(const char *str);
void freeChunks(void); void freeChunks(void);
int fullWrite(int fd, const char *buf, int len); int fullWrite(int fd, const char *buf, int len);
int fullRead(int fd, char *buf, int len); int fullRead(int fd, char *buf, int len);
int recursiveAction(const char *fileName, BOOL recurse, BOOL followLinks, int recursiveAction(const char *fileName, int recurse, int followLinks,
int (*fileAction) (const char *fileName), int (*fileAction) (const char *fileName),
int (*dirAction) (const char *fileName)); int (*dirAction) (const char *fileName));
BOOL match(const char* text, const char * pattern); int match(const char* text, const char * pattern);
const char* timeString(time_t timeVal); const char* timeString(time_t timeVal);

6
mv.c
View File

@ -37,7 +37,7 @@ extern int mv_main (int argc, char **argv)
const char *srcName; const char *srcName;
const char *destName; const char *destName;
const char *lastArg; const char *lastArg;
BOOL dirFlag; int dirFlag;
if (argc < 3) { if (argc < 3) {
fprintf (stderr, "Usage: %s %s", *argv, mv_usage); fprintf (stderr, "Usage: %s %s", *argv, mv_usage);
@ -63,7 +63,7 @@ extern int mv_main (int argc, char **argv)
destName = lastArg; destName = lastArg;
if (dirFlag) if (dirFlag==TRUE)
destName = buildName (destName, srcName); destName = buildName (destName, srcName);
if (rename (srcName, destName) >= 0) if (rename (srcName, destName) >= 0)
@ -74,7 +74,7 @@ extern int mv_main (int argc, char **argv)
continue; continue;
} }
if (!copyFile (srcName, destName, TRUE)) if (!copyFile (srcName, destName, TRUE, FALSE))
continue; continue;
if (unlink (srcName) < 0) if (unlink (srcName) < 0)

42
tar.c
View File

@ -73,18 +73,18 @@ typedef struct
/* /*
* Static data. * Static data.
*/ */
static BOOL listFlag; static int listFlag;
static BOOL extractFlag; static int extractFlag;
static BOOL createFlag; static int createFlag;
static BOOL verboseFlag; static int verboseFlag;
static BOOL tostdoutFlag; static int tostdoutFlag;
static BOOL inHeader; static int inHeader;
static BOOL badHeader; static int badHeader;
static BOOL errorFlag; static int errorFlag;
static BOOL skipFileFlag; static int skipFileFlag;
static BOOL warnedRoot; static int warnedRoot;
static BOOL eofFlag; static int eofFlag;
static long dataCc; static long dataCc;
static int outFd; static int outFd;
static char outName[TAR_NAME_SIZE]; static char outName[TAR_NAME_SIZE];
@ -114,7 +114,7 @@ static void readHeader(const TarHeader * hp,
/* /*
* Local procedures to save files into a tar file. * Local procedures to save files into a tar file.
*/ */
static void saveFile(const char * fileName, BOOL seeLinks); static void saveFile(const char * fileName, int seeLinks);
static void saveRegularFile(const char * fileName, static void saveRegularFile(const char * fileName,
const struct stat * statbuf); const struct stat * statbuf);
@ -122,7 +122,7 @@ static void saveRegularFile(const char * fileName,
static void saveDirectory(const char * fileName, static void saveDirectory(const char * fileName,
const struct stat * statbuf); const struct stat * statbuf);
static BOOL wantFileName(const char * fileName, static int wantFileName(const char * fileName,
int fileCount, char ** fileTable); int fileCount, char ** fileTable);
static void writeHeader(const char * fileName, static void writeHeader(const char * fileName,
@ -130,7 +130,7 @@ static void writeHeader(const char * fileName,
static void writeTarFile(int fileCount, char ** fileTable); static void writeTarFile(int fileCount, char ** fileTable);
static void writeTarBlock(const char * buf, int len); static void writeTarBlock(const char * buf, int len);
static BOOL putOctal(char * cp, int len, long value); static int putOctal(char * cp, int len, long value);
extern int extern int
@ -383,8 +383,8 @@ readHeader(const TarHeader * hp, int fileCount, char ** fileTable)
time_t mtime; time_t mtime;
const char * name; const char * name;
int cc; int cc;
BOOL hardLink; int hardLink;
BOOL softLink; int softLink;
/* /*
* If the block is completely empty, then this is the end of the * If the block is completely empty, then this is the end of the
@ -710,7 +710,7 @@ done:
* they really are, instead of blindly following them. * they really are, instead of blindly following them.
*/ */
static void static void
saveFile(const char * fileName, BOOL seeLinks) saveFile(const char * fileName, int seeLinks)
{ {
int status; int status;
int mode; int mode;
@ -788,7 +788,7 @@ saveFile(const char * fileName, BOOL seeLinks)
static void static void
saveRegularFile(const char * fileName, const struct stat * statbuf) saveRegularFile(const char * fileName, const struct stat * statbuf)
{ {
BOOL sawEof; int sawEof;
int fileFd; int fileFd;
int cc; int cc;
int dataCount; int dataCount;
@ -897,7 +897,7 @@ saveDirectory(const char * dirName, const struct stat * statbuf)
{ {
DIR * dir; DIR * dir;
struct dirent * entry; struct dirent * entry;
BOOL needSlash; int needSlash;
char fullName[PATH_LEN]; char fullName[PATH_LEN];
/* /*
@ -1160,7 +1160,7 @@ getOctal(const char * cp, int len)
* The number is zero and space padded and possibly null padded. * The number is zero and space padded and possibly null padded.
* Returns TRUE if successful. * Returns TRUE if successful.
*/ */
static BOOL static int
putOctal(char * cp, int len, long value) putOctal(char * cp, int len, long value)
{ {
int tempLength; int tempLength;
@ -1212,7 +1212,7 @@ putOctal(char * cp, int len, long value)
* of path prefixes. An empty list implies that all files are wanted. * of path prefixes. An empty list implies that all files are wanted.
* Returns TRUE if the file is selected. * Returns TRUE if the file is selected.
*/ */
static BOOL static int
wantFileName(const char * fileName, int fileCount, char ** fileTable) wantFileName(const char * fileName, int fileCount, char ** fileTable)
{ {
const char * pathName; const char * pathName;

198
umount.c
View File

@ -1,135 +1,95 @@
/*
* Mini umount implementation for busybox
*
* Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "internal.h" #include "internal.h"
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <mntent.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <mntent.h>
#include <fstab.h>
#include <errno.h>
const char umount_usage[] = "umount {filesystem|directory}\n" const char umount_usage[] = "\tumount {filesystem|directory}\n"
"\tumount -a\n" "or to unmount all mounted file systems:\n\tumount -a\n";
"\n"
"\tUnmount a filesystem.\n"
"\t-a:\tUnmounts all mounted filesystems.\n";
static char *
stralloc(const char * string)
{
int length = strlen(string) + 1;
char * n = malloc(length);
memcpy(n, string, length);
return n;
}
extern void
erase_mtab(const char * name)
{
struct mntent entries[100];
int count = 0;
FILE * mountTable = setmntent("/etc/mtab", "r");
struct mntent * m;
if ( mountTable == 0
&& (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
name_and_error("/etc/mtab");
return;
}
while ( (m = getmntent(mountTable)) != 0 ) {
entries[count].mnt_fsname = stralloc(m->mnt_fsname);
entries[count].mnt_dir = stralloc(m->mnt_dir);
entries[count].mnt_type = stralloc(m->mnt_type);
entries[count].mnt_opts = stralloc(m->mnt_opts);
entries[count].mnt_freq = m->mnt_freq;
entries[count].mnt_passno = m->mnt_passno;
count++;
}
endmntent(mountTable);
if ( (mountTable = setmntent("/etc/mtab", "w")) ) {
int i;
for ( i = 0; i < count; i++ ) {
int result = ( strcmp(entries[i].mnt_fsname, name) == 0
|| strcmp(entries[i].mnt_dir, name) == 0 );
if ( result )
continue;
else
addmntent(mountTable, &entries[i]);
}
endmntent(mountTable);
}
else if ( errno != EROFS )
name_and_error("/etc/mtab");
}
static int static int
umount_all(int noMtab) umount_all()
{ {
struct mntent entries[100]; int status;
int count = 0; struct mntent *m;
FILE * mountTable = setmntent("/etc/mtab", "r"); FILE *mountTable;
struct mntent * m;
int status = 0;
if ( mountTable == 0 if ((mountTable = setmntent ("/proc/mounts", "r"))) {
&& (mountTable = setmntent("/proc/mounts", "r")) == 0 ) { while ((m = getmntent (mountTable)) != 0) {
name_and_error("/etc/mtab"); char *blockDevice = m->mnt_fsname;
return 1; if (strcmp (blockDevice, "/dev/root") == 0)
} blockDevice = (getfsfile ("/"))->fs_spec;
status=umount (m->mnt_dir);
while ( (m = getmntent(mountTable)) != 0 ) { if (status!=0) {
entries[count].mnt_fsname = stralloc(m->mnt_fsname); /* Don't bother retrying the umount on busy devices */
count++; if (errno==EBUSY) {
} perror(m->mnt_dir);
endmntent(mountTable); continue;
}
while ( count > 0 ) { printf ("Trying to umount %s failed: %s\n",
int result = umount(entries[--count].mnt_fsname) == 0; m->mnt_dir, strerror(errno));
/* free(entries[count].mnt_fsname); */ printf ("Instead trying to umount %s\n", blockDevice);
if ( result ) { status=umount (blockDevice);
if ( !noMtab ) if (status!=0) {
erase_mtab(entries[count].mnt_fsname); printf ("Couldn't umount %s on %s (type %s): %s\n",
blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
}
} }
else { }
status = 1; endmntent (mountTable);
name_and_error(entries[count].mnt_fsname); }
} return( TRUE);
}
return status;
} }
extern int extern int
do_umount(const char * name, int noMtab) umount_main(int argc, char * * argv)
{ {
if ( umount(name) == 0 ) {
if ( !noMtab ) if (argc < 2) {
erase_mtab(name); fprintf(stderr, "Usage: %s", umount_usage);
return 0; return(FALSE);
}
argc--;
argv++;
/* Parse any options */
while (**argv == '-') {
while (*++(*argv)) switch (**argv) {
case 'a':
return umount_all();
break;
default:
fprintf(stderr, "Usage: %s\n", umount_usage);
exit( FALSE);
} }
return 1; }
if ( umount(*argv) == 0 )
return (TRUE);
else {
perror("umount");
return( FALSE);
}
} }
extern int
umount_main(struct FileInfo * i, int argc, char * * argv)
{
int noMtab = 0;
if ( argv[1][0] == '-' ) {
switch ( argv[1][1] ) {
case 'a':
return umount_all(noMtab);
case 'n':
noMtab = 1;
break;
default:
usage(umount_usage);
return 1;
}
}
if ( do_umount(argv[1],noMtab) != 0 ) {
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
return 1;
}
return 0;
}

View File

@ -1,135 +1,95 @@
/*
* Mini umount implementation for busybox
*
* Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "internal.h" #include "internal.h"
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <mntent.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <mntent.h>
#include <fstab.h>
#include <errno.h>
const char umount_usage[] = "umount {filesystem|directory}\n" const char umount_usage[] = "\tumount {filesystem|directory}\n"
"\tumount -a\n" "or to unmount all mounted file systems:\n\tumount -a\n";
"\n"
"\tUnmount a filesystem.\n"
"\t-a:\tUnmounts all mounted filesystems.\n";
static char *
stralloc(const char * string)
{
int length = strlen(string) + 1;
char * n = malloc(length);
memcpy(n, string, length);
return n;
}
extern void
erase_mtab(const char * name)
{
struct mntent entries[100];
int count = 0;
FILE * mountTable = setmntent("/etc/mtab", "r");
struct mntent * m;
if ( mountTable == 0
&& (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
name_and_error("/etc/mtab");
return;
}
while ( (m = getmntent(mountTable)) != 0 ) {
entries[count].mnt_fsname = stralloc(m->mnt_fsname);
entries[count].mnt_dir = stralloc(m->mnt_dir);
entries[count].mnt_type = stralloc(m->mnt_type);
entries[count].mnt_opts = stralloc(m->mnt_opts);
entries[count].mnt_freq = m->mnt_freq;
entries[count].mnt_passno = m->mnt_passno;
count++;
}
endmntent(mountTable);
if ( (mountTable = setmntent("/etc/mtab", "w")) ) {
int i;
for ( i = 0; i < count; i++ ) {
int result = ( strcmp(entries[i].mnt_fsname, name) == 0
|| strcmp(entries[i].mnt_dir, name) == 0 );
if ( result )
continue;
else
addmntent(mountTable, &entries[i]);
}
endmntent(mountTable);
}
else if ( errno != EROFS )
name_and_error("/etc/mtab");
}
static int static int
umount_all(int noMtab) umount_all()
{ {
struct mntent entries[100]; int status;
int count = 0; struct mntent *m;
FILE * mountTable = setmntent("/etc/mtab", "r"); FILE *mountTable;
struct mntent * m;
int status = 0;
if ( mountTable == 0 if ((mountTable = setmntent ("/proc/mounts", "r"))) {
&& (mountTable = setmntent("/proc/mounts", "r")) == 0 ) { while ((m = getmntent (mountTable)) != 0) {
name_and_error("/etc/mtab"); char *blockDevice = m->mnt_fsname;
return 1; if (strcmp (blockDevice, "/dev/root") == 0)
} blockDevice = (getfsfile ("/"))->fs_spec;
status=umount (m->mnt_dir);
while ( (m = getmntent(mountTable)) != 0 ) { if (status!=0) {
entries[count].mnt_fsname = stralloc(m->mnt_fsname); /* Don't bother retrying the umount on busy devices */
count++; if (errno==EBUSY) {
} perror(m->mnt_dir);
endmntent(mountTable); continue;
}
while ( count > 0 ) { printf ("Trying to umount %s failed: %s\n",
int result = umount(entries[--count].mnt_fsname) == 0; m->mnt_dir, strerror(errno));
/* free(entries[count].mnt_fsname); */ printf ("Instead trying to umount %s\n", blockDevice);
if ( result ) { status=umount (blockDevice);
if ( !noMtab ) if (status!=0) {
erase_mtab(entries[count].mnt_fsname); printf ("Couldn't umount %s on %s (type %s): %s\n",
blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
}
} }
else { }
status = 1; endmntent (mountTable);
name_and_error(entries[count].mnt_fsname); }
} return( TRUE);
}
return status;
} }
extern int extern int
do_umount(const char * name, int noMtab) umount_main(int argc, char * * argv)
{ {
if ( umount(name) == 0 ) {
if ( !noMtab ) if (argc < 2) {
erase_mtab(name); fprintf(stderr, "Usage: %s", umount_usage);
return 0; return(FALSE);
}
argc--;
argv++;
/* Parse any options */
while (**argv == '-') {
while (*++(*argv)) switch (**argv) {
case 'a':
return umount_all();
break;
default:
fprintf(stderr, "Usage: %s\n", umount_usage);
exit( FALSE);
} }
return 1; }
if ( umount(*argv) == 0 )
return (TRUE);
else {
perror("umount");
return( FALSE);
}
} }
extern int
umount_main(struct FileInfo * i, int argc, char * * argv)
{
int noMtab = 0;
if ( argv[1][0] == '-' ) {
switch ( argv[1][1] ) {
case 'a':
return umount_all(noMtab);
case 'n':
noMtab = 1;
break;
default:
usage(umount_usage);
return 1;
}
}
if ( do_umount(argv[1],noMtab) != 0 ) {
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
return 1;
}
return 0;
}

775
utility.c
View File

@ -27,31 +27,30 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <time.h> #include <time.h>
#include <utime.h> #include <utime.h>
#include <sys/stat.h>
#include <unistd.h>
#if 0 #if 0
extern char * extern char *join_paths(char *buffer, const char *a, const char *b)
join_paths(char * buffer, const char * a, const char * b)
{ {
int length = 0; int length = 0;
if ( a && *a ) { if (a && *a) {
length = strlen(a); length = strlen(a);
memcpy(buffer, a, length); memcpy(buffer, a, length);
} }
if ( b && *b ) { if (b && *b) {
if ( length > 0 && buffer[length - 1] != '/' ) if (length > 0 && buffer[length - 1] != '/')
buffer[length++] = '/'; buffer[length++] = '/';
if ( *b == '/' ) if (*b == '/')
b++; b++;
strcpy(&buffer[length], b); strcpy(&buffer[length], b);
} }
return buffer; return buffer;
} }
#endif #endif
@ -61,73 +60,72 @@ join_paths(char * buffer, const char * a, const char * b)
static CHUNK * chunkList; static CHUNK *chunkList;
/* /*
* Return the standard ls-like mode string from a file mode. * Return the standard ls-like mode string from a file mode.
* This is static and so is overwritten on each call. * This is static and so is overwritten on each call.
*/ */
const char * const char *modeString(int mode)
modeString(int mode)
{ {
static char buf[12]; static char buf[12];
strcpy(buf, "----------"); strcpy(buf, "----------");
/* /*
* Fill in the file type. * Fill in the file type.
*/ */
if (S_ISDIR(mode)) if (S_ISDIR(mode))
buf[0] = 'd'; buf[0] = 'd';
if (S_ISCHR(mode)) if (S_ISCHR(mode))
buf[0] = 'c'; buf[0] = 'c';
if (S_ISBLK(mode)) if (S_ISBLK(mode))
buf[0] = 'b'; buf[0] = 'b';
if (S_ISFIFO(mode)) if (S_ISFIFO(mode))
buf[0] = 'p'; buf[0] = 'p';
#ifdef S_ISLNK #ifdef S_ISLNK
if (S_ISLNK(mode)) if (S_ISLNK(mode))
buf[0] = 'l'; buf[0] = 'l';
#endif #endif
#ifdef S_ISSOCK #ifdef S_ISSOCK
if (S_ISSOCK(mode)) if (S_ISSOCK(mode))
buf[0] = 's'; buf[0] = 's';
#endif #endif
/* /*
* Now fill in the normal file permissions. * Now fill in the normal file permissions.
*/ */
if (mode & S_IRUSR) if (mode & S_IRUSR)
buf[1] = 'r'; buf[1] = 'r';
if (mode & S_IWUSR) if (mode & S_IWUSR)
buf[2] = 'w'; buf[2] = 'w';
if (mode & S_IXUSR) if (mode & S_IXUSR)
buf[3] = 'x'; buf[3] = 'x';
if (mode & S_IRGRP) if (mode & S_IRGRP)
buf[4] = 'r'; buf[4] = 'r';
if (mode & S_IWGRP) if (mode & S_IWGRP)
buf[5] = 'w'; buf[5] = 'w';
if (mode & S_IXGRP) if (mode & S_IXGRP)
buf[6] = 'x'; buf[6] = 'x';
if (mode & S_IROTH) if (mode & S_IROTH)
buf[7] = 'r'; buf[7] = 'r';
if (mode & S_IWOTH) if (mode & S_IWOTH)
buf[8] = 'w'; buf[8] = 'w';
if (mode & S_IXOTH) if (mode & S_IXOTH)
buf[9] = 'x'; buf[9] = 'x';
/* /*
* Finally fill in magic stuff like suid and sticky text. * Finally fill in magic stuff like suid and sticky text.
*/ */
if (mode & S_ISUID) if (mode & S_ISUID)
buf[3] = ((mode & S_IXUSR) ? 's' : 'S'); buf[3] = ((mode & S_IXUSR) ? 's' : 'S');
if (mode & S_ISGID) if (mode & S_ISGID)
buf[6] = ((mode & S_IXGRP) ? 's' : 'S'); buf[6] = ((mode & S_IXGRP) ? 's' : 'S');
if (mode & S_ISVTX) if (mode & S_ISVTX)
buf[9] = ((mode & S_IXOTH) ? 't' : 'T'); buf[9] = ((mode & S_IXOTH) ? 't' : 'T');
return buf; return buf;
} }
@ -135,15 +133,14 @@ modeString(int mode)
* Return TRUE if a fileName is a directory. * Return TRUE if a fileName is a directory.
* Nonexistant files return FALSE. * Nonexistant files return FALSE.
*/ */
BOOL int isDirectory(const char *name)
isDirectory(const char * name)
{ {
struct stat statBuf; struct stat statBuf;
if (stat(name, &statBuf) < 0) if (stat(name, &statBuf) < 0)
return FALSE; return FALSE;
return S_ISDIR(statBuf.st_mode); return S_ISDIR(statBuf.st_mode);
} }
@ -151,15 +148,14 @@ isDirectory(const char * name)
* Return TRUE if a filename is a block or character device. * Return TRUE if a filename is a block or character device.
* Nonexistant files return FALSE. * Nonexistant files return FALSE.
*/ */
BOOL int isDevice(const char *name)
isDevice(const char * name)
{ {
struct stat statBuf; struct stat statBuf;
if (stat(name, &statBuf) < 0) if (stat(name, &statBuf) < 0)
return FALSE; return FALSE;
return S_ISBLK(statBuf.st_mode) || S_ISCHR(statBuf.st_mode); return S_ISBLK(statBuf.st_mode) || S_ISCHR(statBuf.st_mode);
} }
@ -169,102 +165,118 @@ isDevice(const char * name)
* error message output. (Failure is not indicted if the attributes cannot * error message output. (Failure is not indicted if the attributes cannot
* be set.) * be set.)
*/ */
BOOL int
copyFile( copyFile(
const char * srcName, const char *srcName,
const char * destName, const char *destName, int setModes, int followLinks)
BOOL setModes
)
{ {
int rfd; int rfd;
int wfd; int wfd;
int rcc; int rcc;
char buf[BUF_SIZE]; char buf[BUF_SIZE];
struct stat statBuf1; struct stat statBuf1;
struct stat statBuf2; struct stat statBuf2;
struct utimbuf times; struct utimbuf times;
if (stat(srcName, &statBuf1) < 0)
{
perror(srcName);
return FALSE; if (stat(srcName, &statBuf1) < 0) {
perror(srcName);
return FALSE;
}
if (stat(destName, &statBuf2) < 0) {
statBuf2.st_ino = -1;
statBuf2.st_dev = -1;
}
if ((statBuf1.st_dev == statBuf2.st_dev) &&
(statBuf1.st_ino == statBuf2.st_ino)) {
fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
return FALSE;
}
if (S_ISDIR(statBuf1.st_mode)) {
/* Make sure the directory is writable */
if (mkdir(destName, 0777777 ^ umask(0))) {
perror(destName);
return (FALSE);
} }
} else if (S_ISFIFO(statBuf1.st_mode)) {
if (stat(destName, &statBuf2) < 0) if (mkfifo(destName, 644)) {
{ perror(destName);
statBuf2.st_ino = -1; return (FALSE);
statBuf2.st_dev = -1;
} }
} else if (S_ISBLK(statBuf1.st_mode) || S_ISCHR(statBuf1.st_mode)) {
if ((statBuf1.st_dev == statBuf2.st_dev) && if (mknod(destName, 644, statBuf1.st_rdev)) {
(statBuf1.st_ino == statBuf2.st_ino)) perror(destName);
{ return (FALSE);
fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
return FALSE;
} }
} else if (S_ISLNK(statBuf1.st_mode)) {
char *link_val;
int link_size;
link_val = (char *) alloca(PATH_MAX + 2);
link_size = readlink(srcName, link_val, PATH_MAX + 1);
if (link_size < 0) {
perror(srcName);
return (FALSE);
}
link_val[link_size] = '\0';
if (symlink(link_val, destName)) {
perror(srcName);
return (FALSE);
}
} else {
rfd = open(srcName, O_RDONLY); rfd = open(srcName, O_RDONLY);
if (rfd < 0) {
if (rfd < 0) perror(srcName);
{ return FALSE;
perror(srcName);
return FALSE;
} }
wfd = creat(destName, statBuf1.st_mode); wfd = creat(destName, statBuf1.st_mode);
if (wfd < 0) {
if (wfd < 0) perror(destName);
{ close(rfd);
perror(destName); return FALSE;
close(rfd);
return FALSE;
} }
while ((rcc = read(rfd, buf, sizeof(buf))) > 0) while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
{ if (fullWrite(wfd, buf, rcc) < 0)
if (fullWrite(wfd, buf, rcc) < 0)
goto error_exit;
}
if (rcc < 0)
{
perror(srcName);
goto error_exit; goto error_exit;
} }
if (rcc < 0) {
(void) close(rfd); perror(srcName);
goto error_exit;
if (close(wfd) < 0)
{
perror(destName);
return FALSE;
} }
if (setModes)
{
(void) chmod(destName, statBuf1.st_mode);
(void) chown(destName, statBuf1.st_uid, statBuf1.st_gid);
times.actime = statBuf1.st_atime;
times.modtime = statBuf1.st_mtime;
(void) utime(destName, &times);
}
return TRUE;
error_exit:
close(rfd); close(rfd);
close(wfd);
return FALSE; if (close(wfd) < 0) {
perror(destName);
return FALSE;
}
}
if (setModes == TRUE) {
chmod(destName, statBuf1.st_mode);
if (followLinks == TRUE)
chown(destName, statBuf1.st_uid, statBuf1.st_gid);
else
lchown(destName, statBuf1.st_uid, statBuf1.st_gid);
times.actime = statBuf1.st_atime;
times.modtime = statBuf1.st_mtime;
utime(destName, &times);
}
return TRUE;
error_exit:
close(rfd);
close(wfd);
return FALSE;
} }
@ -273,25 +285,25 @@ error_exit:
* If the directory name is NULL, then the original fileName is returned. * If the directory name is NULL, then the original fileName is returned.
* The built path is in a static area, and is overwritten for each call. * The built path is in a static area, and is overwritten for each call.
*/ */
const char * char *buildName(const char *dirName, const char *fileName)
buildName(const char * dirName, const char * fileName)
{ {
const char * cp; const char *cp;
static char buf[PATH_LEN]; static char buf[PATH_LEN];
if ((dirName == NULL) || (*dirName == '\0'))
return fileName;
cp = strrchr(fileName, '/');
if (cp)
fileName = cp + 1;
strcpy(buf, dirName);
strcat(buf, "/");
strcat(buf, fileName);
if ((dirName == NULL) || (*dirName == '\0')) {
strcpy(buf, fileName);
return buf; return buf;
}
cp = strrchr(fileName, '/');
if (cp)
fileName = cp + 1;
strcpy(buf, dirName);
strcat(buf, "/");
return buf;
} }
@ -302,41 +314,33 @@ buildName(const char * dirName, const char * fileName)
* with an error message given. This does not handle spaces within * with an error message given. This does not handle spaces within
* arguments correctly. * arguments correctly.
*/ */
BOOL int makeString( int argc, const char **argv, char *buf, int bufLen)
makeString(
int argc,
const char ** argv,
char * buf,
int bufLen
)
{ {
int len; int len;
while (argc-- > 0) while (argc-- > 0) {
{ len = strlen(*argv);
len = strlen(*argv);
if (len >= bufLen) if (len >= bufLen) {
{ fprintf(stderr, "Argument string too long\n");
fprintf(stderr, "Argument string too long\n");
return FALSE; return FALSE;
}
strcpy(buf, *argv++);
buf += len;
bufLen -= len;
if (argc)
*buf++ = ' ';
bufLen--;
} }
*buf = '\0'; strcpy(buf, *argv++);
return TRUE; buf += len;
bufLen -= len;
if (argc)
*buf++ = ' ';
bufLen--;
}
*buf = '\0';
return TRUE;
} }
@ -346,23 +350,22 @@ makeString(
* list of chunks which can be freed all at one time. You CAN NOT free * list of chunks which can be freed all at one time. You CAN NOT free
* an individual chunk. * an individual chunk.
*/ */
char * char *getChunk(int size)
getChunk(int size)
{ {
CHUNK * chunk; CHUNK *chunk;
if (size < CHUNK_INIT_SIZE) if (size < CHUNK_INIT_SIZE)
size = CHUNK_INIT_SIZE; size = CHUNK_INIT_SIZE;
chunk = (CHUNK *) malloc(size + sizeof(CHUNK) - CHUNK_INIT_SIZE); chunk = (CHUNK *) malloc(size + sizeof(CHUNK) - CHUNK_INIT_SIZE);
if (chunk == NULL) if (chunk == NULL)
return NULL; return NULL;
chunk->next = chunkList; chunk->next = chunkList;
chunkList = chunk; chunkList = chunk;
return chunk->data; return chunk->data;
} }
@ -371,19 +374,18 @@ getChunk(int size)
* The returned string cannot be individually freed, but can only be freed * The returned string cannot be individually freed, but can only be freed
* with other strings when freeChunks is called. Returns NULL on failure. * with other strings when freeChunks is called. Returns NULL on failure.
*/ */
char * char *chunkstrdup(const char *str)
chunkstrdup(const char * str)
{ {
int len; int len;
char * newStr; char *newStr;
len = strlen(str) + 1; len = strlen(str) + 1;
newStr = getChunk(len); newStr = getChunk(len);
if (newStr) if (newStr)
memcpy(newStr, str, len); memcpy(newStr, str, len);
return newStr; return newStr;
} }
@ -391,17 +393,15 @@ chunkstrdup(const char * str)
* Free all chunks of memory that had been allocated since the last * Free all chunks of memory that had been allocated since the last
* call to this routine. * call to this routine.
*/ */
void void freeChunks(void)
freeChunks(void)
{ {
CHUNK * chunk; CHUNK *chunk;
while (chunkList) while (chunkList) {
{ chunk = chunkList;
chunk = chunkList; chunkList = chunk->next;
chunkList = chunk->next; free((char *) chunk);
free((char *) chunk); }
}
} }
@ -411,27 +411,25 @@ freeChunks(void)
* The string is returned from a static buffer, and so is overwritten for * The string is returned from a static buffer, and so is overwritten for
* each call. * each call.
*/ */
const char * const char *timeString(time_t timeVal)
timeString(time_t timeVal)
{ {
time_t now; time_t now;
char * str; char *str;
static char buf[26]; static char buf[26];
time(&now); time(&now);
str = ctime(&timeVal); str = ctime(&timeVal);
strcpy(buf, &str[4]); strcpy(buf, &str[4]);
buf[12] = '\0'; buf[12] = '\0';
if ((timeVal > now) || (timeVal < now - 365*24*60*60L)) if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
{ strcpy(&buf[7], &str[20]);
strcpy(&buf[7], &str[20]); buf[11] = '\0';
buf[11] = '\0'; }
}
return buf; return buf;
} }
@ -445,88 +443,81 @@ timeString(time_t timeVal)
* \c quotes character c * \c quotes character c
* Adapted from code written by Ingo Wilken. * Adapted from code written by Ingo Wilken.
*/ */
BOOL int match(const char *text, const char *pattern)
match(const char * text, const char * pattern)
{ {
const char * retryPat; const char *retryPat;
const char * retryText; const char *retryText;
int ch; int ch;
BOOL found; int found;
retryPat = NULL; retryPat = NULL;
retryText = NULL; retryText = NULL;
while (*text || *pattern) while (*text || *pattern) {
{ ch = *pattern++;
ch = *pattern++;
switch (ch) switch (ch) {
{ case '*':
case '*': retryPat = pattern;
retryPat = pattern; retryText = text;
retryText = text; break;
break;
case '[': case '[':
found = FALSE; found = FALSE;
while ((ch = *pattern++) != ']') while ((ch = *pattern++) != ']') {
{ if (ch == '\\')
if (ch == '\\') ch = *pattern++;
ch = *pattern++;
if (ch == '\0') if (ch == '\0')
return FALSE; return FALSE;
if (*text == ch) if (*text == ch)
found = TRUE; found = TRUE;
} }
if (!found) if (!found) {
{ pattern = retryPat;
pattern = retryPat; text = ++retryText;
text = ++retryText; }
}
/* fall into next case */ /* fall into next case */
case '?': case '?':
if (*text++ == '\0') if (*text++ == '\0')
return FALSE; return FALSE;
break; break;
case '\\': case '\\':
ch = *pattern++; ch = *pattern++;
if (ch == '\0') if (ch == '\0')
return FALSE; return FALSE;
/* fall into next case */ /* fall into next case */
default: default:
if (*text == ch) if (*text == ch) {
{ if (*text)
if (*text) text++;
text++; break;
break; }
}
if (*text) if (*text) {
{ pattern = retryPat;
pattern = retryPat; text = ++retryText;
text = ++retryText; break;
break; }
}
return FALSE; return FALSE;
}
if (pattern == NULL)
return FALSE;
} }
return TRUE; if (pattern == NULL)
return FALSE;
}
return TRUE;
} }
@ -535,27 +526,25 @@ match(const char * text, const char * pattern)
* This does multiple writes as necessary. * This does multiple writes as necessary.
* Returns the amount written, or -1 on an error. * Returns the amount written, or -1 on an error.
*/ */
int int fullWrite(int fd, const char *buf, int len)
fullWrite(int fd, const char * buf, int len)
{ {
int cc; int cc;
int total; int total;
total = 0; total = 0;
while (len > 0) while (len > 0) {
{ cc = write(fd, buf, len);
cc = write(fd, buf, len);
if (cc < 0) if (cc < 0)
return -1; return -1;
buf += cc; buf += cc;
total+= cc; total += cc;
len -= cc; len -= cc;
} }
return total; return total;
} }
@ -565,30 +554,28 @@ fullWrite(int fd, const char * buf, int len)
* Returns the amount read, or -1 on an error. * Returns the amount read, or -1 on an error.
* A short read is returned on an end of file. * A short read is returned on an end of file.
*/ */
int int fullRead(int fd, char *buf, int len)
fullRead(int fd, char * buf, int len)
{ {
int cc; int cc;
int total; int total;
total = 0; total = 0;
while (len > 0) while (len > 0) {
{ cc = read(fd, buf, len);
cc = read(fd, buf, len);
if (cc < 0) if (cc < 0)
return -1; return -1;
if (cc == 0) if (cc == 0)
break; break;
buf += cc; buf += cc;
total+= cc; total += cc;
len -= cc; len -= cc;
} }
return total; return total;
} }
@ -598,84 +585,82 @@ fullRead(int fd, char * buf, int len)
* by the fileAction and dirAction function pointers). * by the fileAction and dirAction function pointers).
*/ */
int int
recursiveAction( const char *fileName, BOOL recurse, BOOL followLinks, recursiveAction(const char *fileName, int recurse, int followLinks,
int (*fileAction)(const char* fileName), int (*fileAction) (const char *fileName),
int (*dirAction)(const char* fileName)) int (*dirAction) (const char *fileName))
{ {
int status; int status;
struct stat statbuf; struct stat statbuf;
struct dirent* next; struct dirent *next;
if (!recurse && S_ISDIR(statbuf.st_mode)) {
if (dirAction==NULL)
return(TRUE);
else
return(dirAction(fileName));
} else {
if (fileAction==NULL)
return(TRUE);
else
return(fileAction(fileName));
}
if (followLinks) if (followLinks)
status = stat(fileName, &statbuf);
else
status = lstat(fileName, &statbuf); status = lstat(fileName, &statbuf);
else
status = stat(fileName, &statbuf);
if (status < 0) { if (status < 0) {
perror(fileName); perror(fileName);
return( FALSE); return (FALSE);
}
if (recurse == FALSE) {
if (S_ISDIR(statbuf.st_mode)) {
if (dirAction == NULL)
return (TRUE);
else
return (dirAction(fileName));
} else {
if (fileAction == NULL)
return (TRUE);
else
return (fileAction(fileName));
}
} }
if (S_ISDIR(statbuf.st_mode)) { if (S_ISDIR(statbuf.st_mode)) {
DIR *dir; DIR *dir;
fprintf(stderr, "Dir: %s\n", fileName);
dir = opendir(fileName); dir = opendir(fileName);
if (!dir) { if (!dir) {
perror(fileName); perror(fileName);
return(FALSE); return (FALSE);
} }
while ((next = readdir (dir)) != NULL) { if (dirAction != NULL) {
char nextFile[NAME_MAX]; status = dirAction(fileName);
if ( (strcmp(next->d_name, "..") == 0) || (strcmp(next->d_name, ".") == 0) ) { if (status == FALSE) {
continue; perror("cp");
} return (FALSE);
sprintf(nextFile, "%s/%s", fileName, next->d_name); }
status = recursiveAction(nextFile, TRUE, followLinks, fileAction, dirAction);
if (status < 0) {
closedir(dir);
return(FALSE);
}
} }
status = closedir (dir); while ((next = readdir(dir)) != NULL) {
char nextFile[NAME_MAX];
if ((strcmp(next->d_name, "..") == 0)
|| (strcmp(next->d_name, ".") == 0)) {
continue;
}
sprintf(nextFile, "%s/%s", fileName, next->d_name);
status =
recursiveAction(nextFile, TRUE, followLinks, fileAction,
dirAction);
if (status < 0) {
closedir(dir);
return (FALSE);
}
}
status = closedir(dir);
if (status < 0) { if (status < 0) {
perror(fileName); perror(fileName);
return( FALSE); return (FALSE);
} }
if (dirAction==NULL) } else {
return(TRUE); fprintf(stderr, "File: %s\n", fileName);
if (fileAction == NULL)
return (TRUE);
else else
return(dirAction(fileName)); return (fileAction(fileName));
}
else {
if (fileAction==NULL)
return(TRUE);
else
return(fileAction(fileName));
} }
return (TRUE);
} }
/* END CODE */ /* END CODE */