aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2000-09-19 21:35:14 +0000
committerEric Andersen <andersen@codepoet.org>2000-09-19 21:35:14 +0000
commit46a98dfb138a55e5f7a94d7f7a82671c644a8368 (patch)
treef6f7eb4ac7ff8c5eadc39134d82bc9c9711e8673
parent56f3e353da3facd5f4a04eadf813312433f5363f (diff)
downloadbusybox-w32-46a98dfb138a55e5f7a94d7f7a82671c644a8368.tar.gz
busybox-w32-46a98dfb138a55e5f7a94d7f7a82671c644a8368.tar.bz2
busybox-w32-46a98dfb138a55e5f7a94d7f7a82671c644a8368.zip
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
-rw-r--r--archival/tar.c66
-rw-r--r--tar.c66
2 files changed, 80 insertions, 52 deletions
diff --git a/archival/tar.c b/archival/tar.c
index 723943245..ade77a59d 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -133,18 +133,10 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
133 133
134#ifdef BB_FEATURE_TAR_CREATE 134#ifdef BB_FEATURE_TAR_CREATE
135/* Local procedures to save files into a tar file. */ 135/* Local procedures to save files into a tar file. */
136static int writeTarFile(const char* tarName, int verboseFlag, int argc, 136static int writeTarFile(const char* tarName, int tostdoutFlag,
137 char **argv, char** excludeList); 137 int verboseFlag, int argc, char **argv, char** excludeList);
138#endif 138#endif
139 139
140static struct option longopts[] =
141{
142#ifdef BB_FEATURE_TAR_EXCLUDE
143 {"exclude",required_argument,NULL,'e'},
144#endif
145 {NULL,0,NULL,0}
146};
147
148extern int tar_main(int argc, char **argv) 140extern int tar_main(int argc, char **argv)
149{ 141{
150 char** excludeList=NULL; 142 char** excludeList=NULL;
@@ -157,14 +149,17 @@ extern int tar_main(int argc, char **argv)
157 int createFlag = FALSE; 149 int createFlag = FALSE;
158 int verboseFlag = FALSE; 150 int verboseFlag = FALSE;
159 int tostdoutFlag = FALSE; 151 int tostdoutFlag = FALSE;
160 int opt; 152 int stopIt;
153
161 154
162 if (argc <= 1) 155 if (argc <= 1)
163 usage(tar_usage); 156 usage(tar_usage);
164 157
165 /* do normal option parsing */ 158 /* do normal option parsing */
166 while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) { 159 while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
167 switch (opt) { 160 stopIt=FALSE;
161 while (stopIt==FALSE && *argv && **argv) {
162 switch (**argv) {
168 case 'c': 163 case 'c':
169 if (extractFlag == TRUE || listFlag == TRUE) 164 if (extractFlag == TRUE || listFlag == TRUE)
170 goto flagError; 165 goto flagError;
@@ -185,25 +180,44 @@ extern int tar_main(int argc, char **argv)
185 break; 180 break;
186 case 'O': 181 case 'O':
187 tostdoutFlag = TRUE; 182 tostdoutFlag = TRUE;
183 tarName = "-";
188 break; 184 break;
189 case 'f': 185 case 'f':
186 if (--argc == 0) {
187 fatalError( "Option requires an argument: No file specified\n");
188 }
190 if (*tarName != '-') 189 if (*tarName != '-')
191 fatalError( "Only one 'f' option allowed\n"); 190 fatalError( "Only one 'f' option allowed\n");
192 tarName = optarg; 191 tarName = *(++argv);
192 if (tarName == NULL)
193 fatalError( "Option requires an argument: No file specified\n");
194 if (!strcmp(tarName, "-") && createFlag == TRUE)
195 tostdoutFlag = TRUE;
196 stopIt=TRUE;
193 break; 197 break;
194#if defined BB_FEATURE_TAR_EXCLUDE 198#if defined BB_FEATURE_TAR_EXCLUDE
195 case 'e': 199 case 'e':
196 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2)); 200 if (strcmp(*argv, "-exclude")==0) {
197 excludeList[excludeListSize] = optarg; 201 if (--argc == 0) {
198 /* Remove leading "/"s */ 202 fatalError( "Option requires an argument: No file specified\n");
199 if (*excludeList[excludeListSize] =='/') 203 }
200 excludeList[excludeListSize] = (excludeList[excludeListSize])+1; 204 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
201 /* Tack a NULL onto the end of the list */ 205 excludeList[excludeListSize] = *(++argv);
202 excludeList[++excludeListSize] = NULL; 206 /* Remove leading "/"s */
203 break; 207 if (*excludeList[excludeListSize] =='/')
208 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
209 /* Tack a NULL onto the end of the list */
210 excludeList[++excludeListSize] = NULL;
211 stopIt=TRUE;
212 break;
213 }
204#endif 214#endif
215 case '-':
216 break;
205 default: 217 default:
206 usage(tar_usage); 218 usage(tar_usage);
219 }
220 ++(*argv);
207 } 221 }
208 } 222 }
209 223
@@ -215,7 +229,7 @@ extern int tar_main(int argc, char **argv)
215#ifndef BB_FEATURE_TAR_CREATE 229#ifndef BB_FEATURE_TAR_CREATE
216 fatalError( "This version of tar was not compiled with tar creation support.\n"); 230 fatalError( "This version of tar was not compiled with tar creation support.\n");
217#else 231#else
218 exit(writeTarFile(tarName, verboseFlag, argc-optind, &argv[optind], excludeList)); 232 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
219#endif 233#endif
220 } 234 }
221 if (listFlag == TRUE || extractFlag == TRUE) { 235 if (listFlag == TRUE || extractFlag == TRUE) {
@@ -919,8 +933,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
919 return( TRUE); 933 return( TRUE);
920} 934}
921 935
922static int writeTarFile(const char* tarName, int verboseFlag, int argc, 936static int writeTarFile(const char* tarName, int tostdoutFlag,
923 char **argv, char** excludeList) 937 int verboseFlag, int argc, char **argv, char** excludeList)
924{ 938{
925 int tarFd=-1; 939 int tarFd=-1;
926 int errorFlag=FALSE; 940 int errorFlag=FALSE;
@@ -933,7 +947,7 @@ static int writeTarFile(const char* tarName, int verboseFlag, int argc,
933 fatalError("Cowardly refusing to create an empty archive\n"); 947 fatalError("Cowardly refusing to create an empty archive\n");
934 948
935 /* Open the tar file for writing. */ 949 /* Open the tar file for writing. */
936 if (strcmp(tarName, "-") == 0) 950 if (tostdoutFlag == TRUE)
937 tbInfo.tarFd = fileno(stdout); 951 tbInfo.tarFd = fileno(stdout);
938 else 952 else
939 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); 953 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
diff --git a/tar.c b/tar.c
index 723943245..ade77a59d 100644
--- a/tar.c
+++ b/tar.c
@@ -133,18 +133,10 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
133 133
134#ifdef BB_FEATURE_TAR_CREATE 134#ifdef BB_FEATURE_TAR_CREATE
135/* Local procedures to save files into a tar file. */ 135/* Local procedures to save files into a tar file. */
136static int writeTarFile(const char* tarName, int verboseFlag, int argc, 136static int writeTarFile(const char* tarName, int tostdoutFlag,
137 char **argv, char** excludeList); 137 int verboseFlag, int argc, char **argv, char** excludeList);
138#endif 138#endif
139 139
140static struct option longopts[] =
141{
142#ifdef BB_FEATURE_TAR_EXCLUDE
143 {"exclude",required_argument,NULL,'e'},
144#endif
145 {NULL,0,NULL,0}
146};
147
148extern int tar_main(int argc, char **argv) 140extern int tar_main(int argc, char **argv)
149{ 141{
150 char** excludeList=NULL; 142 char** excludeList=NULL;
@@ -157,14 +149,17 @@ extern int tar_main(int argc, char **argv)
157 int createFlag = FALSE; 149 int createFlag = FALSE;
158 int verboseFlag = FALSE; 150 int verboseFlag = FALSE;
159 int tostdoutFlag = FALSE; 151 int tostdoutFlag = FALSE;
160 int opt; 152 int stopIt;
153
161 154
162 if (argc <= 1) 155 if (argc <= 1)
163 usage(tar_usage); 156 usage(tar_usage);
164 157
165 /* do normal option parsing */ 158 /* do normal option parsing */
166 while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) { 159 while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
167 switch (opt) { 160 stopIt=FALSE;
161 while (stopIt==FALSE && *argv && **argv) {
162 switch (**argv) {
168 case 'c': 163 case 'c':
169 if (extractFlag == TRUE || listFlag == TRUE) 164 if (extractFlag == TRUE || listFlag == TRUE)
170 goto flagError; 165 goto flagError;
@@ -185,25 +180,44 @@ extern int tar_main(int argc, char **argv)
185 break; 180 break;
186 case 'O': 181 case 'O':
187 tostdoutFlag = TRUE; 182 tostdoutFlag = TRUE;
183 tarName = "-";
188 break; 184 break;
189 case 'f': 185 case 'f':
186 if (--argc == 0) {
187 fatalError( "Option requires an argument: No file specified\n");
188 }
190 if (*tarName != '-') 189 if (*tarName != '-')
191 fatalError( "Only one 'f' option allowed\n"); 190 fatalError( "Only one 'f' option allowed\n");
192 tarName = optarg; 191 tarName = *(++argv);
192 if (tarName == NULL)
193 fatalError( "Option requires an argument: No file specified\n");
194 if (!strcmp(tarName, "-") && createFlag == TRUE)
195 tostdoutFlag = TRUE;
196 stopIt=TRUE;
193 break; 197 break;
194#if defined BB_FEATURE_TAR_EXCLUDE 198#if defined BB_FEATURE_TAR_EXCLUDE
195 case 'e': 199 case 'e':
196 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2)); 200 if (strcmp(*argv, "-exclude")==0) {
197 excludeList[excludeListSize] = optarg; 201 if (--argc == 0) {
198 /* Remove leading "/"s */ 202 fatalError( "Option requires an argument: No file specified\n");
199 if (*excludeList[excludeListSize] =='/') 203 }
200 excludeList[excludeListSize] = (excludeList[excludeListSize])+1; 204 excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
201 /* Tack a NULL onto the end of the list */ 205 excludeList[excludeListSize] = *(++argv);
202 excludeList[++excludeListSize] = NULL; 206 /* Remove leading "/"s */
203 break; 207 if (*excludeList[excludeListSize] =='/')
208 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
209 /* Tack a NULL onto the end of the list */
210 excludeList[++excludeListSize] = NULL;
211 stopIt=TRUE;
212 break;
213 }
204#endif 214#endif
215 case '-':
216 break;
205 default: 217 default:
206 usage(tar_usage); 218 usage(tar_usage);
219 }
220 ++(*argv);
207 } 221 }
208 } 222 }
209 223
@@ -215,7 +229,7 @@ extern int tar_main(int argc, char **argv)
215#ifndef BB_FEATURE_TAR_CREATE 229#ifndef BB_FEATURE_TAR_CREATE
216 fatalError( "This version of tar was not compiled with tar creation support.\n"); 230 fatalError( "This version of tar was not compiled with tar creation support.\n");
217#else 231#else
218 exit(writeTarFile(tarName, verboseFlag, argc-optind, &argv[optind], excludeList)); 232 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
219#endif 233#endif
220 } 234 }
221 if (listFlag == TRUE || extractFlag == TRUE) { 235 if (listFlag == TRUE || extractFlag == TRUE) {
@@ -919,8 +933,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
919 return( TRUE); 933 return( TRUE);
920} 934}
921 935
922static int writeTarFile(const char* tarName, int verboseFlag, int argc, 936static int writeTarFile(const char* tarName, int tostdoutFlag,
923 char **argv, char** excludeList) 937 int verboseFlag, int argc, char **argv, char** excludeList)
924{ 938{
925 int tarFd=-1; 939 int tarFd=-1;
926 int errorFlag=FALSE; 940 int errorFlag=FALSE;
@@ -933,7 +947,7 @@ static int writeTarFile(const char* tarName, int verboseFlag, int argc,
933 fatalError("Cowardly refusing to create an empty archive\n"); 947 fatalError("Cowardly refusing to create an empty archive\n");
934 948
935 /* Open the tar file for writing. */ 949 /* Open the tar file for writing. */
936 if (strcmp(tarName, "-") == 0) 950 if (tostdoutFlag == TRUE)
937 tbInfo.tarFd = fileno(stdout); 951 tbInfo.tarFd = fileno(stdout);
938 else 952 else
939 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644); 953 tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);