More fixes

This commit is contained in:
Eric Andersen 1999-10-18 19:02:32 +00:00
parent 9b58718889
commit bed30e9700
19 changed files with 297 additions and 256 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -21,63 +21,59 @@
#include "internal.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <utime.h>
#include <errno.h>
const char mv_usage[] = "source-file [source-file ...] destination-file\n"
"\n" "\tMove the source files to the destination.\n" "\n";
#include <dirent.h>
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) {
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));
}
if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
exit( FALSE);
}
if (unlink (srcName) < 0) {
perror (srcName);
continue;
exit( FALSE);
}
destName = lastArg;
if (dirFlag==TRUE)
destName = buildName (destName, srcName);
if (rename (srcName, destName) >= 0)
continue;
if (errno != EXDEV) {
perror (destName);
continue;
}
if (!copyFile (srcName, destName, TRUE, FALSE))
continue;
if (unlink (srcName) < 0)
perror (srcName);
}
exit (TRUE);
exit( TRUE);
}

View File

@ -1,30 +1,93 @@
/*
* Mini rm 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 <errno.h>
#include <stdio.h>
#include <time.h>
#include <utime.h>
#include <dirent.h>
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);
}
extern int
rm_fn(const struct FileInfo * i)
{
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 (unlink( fileName) < 0 ) {
perror( fileName);
return ( FALSE);
}
else
return 0;
return ( TRUE);
}
static int dirAction(const char *fileName, struct stat* statbuf)
{
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);
}
}
exit( TRUE);
}

View File

@ -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();
}

10
cp.c
View File

@ -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);
}

33
find.c
View File

@ -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 <errno.h>
#include <stdio.h>
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

View File

@ -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 <errno.h>
#include <stdio.h>
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

View File

@ -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);

2
ls.c
View File

@ -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);
}

80
mv.c
View File

@ -21,63 +21,59 @@
#include "internal.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <utime.h>
#include <errno.h>
const char mv_usage[] = "source-file [source-file ...] destination-file\n"
"\n" "\tMove the source files to the destination.\n" "\n";
#include <dirent.h>
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) {
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));
}
if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
exit( FALSE);
}
if (unlink (srcName) < 0) {
perror (srcName);
continue;
exit( FALSE);
}
destName = lastArg;
if (dirFlag==TRUE)
destName = buildName (destName, srcName);
if (rename (srcName, destName) >= 0)
continue;
if (errno != EXDEV) {
perror (destName);
continue;
}
if (!copyFile (srcName, destName, TRUE, FALSE))
continue;
if (unlink (srcName) < 0)
perror (srcName);
}
exit (TRUE);
exit( TRUE);
}

111
rm.c
View File

@ -1,30 +1,93 @@
/*
* Mini rm 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 <errno.h>
#include <stdio.h>
#include <time.h>
#include <utime.h>
#include <dirent.h>
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);
}
extern int
rm_fn(const struct FileInfo * i)
{
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 (unlink( fileName) < 0 ) {
perror( fileName);
return ( FALSE);
}
else
return 0;
return ( TRUE);
}
static int dirAction(const char *fileName, struct stat* statbuf)
{
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);
}
}
exit( TRUE);
}

View File

@ -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))) {

11
sync.c
View File

@ -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();
}

View File

@ -26,8 +26,11 @@
#include <fstab.h>
#include <errno.h>
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()

View File

@ -26,8 +26,11 @@
#include <fstab.h>
#include <errno.h>
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()

View File

@ -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);