diff --git a/busybox.def.h b/busybox.def.h index 15c8edc43..10a55f409 100644 --- a/busybox.def.h +++ b/busybox.def.h @@ -39,12 +39,12 @@ //#define BB_PRINTF #define BB_PWD #define BB_REBOOT -//#define BB_RM +#define BB_RM #define BB_RMDIR #define BB_SLEEP ////#define BB_SWAPOFF //#define BB_SWAPON -//#define BB_SYNC +#define BB_SYNC #define BB_TAR #define BB_TOUCH #define BB_TRUE diff --git a/chmod_chown_chgrp.c b/chmod_chown_chgrp.c index f3d8ce207..27c02fe54 100644 --- a/chmod_chown_chgrp.c +++ b/chmod_chown_chgrp.c @@ -138,7 +138,7 @@ int chmod_chown_chgrp_main(int argc, char **argv) exit( FALSE); } while (argc-- > 1) { - if (recursiveAction( *(++argv), recursiveFlag, TRUE, fileAction, fileAction)==FALSE) + if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE) exit( FALSE); } exit(TRUE); diff --git a/coreutils/cp.c b/coreutils/cp.c index 4016fc760..c5e34d333 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -48,7 +48,7 @@ static int fileAction(const char *fileName, struct stat* statbuf) { char newdestName[NAME_MAX]; strcpy(newdestName, destName); - if (dirFlag==TRUE && newdestName[strlen(newdestName)-1]!= '/' ) { + if (dirFlag==TRUE) { strcat(newdestName, "/"); if ( skipName != NULL) strcat(newdestName, strstr(fileName, skipName)); @@ -104,10 +104,12 @@ extern int cp_main(int argc, char **argv) while (argc-- > 1) { srcName = *(argv++); skipName = strrchr(srcName, '/'); - if (skipName) skipName++; - if (recursiveAction(srcName, recursiveFlag, followLinks, - fileAction, fileAction) == FALSE) + if (skipName) + skipName++; + if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE, + fileAction, fileAction) == FALSE) { exit( FALSE); + } } exit( TRUE); } diff --git a/coreutils/ls.c b/coreutils/ls.c index 50b983e24..2e3d50e9f 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -68,7 +68,7 @@ static int dirAction(const char *fileName) exit(FALSE); } while ((entry = readdir(dir)) != NULL) { - recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, fileAction, dirAction); + recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, FALSE, fileAction, dirAction); } return( TRUE); } diff --git a/coreutils/mv.c b/coreutils/mv.c index 10a082210..2a7c8c1f3 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c @@ -21,63 +21,59 @@ #include "internal.h" #include -#include -#include -#include +#include #include -#include - -const char mv_usage[] = "source-file [source-file ...] destination-file\n" - "\n" "\tMove the source files to the destination.\n" "\n"; +#include +static const char mv_usage[] = "mv SOURCE DEST\n" +" or: mv SOURCE... DIRECTORY\n" +"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; -extern int mv_main (int argc, char **argv) + +static const char *srcName; +static const char *destName; +static const char *skipName; +static int dirFlag = FALSE; + + +extern int mv_main(int argc, char **argv) { - const char *srcName; - const char *destName; - const char *lastArg; - int dirFlag; + char newdestName[NAME_MAX]; if (argc < 3) { - fprintf (stderr, "Usage: %s %s", *argv, mv_usage); + fprintf(stderr, "Usage: %s", mv_usage); exit (FALSE); } - lastArg = argv[argc - 1]; + argc--; + argv++; - dirFlag = isDirectory (lastArg); + destName = argv[argc - 1]; + dirFlag = isDirectory(destName); - if ((argc > 3) && !dirFlag) { - fprintf (stderr, "%s: not a directory\n", lastArg); + if ((argc > 3) && dirFlag==FALSE) { + fprintf(stderr, "%s: not a directory\n", destName); exit (FALSE); } - while (argc-- > 2) { - srcName = *(++argv); - - if (access (srcName, 0) < 0) { - perror (srcName); - continue; + while (argc-- > 1) { + srcName = *(argv++); + skipName = strrchr(srcName, '/'); + if (skipName) + skipName++; + strcpy(newdestName, destName); + if (dirFlag==TRUE) { + strcat(newdestName, "/"); + if ( skipName != NULL) + strcat(newdestName, strstr(srcName, skipName)); } - - destName = lastArg; - - if (dirFlag==TRUE) - destName = buildName (destName, srcName); - - if (rename (srcName, destName) >= 0) - continue; - - if (errno != EXDEV) { - perror (destName); - continue; + if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) { + exit( FALSE); } - - if (!copyFile (srcName, destName, TRUE, FALSE)) - continue; - - if (unlink (srcName) < 0) + if (unlink (srcName) < 0) { perror (srcName); + exit( FALSE); + } } - exit (TRUE); + exit( TRUE); } diff --git a/coreutils/rm.c b/coreutils/rm.c index dc35b0297..477d3af59 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -1,30 +1,93 @@ +/* + * Mini rm implementation for busybox + * + * Copyright (C) 1998 by Erik Andersen + * + * 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 +#include +#include +#include +#include -const char rm_usage[] = "rm [-r] file [file ...]\n" -"\n" -"\tDelete files.\n" -"\n" -"\t-r:\tRecursively remove files and directories.\n"; +static const char* rm_usage = "Usage: rm [OPTION]... FILE...\n" +"Remove (unlink) the FILE(s).\n\n" +"\t-f\tremove existing destinations, never prompt\n" +"\t-r\tremove the contents of directories recursively\n"; -extern int -rm_main(struct FileInfo * i, int argc, char * * argv) + +static int recursiveFlag = FALSE; +static int forceFlag = FALSE; +static const char *srcName; + + +static int fileAction(const char *fileName, struct stat* statbuf) { - i->processDirectoriesAfterTheirContents = 1; - return monadic_main(i, argc, argv); + if (unlink( fileName) < 0 ) { + perror( fileName); + return ( FALSE); + } + return ( TRUE); } -extern int -rm_fn(const struct FileInfo * i) +static int dirAction(const char *fileName, struct stat* statbuf) { - if ( i->recursive - && !i->isSymbolicLink - && (i->stat.st_mode & S_IFMT) == S_IFDIR ) - return rmdir_fn(i); - else if ( unlink(i->source) != 0 && errno != ENOENT && !i->force ) { - name_and_error(i->source); - return 1; + if (rmdir( fileName) < 0 ) { + perror( fileName); + return ( FALSE); + } + return ( TRUE); +} + +extern int rm_main(int argc, char **argv) +{ + + if (argc < 2) { + fprintf(stderr, "Usage: %s", rm_usage); + exit (FALSE); + } + argc--; + argv++; + + /* Parse any options */ + while (**argv == '-') { + while (*++(*argv)) + switch (**argv) { + case 'r': + recursiveFlag = TRUE; + break; + case 'f': + forceFlag = TRUE; + break; + default: + fprintf(stderr, "Usage: %s\n", rm_usage); + exit(FALSE); + } + argc--; + argv++; + } + + while (argc-- > 0) { + srcName = *(argv++); + if (recursiveAction( srcName, recursiveFlag, TRUE, TRUE, + fileAction, dirAction) == FALSE) { + exit( FALSE); } - else - return 0; + } + exit( TRUE); } diff --git a/coreutils/sync.c b/coreutils/sync.c index 6fa5b380b..e7aa44585 100644 --- a/coreutils/sync.c +++ b/coreutils/sync.c @@ -1,11 +1,12 @@ #include "internal.h" -const char sync_usage[] = "sync\n" -"\n" -"\tWrite all buffered filesystem blocks to disk.\n"; - extern int -sync_main(struct FileInfo * i, int argc, char * * argv) +sync_main(int argc, char * * argv) { + if ( **(argv+1) == '-' ) { + fprintf(stderr, "Usage: sync\nWrite all buffered filesystem blocks to disk.\n"); + exit(FALSE); + } return sync(); } + diff --git a/cp.c b/cp.c index 4016fc760..c5e34d333 100644 --- a/cp.c +++ b/cp.c @@ -48,7 +48,7 @@ static int fileAction(const char *fileName, struct stat* statbuf) { char newdestName[NAME_MAX]; strcpy(newdestName, destName); - if (dirFlag==TRUE && newdestName[strlen(newdestName)-1]!= '/' ) { + if (dirFlag==TRUE) { strcat(newdestName, "/"); if ( skipName != NULL) strcat(newdestName, strstr(fileName, skipName)); @@ -104,10 +104,12 @@ extern int cp_main(int argc, char **argv) while (argc-- > 1) { srcName = *(argv++); skipName = strrchr(srcName, '/'); - if (skipName) skipName++; - if (recursiveAction(srcName, recursiveFlag, followLinks, - fileAction, fileAction) == FALSE) + if (skipName) + skipName++; + if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE, + fileAction, fileAction) == FALSE) { exit( FALSE); + } } exit( TRUE); } diff --git a/find.c b/find.c index c92202b86..1fb219496 100644 --- a/find.c +++ b/find.c @@ -62,7 +62,7 @@ static int dirAction(const char *fileName, struct stat* statbuf) while ((entry = readdir(dir)) != NULL) { char dirName[NAME_MAX]; sprintf(dirName, "%s/%s", fileName, entry->d_name); - recursiveAction( dirName, TRUE, dereferenceFlag, fileAction, dirAction); + recursiveAction( dirName, TRUE, dereferenceFlag, FALSE, fileAction, dirAction); } return( TRUE); } @@ -123,34 +123,3 @@ int find_main(int argc, char **argv) dirAction( directory, NULL); exit(TRUE); } - - - -#ifdef foobar - -#include "internal.h" -#include -#include - -const char find_usage[] = "find dir [pattern]\n" -"\n" -"\tFind files.\n"; - -extern int -find_main(struct FileInfo * i, int argc, char * * argv) -{ - i->recursive=1; - i->processDirectoriesAfterTheirContents=1; - return monadic_main(i, argc, argv); -} - -extern int -find_fn(const struct FileInfo * i) -{ - printf("%s\n",i->source); - - return(0); -} - - -#endif diff --git a/findutils/find.c b/findutils/find.c index c92202b86..1fb219496 100644 --- a/findutils/find.c +++ b/findutils/find.c @@ -62,7 +62,7 @@ static int dirAction(const char *fileName, struct stat* statbuf) while ((entry = readdir(dir)) != NULL) { char dirName[NAME_MAX]; sprintf(dirName, "%s/%s", fileName, entry->d_name); - recursiveAction( dirName, TRUE, dereferenceFlag, fileAction, dirAction); + recursiveAction( dirName, TRUE, dereferenceFlag, FALSE, fileAction, dirAction); } return( TRUE); } @@ -123,34 +123,3 @@ int find_main(int argc, char **argv) dirAction( directory, NULL); exit(TRUE); } - - - -#ifdef foobar - -#include "internal.h" -#include -#include - -const char find_usage[] = "find dir [pattern]\n" -"\n" -"\tFind files.\n"; - -extern int -find_main(struct FileInfo * i, int argc, char * * argv) -{ - i->recursive=1; - i->processDirectoriesAfterTheirContents=1; - return monadic_main(i, argc, argv); -} - -extern int -find_fn(const struct FileInfo * i) -{ - printf("%s\n",i->source); - - return(0); -} - - -#endif diff --git a/internal.h b/internal.h index 4650e9f41..a87653927 100644 --- a/internal.h +++ b/internal.h @@ -121,7 +121,7 @@ char *chunkstrdup(const char *str); void freeChunks(void); int fullWrite(int fd, const char *buf, int len); int fullRead(int fd, char *buf, int len); -int recursiveAction(const char *fileName, int recurse, int followLinks, +int recursiveAction(const char *fileName, int recurse, int followLinks, int delayDirAction, int (*fileAction) (const char *fileName, struct stat* statbuf), int (*dirAction) (const char *fileName, struct stat* statbuf)); int match(const char* text, const char * pattern); diff --git a/ls.c b/ls.c index 50b983e24..2e3d50e9f 100644 --- a/ls.c +++ b/ls.c @@ -68,7 +68,7 @@ static int dirAction(const char *fileName) exit(FALSE); } while ((entry = readdir(dir)) != NULL) { - recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, fileAction, dirAction); + recursiveAction( entry->d_name, recursiveFlag, dereferenceFlag, FALSE, fileAction, dirAction); } return( TRUE); } diff --git a/mv.c b/mv.c index 10a082210..2a7c8c1f3 100644 --- a/mv.c +++ b/mv.c @@ -21,63 +21,59 @@ #include "internal.h" #include -#include -#include -#include +#include #include -#include - -const char mv_usage[] = "source-file [source-file ...] destination-file\n" - "\n" "\tMove the source files to the destination.\n" "\n"; +#include +static const char mv_usage[] = "mv SOURCE DEST\n" +" or: mv SOURCE... DIRECTORY\n" +"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; -extern int mv_main (int argc, char **argv) + +static const char *srcName; +static const char *destName; +static const char *skipName; +static int dirFlag = FALSE; + + +extern int mv_main(int argc, char **argv) { - const char *srcName; - const char *destName; - const char *lastArg; - int dirFlag; + char newdestName[NAME_MAX]; if (argc < 3) { - fprintf (stderr, "Usage: %s %s", *argv, mv_usage); + fprintf(stderr, "Usage: %s", mv_usage); exit (FALSE); } - lastArg = argv[argc - 1]; + argc--; + argv++; - dirFlag = isDirectory (lastArg); + destName = argv[argc - 1]; + dirFlag = isDirectory(destName); - if ((argc > 3) && !dirFlag) { - fprintf (stderr, "%s: not a directory\n", lastArg); + if ((argc > 3) && dirFlag==FALSE) { + fprintf(stderr, "%s: not a directory\n", destName); exit (FALSE); } - while (argc-- > 2) { - srcName = *(++argv); - - if (access (srcName, 0) < 0) { - perror (srcName); - continue; + while (argc-- > 1) { + srcName = *(argv++); + skipName = strrchr(srcName, '/'); + if (skipName) + skipName++; + strcpy(newdestName, destName); + if (dirFlag==TRUE) { + strcat(newdestName, "/"); + if ( skipName != NULL) + strcat(newdestName, strstr(srcName, skipName)); } - - destName = lastArg; - - if (dirFlag==TRUE) - destName = buildName (destName, srcName); - - if (rename (srcName, destName) >= 0) - continue; - - if (errno != EXDEV) { - perror (destName); - continue; + if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) { + exit( FALSE); } - - if (!copyFile (srcName, destName, TRUE, FALSE)) - continue; - - if (unlink (srcName) < 0) + if (unlink (srcName) < 0) { perror (srcName); + exit( FALSE); + } } - exit (TRUE); + exit( TRUE); } diff --git a/rm.c b/rm.c index dc35b0297..477d3af59 100644 --- a/rm.c +++ b/rm.c @@ -1,30 +1,93 @@ +/* + * Mini rm implementation for busybox + * + * Copyright (C) 1998 by Erik Andersen + * + * 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 +#include +#include +#include +#include -const char rm_usage[] = "rm [-r] file [file ...]\n" -"\n" -"\tDelete files.\n" -"\n" -"\t-r:\tRecursively remove files and directories.\n"; +static const char* rm_usage = "Usage: rm [OPTION]... FILE...\n" +"Remove (unlink) the FILE(s).\n\n" +"\t-f\tremove existing destinations, never prompt\n" +"\t-r\tremove the contents of directories recursively\n"; -extern int -rm_main(struct FileInfo * i, int argc, char * * argv) + +static int recursiveFlag = FALSE; +static int forceFlag = FALSE; +static const char *srcName; + + +static int fileAction(const char *fileName, struct stat* statbuf) { - i->processDirectoriesAfterTheirContents = 1; - return monadic_main(i, argc, argv); + if (unlink( fileName) < 0 ) { + perror( fileName); + return ( FALSE); + } + return ( TRUE); } -extern int -rm_fn(const struct FileInfo * i) +static int dirAction(const char *fileName, struct stat* statbuf) { - if ( i->recursive - && !i->isSymbolicLink - && (i->stat.st_mode & S_IFMT) == S_IFDIR ) - return rmdir_fn(i); - else if ( unlink(i->source) != 0 && errno != ENOENT && !i->force ) { - name_and_error(i->source); - return 1; + if (rmdir( fileName) < 0 ) { + perror( fileName); + return ( FALSE); + } + return ( TRUE); +} + +extern int rm_main(int argc, char **argv) +{ + + if (argc < 2) { + fprintf(stderr, "Usage: %s", rm_usage); + exit (FALSE); + } + argc--; + argv++; + + /* Parse any options */ + while (**argv == '-') { + while (*++(*argv)) + switch (**argv) { + case 'r': + recursiveFlag = TRUE; + break; + case 'f': + forceFlag = TRUE; + break; + default: + fprintf(stderr, "Usage: %s\n", rm_usage); + exit(FALSE); + } + argc--; + argv++; + } + + while (argc-- > 0) { + srcName = *(argv++); + if (recursiveAction( srcName, recursiveFlag, TRUE, TRUE, + fileAction, dirAction) == FALSE) { + exit( FALSE); } - else - return 0; + } + exit( TRUE); } diff --git a/swapoff.c b/swapoff.c index 55124d0b8..29b9daedd 100644 --- a/swapoff.c +++ b/swapoff.c @@ -13,7 +13,7 @@ swapoff_fn(const struct FileInfo * i) { struct mntent entries[100]; int count = 0; - FILE * swapsTable = setmntent("/etc/swaps", "r"); + FILE * swapsTable = setmntent("/proc/swaps", "r"); struct mntent * m; if (!(swapoff(i->source))) { diff --git a/sync.c b/sync.c index 6fa5b380b..e7aa44585 100644 --- a/sync.c +++ b/sync.c @@ -1,11 +1,12 @@ #include "internal.h" -const char sync_usage[] = "sync\n" -"\n" -"\tWrite all buffered filesystem blocks to disk.\n"; - extern int -sync_main(struct FileInfo * i, int argc, char * * argv) +sync_main(int argc, char * * argv) { + if ( **(argv+1) == '-' ) { + fprintf(stderr, "Usage: sync\nWrite all buffered filesystem blocks to disk.\n"); + exit(FALSE); + } return sync(); } + diff --git a/umount.c b/umount.c index 04cd8a080..a83a508f7 100644 --- a/umount.c +++ b/umount.c @@ -26,8 +26,11 @@ #include #include -const char umount_usage[] = "\tumount {filesystem|directory}\n" -"or to unmount all mounted file systems:\n\tumount -a\n"; +const char umount_usage[] = +"Usage: umount filesystem\n" +" or: umount directory\n" +" or: umount -a" +"to unmount all mounted file systems.\n"; static int umount_all() diff --git a/util-linux/umount.c b/util-linux/umount.c index 04cd8a080..a83a508f7 100644 --- a/util-linux/umount.c +++ b/util-linux/umount.c @@ -26,8 +26,11 @@ #include #include -const char umount_usage[] = "\tumount {filesystem|directory}\n" -"or to unmount all mounted file systems:\n\tumount -a\n"; +const char umount_usage[] = +"Usage: umount filesystem\n" +" or: umount directory\n" +" or: umount -a" +"to unmount all mounted file systems.\n"; static int umount_all() diff --git a/utility.c b/utility.c index 4bb479f0c..c6121cb11 100644 --- a/utility.c +++ b/utility.c @@ -188,36 +188,8 @@ copyFile( const char *srcName, const char *destName, #endif -#ifdef BB_MV -/* - * Build a path name from the specified directory name and file name. - * 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. - */ -char *buildName(const char *dirName, const char *fileName) -{ - const char *cp; - static char buf[PATH_LEN]; - - if ((dirName == NULL) || (*dirName == '\0')) { - strcpy(buf, fileName); - return buf; - } - - cp = strrchr(fileName, '/'); - - if (cp) - fileName = cp + 1; - - strcpy(buf, dirName); - strcat(buf, "/"); - - return buf; -} -#endif - - +#ifdef BB_TAR /* * Return the standard ls-like mode string from a file mode. * This is static and so is overwritten on each call. @@ -239,15 +211,10 @@ const char *modeString(int mode) buf[0] = 'b'; if (S_ISFIFO(mode)) buf[0] = 'p'; -#ifdef S_ISLNK if (S_ISLNK(mode)) buf[0] = 'l'; -#endif -#ifdef S_ISSOCK if (S_ISSOCK(mode)) buf[0] = 's'; -#endif - /* * Now fill in the normal file permissions. */ @@ -284,7 +251,6 @@ const char *modeString(int mode) } -#ifdef BB_TAR /* * Get the time string to be used for a file. * This is down to the minute for new files, but only the date for old files. @@ -467,7 +433,7 @@ int fullRead(int fd, char *buf, int len) * by the fileAction and dirAction function pointers). */ int -recursiveAction(const char *fileName, int recurse, int followLinks, +recursiveAction(const char *fileName, int recurse, int followLinks, int delayDirAction, int (*fileAction) (const char *fileName, struct stat* statbuf), int (*dirAction) (const char *fileName, struct stat* statbuf)) { @@ -501,7 +467,7 @@ recursiveAction(const char *fileName, int recurse, int followLinks, perror(fileName); return (FALSE); } - if (dirAction != NULL) { + if (dirAction != NULL && delayDirAction == FALSE) { status = dirAction(fileName, &statbuf); if (status == FALSE) { perror(fileName); @@ -516,8 +482,8 @@ recursiveAction(const char *fileName, int recurse, int followLinks, } sprintf(nextFile, "%s/%s", fileName, next->d_name); status = - recursiveAction(nextFile, TRUE, followLinks, fileAction, - dirAction); + recursiveAction(nextFile, TRUE, followLinks, delayDirAction, + fileAction, dirAction); if (status < 0) { closedir(dir); return (FALSE); @@ -528,6 +494,13 @@ recursiveAction(const char *fileName, int recurse, int followLinks, perror(fileName); return (FALSE); } + if (dirAction != NULL && delayDirAction == TRUE) { + status = dirAction(fileName, &statbuf); + if (status == FALSE) { + perror(fileName); + return (FALSE); + } + } } else { if (fileAction == NULL) return (TRUE);