More stuff.
This commit is contained in:
parent
8341a15653
commit
f811e07b07
@ -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},
|
||||||
|
@ -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;
|
||||||
|
@ -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},
|
||||||
|
@ -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
|
||||||
|
204
coreutils/cp.c
204
coreutils/cp.c
@ -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"
|
||||||
|
"Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\tCopy the source files to the destination.\n"
|
"\t-a\tsame as -dpR\n"
|
||||||
"\n"
|
"\t-d\tpreserve links\n"
|
||||||
"\t-r:\tRecursively copy all files and directories\n"
|
"\t-p\tpreserve file attributes if possable\n"
|
||||||
"\t\tunder the argument directory.";
|
"\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];
|
|
||||||
|
|
||||||
if ( (i->stat.st_mode & S_IFMT) == S_IFDIR ) {
|
|
||||||
if ( mkdir(destination, i->stat.st_mode & ~S_IFMT)
|
|
||||||
!= 0 && errno != EEXIST ) {
|
|
||||||
name_and_error(destination);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if ( (sourceFd = open(i->source, O_RDONLY)) < 0 ) {
|
|
||||||
name_and_error(i->source);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if ( stat(destination, &destination_stat) == 0 ) {
|
|
||||||
if ( i->stat.st_ino == destination_stat.st_ino
|
|
||||||
&& i->stat.st_dev == destination_stat.st_dev ) {
|
|
||||||
fprintf(stderr
|
|
||||||
,"copy of %s to %s would copy file to itself.\n"
|
|
||||||
,i->source
|
|
||||||
,destination);
|
|
||||||
close(sourceFd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* If the destination is a directory, create a file within it.
|
|
||||||
*/
|
|
||||||
if ( (destination_stat.st_mode & S_IFMT) == S_IFDIR ) {
|
|
||||||
destination = join_paths(
|
|
||||||
d
|
|
||||||
,i->destination
|
|
||||||
,&i->source[i->directoryLength]);
|
|
||||||
|
|
||||||
if ( stat(destination, &destination_stat) == 0 ) {
|
|
||||||
if ( i->stat.st_ino == destination_stat.st_ino
|
|
||||||
&& i->stat.st_dev == destination_stat.st_dev ) {
|
|
||||||
fprintf(stderr
|
|
||||||
,"copy of %s to %s would copy file to itself.\n"
|
|
||||||
,i->source
|
|
||||||
,destination);
|
|
||||||
close(sourceFd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destinationFd = creat(destination, i->stat.st_mode & 07777);
|
static int dirAction(const char *fileName)
|
||||||
|
{
|
||||||
|
char newdestName[NAME_MAX];
|
||||||
|
struct stat statBuf;
|
||||||
|
struct utimbuf times;
|
||||||
|
|
||||||
while ( (status = read(sourceFd, buf, sizeof(buf))) > 0 ) {
|
strcpy(newdestName, destName);
|
||||||
if ( write(destinationFd, buf, status) != status ) {
|
strcat(newdestName, fileName+(strlen(srcName)));
|
||||||
name_and_error(destination);
|
if (stat(newdestName, &statBuf)) {
|
||||||
close(sourceFd);
|
if (mkdir( newdestName, 0777777 ^ umask (0))) {
|
||||||
close(destinationFd);
|
perror(newdestName);
|
||||||
return 1;
|
return( FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(sourceFd);
|
else if (!S_ISDIR (statBuf.st_mode)) {
|
||||||
close(destinationFd);
|
fprintf(stderr, "`%s' exists but is not a directory", newdestName);
|
||||||
if ( status < 0 ) {
|
return( FALSE);
|
||||||
name_and_error(i->source);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
if (preserveFlag==TRUE) {
|
||||||
|
/* Try to preserve premissions, but don't whine on failure */
|
||||||
|
if (stat(newdestName, &statBuf)) {
|
||||||
|
perror(newdestName);
|
||||||
|
return( FALSE);
|
||||||
|
}
|
||||||
|
chmod(newdestName, statBuf.st_mode);
|
||||||
|
chown(newdestName, statBuf.st_uid, statBuf.st_gid);
|
||||||
|
times.actime = statBuf.st_atime;
|
||||||
|
times.modtime = statBuf.st_mtime;
|
||||||
|
utime(newdestName, ×);
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern int cp_main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
|
||||||
|
int dirFlag;
|
||||||
|
|
||||||
|
if (argc < 3) {
|
||||||
|
fprintf(stderr, "Usage: %s", cp_usage);
|
||||||
|
return (FALSE);
|
||||||
|
}
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
|
||||||
|
/* Parse any options */
|
||||||
|
while (**argv == '-') {
|
||||||
|
while (*++(*argv))
|
||||||
|
switch (**argv) {
|
||||||
|
case 'a':
|
||||||
|
followLinks = TRUE;
|
||||||
|
preserveFlag = TRUE;
|
||||||
|
recursiveFlag = TRUE;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
followLinks = TRUE;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
preserveFlag = TRUE;
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
recursiveFlag = TRUE;
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
204
cp.c
204
cp.c
@ -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"
|
||||||
|
"Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"\tCopy the source files to the destination.\n"
|
"\t-a\tsame as -dpR\n"
|
||||||
"\n"
|
"\t-d\tpreserve links\n"
|
||||||
"\t-r:\tRecursively copy all files and directories\n"
|
"\t-p\tpreserve file attributes if possable\n"
|
||||||
"\t\tunder the argument directory.";
|
"\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];
|
|
||||||
|
|
||||||
if ( (i->stat.st_mode & S_IFMT) == S_IFDIR ) {
|
|
||||||
if ( mkdir(destination, i->stat.st_mode & ~S_IFMT)
|
|
||||||
!= 0 && errno != EEXIST ) {
|
|
||||||
name_and_error(destination);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if ( (sourceFd = open(i->source, O_RDONLY)) < 0 ) {
|
|
||||||
name_and_error(i->source);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if ( stat(destination, &destination_stat) == 0 ) {
|
|
||||||
if ( i->stat.st_ino == destination_stat.st_ino
|
|
||||||
&& i->stat.st_dev == destination_stat.st_dev ) {
|
|
||||||
fprintf(stderr
|
|
||||||
,"copy of %s to %s would copy file to itself.\n"
|
|
||||||
,i->source
|
|
||||||
,destination);
|
|
||||||
close(sourceFd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* If the destination is a directory, create a file within it.
|
|
||||||
*/
|
|
||||||
if ( (destination_stat.st_mode & S_IFMT) == S_IFDIR ) {
|
|
||||||
destination = join_paths(
|
|
||||||
d
|
|
||||||
,i->destination
|
|
||||||
,&i->source[i->directoryLength]);
|
|
||||||
|
|
||||||
if ( stat(destination, &destination_stat) == 0 ) {
|
|
||||||
if ( i->stat.st_ino == destination_stat.st_ino
|
|
||||||
&& i->stat.st_dev == destination_stat.st_dev ) {
|
|
||||||
fprintf(stderr
|
|
||||||
,"copy of %s to %s would copy file to itself.\n"
|
|
||||||
,i->source
|
|
||||||
,destination);
|
|
||||||
close(sourceFd);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destinationFd = creat(destination, i->stat.st_mode & 07777);
|
static int dirAction(const char *fileName)
|
||||||
|
{
|
||||||
|
char newdestName[NAME_MAX];
|
||||||
|
struct stat statBuf;
|
||||||
|
struct utimbuf times;
|
||||||
|
|
||||||
while ( (status = read(sourceFd, buf, sizeof(buf))) > 0 ) {
|
strcpy(newdestName, destName);
|
||||||
if ( write(destinationFd, buf, status) != status ) {
|
strcat(newdestName, fileName+(strlen(srcName)));
|
||||||
name_and_error(destination);
|
if (stat(newdestName, &statBuf)) {
|
||||||
close(sourceFd);
|
if (mkdir( newdestName, 0777777 ^ umask (0))) {
|
||||||
close(destinationFd);
|
perror(newdestName);
|
||||||
return 1;
|
return( FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(sourceFd);
|
else if (!S_ISDIR (statBuf.st_mode)) {
|
||||||
close(destinationFd);
|
fprintf(stderr, "`%s' exists but is not a directory", newdestName);
|
||||||
if ( status < 0 ) {
|
return( FALSE);
|
||||||
name_and_error(i->source);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
if (preserveFlag==TRUE) {
|
||||||
|
/* Try to preserve premissions, but don't whine on failure */
|
||||||
|
if (stat(newdestName, &statBuf)) {
|
||||||
|
perror(newdestName);
|
||||||
|
return( FALSE);
|
||||||
|
}
|
||||||
|
chmod(newdestName, statBuf.st_mode);
|
||||||
|
chown(newdestName, statBuf.st_uid, statBuf.st_gid);
|
||||||
|
times.actime = statBuf.st_atime;
|
||||||
|
times.modtime = statBuf.st_mtime;
|
||||||
|
utime(newdestName, ×);
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern int cp_main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
|
||||||
|
int dirFlag;
|
||||||
|
|
||||||
|
if (argc < 3) {
|
||||||
|
fprintf(stderr, "Usage: %s", cp_usage);
|
||||||
|
return (FALSE);
|
||||||
|
}
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
|
||||||
|
/* Parse any options */
|
||||||
|
while (**argv == '-') {
|
||||||
|
while (*++(*argv))
|
||||||
|
switch (**argv) {
|
||||||
|
case 'a':
|
||||||
|
followLinks = TRUE;
|
||||||
|
preserveFlag = TRUE;
|
||||||
|
recursiveFlag = TRUE;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
followLinks = TRUE;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
preserveFlag = TRUE;
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
recursiveFlag = TRUE;
|
||||||
|
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
2
find.c
@ -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"
|
||||||
|
@ -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"
|
||||||
|
@ -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
10
grep.c
@ -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;
|
||||||
|
21
internal.h
21
internal.h
@ -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
6
mv.c
@ -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
42
tar.c
@ -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;
|
||||||
|
178
umount.c
178
umount.c
@ -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;
|
|
||||||
FILE * mountTable = setmntent("/etc/mtab", "r");
|
|
||||||
struct mntent *m;
|
struct mntent *m;
|
||||||
int status = 0;
|
FILE *mountTable;
|
||||||
|
|
||||||
if ( mountTable == 0
|
|
||||||
&& (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
|
|
||||||
name_and_error("/etc/mtab");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ((mountTable = setmntent ("/proc/mounts", "r"))) {
|
||||||
while ((m = getmntent (mountTable)) != 0) {
|
while ((m = getmntent (mountTable)) != 0) {
|
||||||
entries[count].mnt_fsname = stralloc(m->mnt_fsname);
|
char *blockDevice = m->mnt_fsname;
|
||||||
count++;
|
if (strcmp (blockDevice, "/dev/root") == 0)
|
||||||
|
blockDevice = (getfsfile ("/"))->fs_spec;
|
||||||
|
status=umount (m->mnt_dir);
|
||||||
|
if (status!=0) {
|
||||||
|
/* Don't bother retrying the umount on busy devices */
|
||||||
|
if (errno==EBUSY) {
|
||||||
|
perror(m->mnt_dir);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
printf ("Trying to umount %s failed: %s\n",
|
||||||
|
m->mnt_dir, strerror(errno));
|
||||||
|
printf ("Instead trying to umount %s\n", blockDevice);
|
||||||
|
status=umount (blockDevice);
|
||||||
|
if (status!=0) {
|
||||||
|
printf ("Couldn't umount %s on %s (type %s): %s\n",
|
||||||
|
blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
endmntent (mountTable);
|
endmntent (mountTable);
|
||||||
|
|
||||||
while ( count > 0 ) {
|
|
||||||
int result = umount(entries[--count].mnt_fsname) == 0;
|
|
||||||
/* free(entries[count].mnt_fsname); */
|
|
||||||
if ( result ) {
|
|
||||||
if ( !noMtab )
|
|
||||||
erase_mtab(entries[count].mnt_fsname);
|
|
||||||
}
|
}
|
||||||
else {
|
return( TRUE);
|
||||||
status = 1;
|
|
||||||
name_and_error(entries[count].mnt_fsname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 )
|
|
||||||
erase_mtab(name);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern int
|
if (argc < 2) {
|
||||||
umount_main(struct FileInfo * i, int argc, char * * argv)
|
fprintf(stderr, "Usage: %s", umount_usage);
|
||||||
{
|
return(FALSE);
|
||||||
int noMtab = 0;
|
}
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
|
||||||
if ( argv[1][0] == '-' ) {
|
/* Parse any options */
|
||||||
switch ( argv[1][1] ) {
|
while (**argv == '-') {
|
||||||
|
while (*++(*argv)) switch (**argv) {
|
||||||
case 'a':
|
case 'a':
|
||||||
return umount_all(noMtab);
|
return umount_all();
|
||||||
case 'n':
|
|
||||||
noMtab = 1;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage(umount_usage);
|
fprintf(stderr, "Usage: %s\n", umount_usage);
|
||||||
return 1;
|
exit( FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( do_umount(argv[1],noMtab) != 0 ) {
|
if ( umount(*argv) == 0 )
|
||||||
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
|
return (TRUE);
|
||||||
return 1;
|
else {
|
||||||
|
perror("umount");
|
||||||
|
return( FALSE);
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
|
||||||
FILE * mountTable = setmntent("/etc/mtab", "r");
|
|
||||||
struct mntent *m;
|
struct mntent *m;
|
||||||
int status = 0;
|
FILE *mountTable;
|
||||||
|
|
||||||
if ( mountTable == 0
|
|
||||||
&& (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
|
|
||||||
name_and_error("/etc/mtab");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ((mountTable = setmntent ("/proc/mounts", "r"))) {
|
||||||
while ((m = getmntent (mountTable)) != 0) {
|
while ((m = getmntent (mountTable)) != 0) {
|
||||||
entries[count].mnt_fsname = stralloc(m->mnt_fsname);
|
char *blockDevice = m->mnt_fsname;
|
||||||
count++;
|
if (strcmp (blockDevice, "/dev/root") == 0)
|
||||||
|
blockDevice = (getfsfile ("/"))->fs_spec;
|
||||||
|
status=umount (m->mnt_dir);
|
||||||
|
if (status!=0) {
|
||||||
|
/* Don't bother retrying the umount on busy devices */
|
||||||
|
if (errno==EBUSY) {
|
||||||
|
perror(m->mnt_dir);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
printf ("Trying to umount %s failed: %s\n",
|
||||||
|
m->mnt_dir, strerror(errno));
|
||||||
|
printf ("Instead trying to umount %s\n", blockDevice);
|
||||||
|
status=umount (blockDevice);
|
||||||
|
if (status!=0) {
|
||||||
|
printf ("Couldn't umount %s on %s (type %s): %s\n",
|
||||||
|
blockDevice, m->mnt_dir, m->mnt_type, strerror(errno));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
endmntent (mountTable);
|
endmntent (mountTable);
|
||||||
|
|
||||||
while ( count > 0 ) {
|
|
||||||
int result = umount(entries[--count].mnt_fsname) == 0;
|
|
||||||
/* free(entries[count].mnt_fsname); */
|
|
||||||
if ( result ) {
|
|
||||||
if ( !noMtab )
|
|
||||||
erase_mtab(entries[count].mnt_fsname);
|
|
||||||
}
|
}
|
||||||
else {
|
return( TRUE);
|
||||||
status = 1;
|
|
||||||
name_and_error(entries[count].mnt_fsname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 )
|
|
||||||
erase_mtab(name);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern int
|
if (argc < 2) {
|
||||||
umount_main(struct FileInfo * i, int argc, char * * argv)
|
fprintf(stderr, "Usage: %s", umount_usage);
|
||||||
{
|
return(FALSE);
|
||||||
int noMtab = 0;
|
}
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
|
||||||
if ( argv[1][0] == '-' ) {
|
/* Parse any options */
|
||||||
switch ( argv[1][1] ) {
|
while (**argv == '-') {
|
||||||
|
while (*++(*argv)) switch (**argv) {
|
||||||
case 'a':
|
case 'a':
|
||||||
return umount_all(noMtab);
|
return umount_all();
|
||||||
case 'n':
|
|
||||||
noMtab = 1;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage(umount_usage);
|
fprintf(stderr, "Usage: %s\n", umount_usage);
|
||||||
return 1;
|
exit( FALSE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( do_umount(argv[1],noMtab) != 0 ) {
|
if ( umount(*argv) == 0 )
|
||||||
fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
|
return (TRUE);
|
||||||
return 1;
|
else {
|
||||||
|
perror("umount");
|
||||||
|
return( FALSE);
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
237
utility.c
237
utility.c
@ -27,16 +27,15 @@
|
|||||||
#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;
|
||||||
|
|
||||||
@ -68,8 +67,7 @@ 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];
|
||||||
|
|
||||||
@ -135,8 +133,7 @@ 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;
|
||||||
|
|
||||||
@ -151,8 +148,7 @@ 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;
|
||||||
|
|
||||||
@ -169,12 +165,10 @@ 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;
|
||||||
@ -184,77 +178,95 @@ copyFile(
|
|||||||
struct stat statBuf2;
|
struct stat statBuf2;
|
||||||
struct utimbuf times;
|
struct utimbuf times;
|
||||||
|
|
||||||
if (stat(srcName, &statBuf1) < 0)
|
if (stat(srcName, &statBuf1) < 0) {
|
||||||
{
|
|
||||||
perror(srcName);
|
perror(srcName);
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stat(destName, &statBuf2) < 0)
|
if (stat(destName, &statBuf2) < 0) {
|
||||||
{
|
|
||||||
statBuf2.st_ino = -1;
|
statBuf2.st_ino = -1;
|
||||||
statBuf2.st_dev = -1;
|
statBuf2.st_dev = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((statBuf1.st_dev == statBuf2.st_dev) &&
|
if ((statBuf1.st_dev == statBuf2.st_dev) &&
|
||||||
(statBuf1.st_ino == statBuf2.st_ino))
|
(statBuf1.st_ino == statBuf2.st_ino)) {
|
||||||
{
|
|
||||||
fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
|
fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
rfd = open(srcName, O_RDONLY);
|
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 (mkfifo(destName, 644)) {
|
||||||
|
perror(destName);
|
||||||
|
return (FALSE);
|
||||||
|
}
|
||||||
|
} else if (S_ISBLK(statBuf1.st_mode) || S_ISCHR(statBuf1.st_mode)) {
|
||||||
|
if (mknod(destName, 644, statBuf1.st_rdev)) {
|
||||||
|
perror(destName);
|
||||||
|
return (FALSE);
|
||||||
|
}
|
||||||
|
} else if (S_ISLNK(statBuf1.st_mode)) {
|
||||||
|
char *link_val;
|
||||||
|
int link_size;
|
||||||
|
|
||||||
if (rfd < 0)
|
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);
|
||||||
|
if (rfd < 0) {
|
||||||
perror(srcName);
|
perror(srcName);
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
wfd = creat(destName, statBuf1.st_mode);
|
wfd = creat(destName, statBuf1.st_mode);
|
||||||
|
if (wfd < 0) {
|
||||||
if (wfd < 0)
|
|
||||||
{
|
|
||||||
perror(destName);
|
perror(destName);
|
||||||
close(rfd);
|
close(rfd);
|
||||||
|
|
||||||
return FALSE;
|
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;
|
goto error_exit;
|
||||||
}
|
}
|
||||||
|
if (rcc < 0) {
|
||||||
if (rcc < 0)
|
|
||||||
{
|
|
||||||
perror(srcName);
|
perror(srcName);
|
||||||
goto error_exit;
|
goto error_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) close(rfd);
|
close(rfd);
|
||||||
|
|
||||||
if (close(wfd) < 0)
|
if (close(wfd) < 0) {
|
||||||
{
|
|
||||||
perror(destName);
|
perror(destName);
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (setModes)
|
if (setModes == TRUE) {
|
||||||
{
|
chmod(destName, statBuf1.st_mode);
|
||||||
(void) chmod(destName, statBuf1.st_mode);
|
if (followLinks == TRUE)
|
||||||
|
chown(destName, statBuf1.st_uid, statBuf1.st_gid);
|
||||||
(void) chown(destName, statBuf1.st_uid, statBuf1.st_gid);
|
else
|
||||||
|
lchown(destName, statBuf1.st_uid, statBuf1.st_gid);
|
||||||
|
|
||||||
times.actime = statBuf1.st_atime;
|
times.actime = statBuf1.st_atime;
|
||||||
times.modtime = statBuf1.st_mtime;
|
times.modtime = statBuf1.st_mtime;
|
||||||
|
|
||||||
(void) utime(destName, ×);
|
utime(destName, ×);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@ -273,14 +285,15 @@ 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'))
|
if ((dirName == NULL) || (*dirName == '\0')) {
|
||||||
return fileName;
|
strcpy(buf, fileName);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
cp = strrchr(fileName, '/');
|
cp = strrchr(fileName, '/');
|
||||||
|
|
||||||
@ -289,7 +302,6 @@ buildName(const char * dirName, const char * fileName)
|
|||||||
|
|
||||||
strcpy(buf, dirName);
|
strcpy(buf, dirName);
|
||||||
strcat(buf, "/");
|
strcat(buf, "/");
|
||||||
strcat(buf, fileName);
|
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
@ -302,22 +314,14 @@ 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;
|
||||||
@ -346,8 +350,7 @@ 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;
|
||||||
|
|
||||||
@ -371,8 +374,7 @@ 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;
|
||||||
@ -391,13 +393,11 @@ 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,8 +411,7 @@ 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;
|
||||||
@ -425,8 +424,7 @@ timeString(time_t 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';
|
||||||
}
|
}
|
||||||
@ -445,23 +443,20 @@ 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;
|
||||||
@ -470,8 +465,7 @@ match(const char * text, const char * pattern)
|
|||||||
case '[':
|
case '[':
|
||||||
found = FALSE;
|
found = FALSE;
|
||||||
|
|
||||||
while ((ch = *pattern++) != ']')
|
while ((ch = *pattern++) != ']') {
|
||||||
{
|
|
||||||
if (ch == '\\')
|
if (ch == '\\')
|
||||||
ch = *pattern++;
|
ch = *pattern++;
|
||||||
|
|
||||||
@ -482,8 +476,7 @@ match(const char * text, const char * pattern)
|
|||||||
found = TRUE;
|
found = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found)
|
if (!found) {
|
||||||
{
|
|
||||||
pattern = retryPat;
|
pattern = retryPat;
|
||||||
text = ++retryText;
|
text = ++retryText;
|
||||||
}
|
}
|
||||||
@ -505,15 +498,13 @@ match(const char * text, const char * pattern)
|
|||||||
/* 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;
|
||||||
@ -535,16 +526,14 @@ 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)
|
||||||
@ -565,16 +554,14 @@ 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)
|
||||||
@ -598,7 +585,7 @@ 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))
|
||||||
{
|
{
|
||||||
@ -606,7 +593,17 @@ recursiveAction( const char *fileName, BOOL recurse, BOOL followLinks,
|
|||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
struct dirent *next;
|
struct dirent *next;
|
||||||
|
|
||||||
if (!recurse && S_ISDIR(statbuf.st_mode)) {
|
if (followLinks)
|
||||||
|
status = lstat(fileName, &statbuf);
|
||||||
|
else
|
||||||
|
status = stat(fileName, &statbuf);
|
||||||
|
if (status < 0) {
|
||||||
|
perror(fileName);
|
||||||
|
return (FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recurse == FALSE) {
|
||||||
|
if (S_ISDIR(statbuf.st_mode)) {
|
||||||
if (dirAction == NULL)
|
if (dirAction == NULL)
|
||||||
return (TRUE);
|
return (TRUE);
|
||||||
else
|
else
|
||||||
@ -617,31 +614,33 @@ recursiveAction( const char *fileName, BOOL recurse, BOOL followLinks,
|
|||||||
else
|
else
|
||||||
return (fileAction(fileName));
|
return (fileAction(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (followLinks)
|
|
||||||
status = stat(fileName, &statbuf);
|
|
||||||
else
|
|
||||||
status = lstat(fileName, &statbuf);
|
|
||||||
|
|
||||||
if (status < 0) {
|
|
||||||
perror(fileName);
|
|
||||||
return( FALSE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
if (dirAction != NULL) {
|
||||||
|
status = dirAction(fileName);
|
||||||
|
if (status == FALSE) {
|
||||||
|
perror("cp");
|
||||||
|
return (FALSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
while ((next = readdir(dir)) != NULL) {
|
while ((next = readdir(dir)) != NULL) {
|
||||||
char nextFile[NAME_MAX];
|
char nextFile[NAME_MAX];
|
||||||
if ( (strcmp(next->d_name, "..") == 0) || (strcmp(next->d_name, ".") == 0) ) {
|
if ((strcmp(next->d_name, "..") == 0)
|
||||||
|
|| (strcmp(next->d_name, ".") == 0)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sprintf(nextFile, "%s/%s", fileName, next->d_name);
|
sprintf(nextFile, "%s/%s", fileName, next->d_name);
|
||||||
status = recursiveAction(nextFile, TRUE, followLinks, fileAction, dirAction);
|
status =
|
||||||
|
recursiveAction(nextFile, TRUE, followLinks, fileAction,
|
||||||
|
dirAction);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
@ -652,30 +651,16 @@ recursiveAction( const char *fileName, BOOL recurse, BOOL followLinks,
|
|||||||
perror(fileName);
|
perror(fileName);
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
}
|
}
|
||||||
if (dirAction==NULL)
|
} else {
|
||||||
return(TRUE);
|
fprintf(stderr, "File: %s\n", fileName);
|
||||||
else
|
|
||||||
return(dirAction(fileName));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (fileAction == NULL)
|
if (fileAction == NULL)
|
||||||
return (TRUE);
|
return (TRUE);
|
||||||
else
|
else
|
||||||
return (fileAction(fileName));
|
return (fileAction(fileName));
|
||||||
}
|
}
|
||||||
|
return (TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* END CODE */
|
/* END CODE */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user