Reverted my conversion of tar to getopt to ensure tar can

handle traditional semantics (i.e. 'tar -xvf -' or 'tar xvf'
now both work).
 -Erik
This commit is contained in:
Eric Andersen 2000-09-19 21:35:14 +00:00
parent 56f3e353da
commit 46a98dfb13
2 changed files with 80 additions and 52 deletions

View File

@ -133,18 +133,10 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
#ifdef BB_FEATURE_TAR_CREATE #ifdef BB_FEATURE_TAR_CREATE
/* Local procedures to save files into a tar file. */ /* Local procedures to save files into a tar file. */
static int writeTarFile(const char* tarName, int verboseFlag, int argc, static int writeTarFile(const char* tarName, int tostdoutFlag,
char **argv, char** excludeList); int verboseFlag, int argc, char **argv, char** excludeList);
#endif #endif
static struct option longopts[] =
{
#ifdef BB_FEATURE_TAR_EXCLUDE
{"exclude",required_argument,NULL,'e'},
#endif
{NULL,0,NULL,0}
};
extern int tar_main(int argc, char **argv) extern int tar_main(int argc, char **argv)
{ {
char** excludeList=NULL; char** excludeList=NULL;
@ -157,14 +149,17 @@ extern int tar_main(int argc, char **argv)
int createFlag = FALSE; int createFlag = FALSE;
int verboseFlag = FALSE; int verboseFlag = FALSE;
int tostdoutFlag = FALSE; int tostdoutFlag = FALSE;
int opt; int stopIt;
if (argc <= 1) if (argc <= 1)
usage(tar_usage); usage(tar_usage);
/* do normal option parsing */ /* do normal option parsing */
while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) { while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
switch (opt) { stopIt=FALSE;
while (stopIt==FALSE && *argv && **argv) {
switch (**argv) {
case 'c': case 'c':
if (extractFlag == TRUE || listFlag == TRUE) if (extractFlag == TRUE || listFlag == TRUE)
goto flagError; goto flagError;
@ -185,26 +180,45 @@ extern int tar_main(int argc, char **argv)
break; break;
case 'O': case 'O':
tostdoutFlag = TRUE; tostdoutFlag = TRUE;
tarName = "-";
break; break;
case 'f': case 'f':
if (--argc == 0) {
fatalError( "Option requires an argument: No file specified\n");
}
if (*tarName != '-') if (*tarName != '-')
fatalError( "Only one 'f' option allowed\n"); fatalError( "Only one 'f' option allowed\n");
tarName = optarg; tarName = *(++argv);
if (tarName == NULL)
fatalError( "Option requires an argument: No file specified\n");
if (!strcmp(tarName, "-") && createFlag == TRUE)
tostdoutFlag = TRUE;
stopIt=TRUE;
break; break;
#if defined BB_FEATURE_TAR_EXCLUDE #if defined BB_FEATURE_TAR_EXCLUDE
case 'e': case 'e':
if (strcmp(*argv, "-exclude")==0) {
if (--argc == 0) {
fatalError( "Option requires an argument: No file specified\n");
}
excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2)); excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
excludeList[excludeListSize] = optarg; excludeList[excludeListSize] = *(++argv);
/* Remove leading "/"s */ /* Remove leading "/"s */
if (*excludeList[excludeListSize] =='/') if (*excludeList[excludeListSize] =='/')
excludeList[excludeListSize] = (excludeList[excludeListSize])+1; excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
/* Tack a NULL onto the end of the list */ /* Tack a NULL onto the end of the list */
excludeList[++excludeListSize] = NULL; excludeList[++excludeListSize] = NULL;
stopIt=TRUE;
break; break;
}
#endif #endif
case '-':
break;
default: default:
usage(tar_usage); usage(tar_usage);
} }
++(*argv);
}
} }
/* /*
@ -215,7 +229,7 @@ extern int tar_main(int argc, char **argv)
#ifndef BB_FEATURE_TAR_CREATE #ifndef BB_FEATURE_TAR_CREATE
fatalError( "This version of tar was not compiled with tar creation support.\n"); fatalError( "This version of tar was not compiled with tar creation support.\n");
#else #else
exit(writeTarFile(tarName, verboseFlag, argc-optind, &argv[optind], excludeList)); exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
#endif #endif
} }
if (listFlag == TRUE || extractFlag == TRUE) { if (listFlag == TRUE || extractFlag == TRUE) {
@ -919,8 +933,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
return( TRUE); return( TRUE);
} }
static int writeTarFile(const char* tarName, int verboseFlag, int argc, static int writeTarFile(const char* tarName, int tostdoutFlag,
char **argv, char** excludeList) int verboseFlag, int argc, char **argv, char** excludeList)
{ {
int tarFd=-1; int tarFd=-1;
int errorFlag=FALSE; int errorFlag=FALSE;
@ -933,7 +947,7 @@ static int writeTarFile(const char* tarName, int verboseFlag, int argc,
fatalError("Cowardly refusing to create an empty archive\n"); fatalError("Cowardly refusing to create an empty archive\n");
/* Open the tar file for writing. */ /* Open the tar file for writing. */
if (strcmp(tarName, "-") == 0) if (tostdoutFlag == TRUE)
tbInfo.tarFd = fileno(stdout); tbInfo.tarFd = fileno(stdout);
else else
tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);

52
tar.c
View File

@ -133,18 +133,10 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
#ifdef BB_FEATURE_TAR_CREATE #ifdef BB_FEATURE_TAR_CREATE
/* Local procedures to save files into a tar file. */ /* Local procedures to save files into a tar file. */
static int writeTarFile(const char* tarName, int verboseFlag, int argc, static int writeTarFile(const char* tarName, int tostdoutFlag,
char **argv, char** excludeList); int verboseFlag, int argc, char **argv, char** excludeList);
#endif #endif
static struct option longopts[] =
{
#ifdef BB_FEATURE_TAR_EXCLUDE
{"exclude",required_argument,NULL,'e'},
#endif
{NULL,0,NULL,0}
};
extern int tar_main(int argc, char **argv) extern int tar_main(int argc, char **argv)
{ {
char** excludeList=NULL; char** excludeList=NULL;
@ -157,14 +149,17 @@ extern int tar_main(int argc, char **argv)
int createFlag = FALSE; int createFlag = FALSE;
int verboseFlag = FALSE; int verboseFlag = FALSE;
int tostdoutFlag = FALSE; int tostdoutFlag = FALSE;
int opt; int stopIt;
if (argc <= 1) if (argc <= 1)
usage(tar_usage); usage(tar_usage);
/* do normal option parsing */ /* do normal option parsing */
while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) { while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
switch (opt) { stopIt=FALSE;
while (stopIt==FALSE && *argv && **argv) {
switch (**argv) {
case 'c': case 'c':
if (extractFlag == TRUE || listFlag == TRUE) if (extractFlag == TRUE || listFlag == TRUE)
goto flagError; goto flagError;
@ -185,26 +180,45 @@ extern int tar_main(int argc, char **argv)
break; break;
case 'O': case 'O':
tostdoutFlag = TRUE; tostdoutFlag = TRUE;
tarName = "-";
break; break;
case 'f': case 'f':
if (--argc == 0) {
fatalError( "Option requires an argument: No file specified\n");
}
if (*tarName != '-') if (*tarName != '-')
fatalError( "Only one 'f' option allowed\n"); fatalError( "Only one 'f' option allowed\n");
tarName = optarg; tarName = *(++argv);
if (tarName == NULL)
fatalError( "Option requires an argument: No file specified\n");
if (!strcmp(tarName, "-") && createFlag == TRUE)
tostdoutFlag = TRUE;
stopIt=TRUE;
break; break;
#if defined BB_FEATURE_TAR_EXCLUDE #if defined BB_FEATURE_TAR_EXCLUDE
case 'e': case 'e':
if (strcmp(*argv, "-exclude")==0) {
if (--argc == 0) {
fatalError( "Option requires an argument: No file specified\n");
}
excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2)); excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
excludeList[excludeListSize] = optarg; excludeList[excludeListSize] = *(++argv);
/* Remove leading "/"s */ /* Remove leading "/"s */
if (*excludeList[excludeListSize] =='/') if (*excludeList[excludeListSize] =='/')
excludeList[excludeListSize] = (excludeList[excludeListSize])+1; excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
/* Tack a NULL onto the end of the list */ /* Tack a NULL onto the end of the list */
excludeList[++excludeListSize] = NULL; excludeList[++excludeListSize] = NULL;
stopIt=TRUE;
break; break;
}
#endif #endif
case '-':
break;
default: default:
usage(tar_usage); usage(tar_usage);
} }
++(*argv);
}
} }
/* /*
@ -215,7 +229,7 @@ extern int tar_main(int argc, char **argv)
#ifndef BB_FEATURE_TAR_CREATE #ifndef BB_FEATURE_TAR_CREATE
fatalError( "This version of tar was not compiled with tar creation support.\n"); fatalError( "This version of tar was not compiled with tar creation support.\n");
#else #else
exit(writeTarFile(tarName, verboseFlag, argc-optind, &argv[optind], excludeList)); exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
#endif #endif
} }
if (listFlag == TRUE || extractFlag == TRUE) { if (listFlag == TRUE || extractFlag == TRUE) {
@ -919,8 +933,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
return( TRUE); return( TRUE);
} }
static int writeTarFile(const char* tarName, int verboseFlag, int argc, static int writeTarFile(const char* tarName, int tostdoutFlag,
char **argv, char** excludeList) int verboseFlag, int argc, char **argv, char** excludeList)
{ {
int tarFd=-1; int tarFd=-1;
int errorFlag=FALSE; int errorFlag=FALSE;
@ -933,7 +947,7 @@ static int writeTarFile(const char* tarName, int verboseFlag, int argc,
fatalError("Cowardly refusing to create an empty archive\n"); fatalError("Cowardly refusing to create an empty archive\n");
/* Open the tar file for writing. */ /* Open the tar file for writing. */
if (strcmp(tarName, "-") == 0) if (tostdoutFlag == TRUE)
tbInfo.tarFd = fileno(stdout); tbInfo.tarFd = fileno(stdout);
else else
tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);