aboutsummaryrefslogtreecommitdiff
path: root/tar.c
diff options
context:
space:
mode:
authorandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-08-02 18:48:26 +0000
committerandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-08-02 18:48:26 +0000
commit1cc94bd9ac7134781d9fe9dfecb487fd51978334 (patch)
tree06e7faef9293569687b853590742c91e1e36d940 /tar.c
parent78e93f0ce2824e11fbb48e50b620da09add86dc9 (diff)
downloadbusybox-w32-1cc94bd9ac7134781d9fe9dfecb487fd51978334.tar.gz
busybox-w32-1cc94bd9ac7134781d9fe9dfecb487fd51978334.tar.bz2
busybox-w32-1cc94bd9ac7134781d9fe9dfecb487fd51978334.zip
Merge in two patches from Dave Cinege:
the first is a cleanup of tar --exclude the second changes mount so mtab works more as it should, and also allows mount to use the traditional short form (i.e. 'mount / -o remount,rw' now works. While inside tar, I changed it to use getopt... -Erik git-svn-id: svn://busybox.net/trunk/busybox@937 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'tar.c')
-rw-r--r--tar.c94
1 files changed, 40 insertions, 54 deletions
diff --git a/tar.c b/tar.c
index 3227ce5bd..cab53aa10 100644
--- a/tar.c
+++ b/tar.c
@@ -148,85 +148,65 @@ extern int tar_main(int argc, char **argv)
148 int createFlag = FALSE; 148 int createFlag = FALSE;
149 int verboseFlag = FALSE; 149 int verboseFlag = FALSE;
150 int tostdoutFlag = FALSE; 150 int tostdoutFlag = FALSE;
151 int stopIt; 151 int opt;
152 152
153 if (argc <= 1) 153 if (argc <= 1)
154 usage(tar_usage); 154 usage(tar_usage);
155 155
156 /* Parse any options */ 156 /* do normal option parsing */
157 while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) { 157 while ((opt = getopt(argc, argv, "cxtvOf:-:")) > 0) {
158 stopIt=FALSE; 158 switch (opt) {
159 while (stopIt==FALSE && *argv && **argv) { 159 case 'c':
160 switch (**argv) { 160 if (extractFlag == TRUE || listFlag == TRUE)
161 case 'f':
162 if (--argc == 0) {
163 fatalError( "Option requires an argument: No file specified\n");
164 }
165 if (*tarName != '-')
166 fatalError( "Only one 'f' option allowed\n");
167 tarName = *(++argv);
168 if (tarName == NULL)
169 fatalError( "Option requires an argument: No file specified\n");
170 if (!strcmp(tarName, "-") && createFlag == TRUE)
171 tostdoutFlag = TRUE;
172 stopIt=TRUE;
173 break;
174
175 case 't':
176 if (extractFlag == TRUE || createFlag == TRUE)
177 goto flagError; 161 goto flagError;
178 listFlag = TRUE; 162 createFlag = TRUE;
179 break; 163 break;
180
181 case 'x': 164 case 'x':
182 if (listFlag == TRUE || createFlag == TRUE) 165 if (listFlag == TRUE || createFlag == TRUE)
183 goto flagError; 166 goto flagError;
184 extractFlag = TRUE; 167 extractFlag = TRUE;
185 break; 168 break;
186 case 'c': 169 case 't':
187 if (extractFlag == TRUE || listFlag == TRUE) 170 if (extractFlag == TRUE || createFlag == TRUE)
188 goto flagError; 171 goto flagError;
189 createFlag = TRUE; 172 listFlag = TRUE;
190 break; 173 break;
191
192 case 'v': 174 case 'v':
193 verboseFlag = TRUE; 175 verboseFlag = TRUE;
194 break; 176 break;
195
196 case 'O': 177 case 'O':
197 tostdoutFlag = TRUE; 178 tostdoutFlag = TRUE;
198 tarName = "-"; 179 tarName = "-";
180 break;
181 case 'f':
182 if (*tarName != '-')
183 fatalError( "Only one 'f' option allowed\n");
184 tarName = optarg;
185 if (!strcmp(tarName, "-") && createFlag == TRUE)
186 tostdoutFlag = TRUE;
199 break; 187 break;
200 case '-': 188 case '-':
201#if defined BB_FEATURE_TAR_EXCLUDE 189#if defined BB_FEATURE_TAR_EXCLUDE
202 if (strcmp(*argv, "-exclude")==0) { 190 if (strcmp(optarg, "exclude")==0) {
203 if (--argc == 0) { 191 if (argv[optind]==NULL)
204 fatalError( "Option requires an argument: No file specified\n"); 192 fatalError( "option `--exclude' requires an argument\n");
205 }
206 excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2)); 193 excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2));
207 excludeList[excludeListSize] = *(++argv); 194 excludeList[excludeListSize] = argv[optind];
208 /* Remove leading "/"s */ 195 /* Remove leading "/"s */
209 if (*excludeList[excludeListSize] =='/') { 196 if (*excludeList[excludeListSize] =='/') {
210 excludeList[excludeListSize] = (excludeList[excludeListSize])+1; 197 excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
211 } 198 }
212 if (excludeList[excludeListSize++] == NULL)
213 fatalError( "Option requires an argument: No file specified\n");
214 /* Tack a NULL onto the end of the list */ 199 /* Tack a NULL onto the end of the list */
215 excludeList[excludeListSize] = NULL; 200 excludeList[excludeListSize] = NULL;
216 stopIt=TRUE; 201 optind++;
217 break; 202 break;
218 } 203 }
219#endif 204#endif
220 if (strcmp(*argv, "-help")==0) { 205 fatalError( "Unknown tar flag '%s'\n"
221 usage(tar_usage); 206 "Try `tar --help' for more information\n", optarg);
222 }
223 break;
224
225 default: 207 default:
226 fatalError( "Unknown tar flag '%c'\n" 208 fatalError( "Unknown tar flag '%c'\n"
227 "Try `tar --help' for more information\n", **argv); 209 "Try `tar --help' for more information\n", **argv);
228 }
229 ++(*argv);
230 } 210 }
231 } 211 }
232 212
@@ -238,7 +218,7 @@ extern int tar_main(int argc, char **argv)
238#ifndef BB_FEATURE_TAR_CREATE 218#ifndef BB_FEATURE_TAR_CREATE
239 fatalError( "This version of tar was not compiled with tar creation support.\n"); 219 fatalError( "This version of tar was not compiled with tar creation support.\n");
240#else 220#else
241 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList)); 221 exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc-optind, &argv[optind], excludeList));
242#endif 222#endif
243 } 223 }
244 if (listFlag == TRUE || extractFlag == TRUE) { 224 if (listFlag == TRUE || extractFlag == TRUE) {
@@ -603,18 +583,20 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
603 } 583 }
604 } 584 }
605 /* List contents if we are supposed to do that */ 585 /* List contents if we are supposed to do that */
606 if (verboseFlag == TRUE || listFlag == TRUE) { 586 if (verboseFlag == TRUE && listFlag != TRUE) {
607 /* Now the normal listing */ 587 /* Now the normal listing */
608 printf("%s", header.name); 588 FILE *vbFd = stdout;
589 if (tostdoutFlag == TRUE) // If the archive goes to stdout, verbose to stderr
590 vbFd = stderr;
591 fprintf(vbFd, "%s\n", header.name);
609 } 592 }
593
610 if (verboseFlag == TRUE && listFlag == TRUE) { 594 if (verboseFlag == TRUE && listFlag == TRUE) {
611 /* If this is a link, say so */ 595 printf("%s", header.name);
612 if (header.type==LNKTYPE) 596 if (header.type==LNKTYPE) /* If this is a link, say so */
613 printf(" link to %s", header.linkname); 597 printf(" link to %s", header.linkname);
614 else if (header.type==SYMTYPE) 598 else if (header.type==SYMTYPE)
615 printf(" -> %s", header.linkname); 599 printf(" -> %s", header.linkname);
616 }
617 if (verboseFlag == TRUE || listFlag == TRUE) {
618 printf("\n"); 600 printf("\n");
619 } 601 }
620 602
@@ -745,7 +727,7 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
745#endif 727#endif
746 const unsigned char *cp = (const unsigned char *) &header; 728 const unsigned char *cp = (const unsigned char *) &header;
747 ssize_t size = sizeof(struct TarHeader); 729 ssize_t size = sizeof(struct TarHeader);
748 730
749 memset( &header, 0, size); 731 memset( &header, 0, size);
750 732
751 if (*fileName=='/') { 733 if (*fileName=='/') {
@@ -848,8 +830,12 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
848 write(tbInfo->tarFd, "\0", 1); 830 write(tbInfo->tarFd, "\0", 1);
849 } 831 }
850 /* Now do the verbose thing (or not) */ 832 /* Now do the verbose thing (or not) */
851 if (tbInfo->verboseFlag==TRUE) 833 if (tbInfo->verboseFlag==TRUE) {
852 fprintf(stdout, "%s\n", header.name); 834 FILE *vbFd = stdout;
835 if (tbInfo->tarFd == fileno(stdout)) // If the archive goes to stdout, verbose to stderr
836 vbFd = stderr;
837 fprintf(vbFd, "%s\n", header.name);
838 }
853 839
854 return ( TRUE); 840 return ( TRUE);
855} 841}