Brand new version of xargs. Tested thoroughly by Kent Robotti. (Domo arigato,
[oweals/busybox.git] / tar.c
diff --git a/tar.c b/tar.c
index 3e1a65062499e77a056021833c6d03c7d2ff3d30..07c0e71052db07e69764057245dbf0e8dd982601 100644 (file)
--- a/tar.c
+++ b/tar.c
  */
 
 
-#include "internal.h"
+#include "busybox.h"
 #define BB_DECLARE_EXTERN
 #define bb_need_io_error
+#define bb_need_name_longer_than_foo
 #include "messages.c"
 #include <stdio.h>
 #include <dirent.h>
 #define MINOR(dev) ((dev)&0xff)
 #endif
 
+#define NAME_SIZE      100
 
 /* POSIX tar Header Block, from POSIX 1003.1-1990  */
 struct TarHeader
 {
                                 /* byte offset */
-       char name[100];               /*   0-99 */
+       char name[NAME_SIZE];         /*   0-99 */
        char mode[8];                 /* 100-107 */
        char uid[8];                  /* 108-115 */
        char gid[8];                  /* 116-123 */
@@ -70,7 +72,7 @@ struct TarHeader
        char mtime[12];               /* 136-147 */
        char chksum[8];               /* 148-155 */
        char typeflag;                /* 156-156 */
-       char linkname[100];           /* 157-256 */
+       char linkname[NAME_SIZE];     /* 157-256 */
        char magic[6];                /* 257-262 */
        char version[2];              /* 263-264 */
        char uname[32];               /* 265-296 */
@@ -102,6 +104,8 @@ enum TarFileType
        DIRTYPE  = '5',            /* directory */
        FIFOTYPE = '6',            /* FIFO special */
        CONTTYPE = '7',            /* reserved */
+       GNULONGLINK = 'K',         /* GNU long (>100 chars) link name */
+       GNULONGNAME = 'L',         /* GNU long (>100 chars) file name */
 };
 typedef enum TarFileType TarFileType;
 
@@ -133,17 +137,9 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
 
 #ifdef BB_FEATURE_TAR_CREATE
 /* Local procedures to save files into a tar file.  */
-static int writeTarFile(const char* tarName, int tostdoutFlag, 
-               int verboseFlag, int argc, char **argv, char** excludeList);
-#endif
-
-static struct option longopts[] =
-{
-#ifdef BB_FEATURE_TAR_EXCLUDE
-        {"exclude",required_argument,NULL,'e'},
+static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
+               char** excludeList);
 #endif
-               {NULL,0,NULL,0}
-};
 
 extern int tar_main(int argc, char **argv)
 {
@@ -157,14 +153,18 @@ extern int tar_main(int argc, char **argv)
        int createFlag   = FALSE;
        int verboseFlag  = FALSE;
        int tostdoutFlag = FALSE;
-       int opt;
+       int firstOpt = TRUE;
+       int stopIt;
+                                                                                                                                                  
 
        if (argc <= 1)
                usage(tar_usage);
 
-       /* do normal option parsing */
-       while ((opt = getopt_long(argc, argv, "cxtvOf:", longopts, NULL)) != EOF) {
-               switch (opt) {
+       while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
+               firstOpt=FALSE;
+               stopIt=FALSE;
+               while (stopIt==FALSE && **argv) {
+                       switch (*((*argv)++)) {
                                case 'c':
                                        if (extractFlag == TRUE || listFlag == TRUE)
                                                goto flagError;
@@ -185,28 +185,36 @@ extern int tar_main(int argc, char **argv)
                                        break;
                                case 'O':
                                        tostdoutFlag = TRUE;
-                                       tarName = "-";
                                        break;                                  
                                case 'f':
                                        if (*tarName != '-')
                                                fatalError( "Only one 'f' option allowed\n");
-                                       tarName = optarg;
-                                       if (!strcmp(tarName, "-") && createFlag == TRUE)
-                                               tostdoutFlag = TRUE;
+                                       tarName = *(++argv);
+                                       if (tarName == NULL)
+                                               fatalError( "Option requires an argument: No file specified\n");
+                                       stopIt=TRUE;
                                        break;
 #if defined BB_FEATURE_TAR_EXCLUDE
                                case 'e':
-                                       excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
-                                       excludeList[excludeListSize] = optarg;
-                                       /* Remove leading "/"s */
-                                       if (*excludeList[excludeListSize] =='/')
-                                               excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
-                                       /* Tack a NULL onto the end of the list */
-                                       excludeList[++excludeListSize] = NULL;
-                                       break;
+                                       if (strcmp(*argv, "exclude")==0) {
+                                               excludeList=xrealloc( excludeList, sizeof(char**) * (excludeListSize+2));
+                                               excludeList[excludeListSize] = *(++argv);
+                                               if (excludeList[excludeListSize] == NULL)
+                                                       fatalError( "Option requires an argument: No file specified\n");
+                                               /* Remove leading "/"s */
+                                               if (*excludeList[excludeListSize] =='/')
+                                                       excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
+                                               /* Tack a NULL onto the end of the list */
+                                               excludeList[++excludeListSize] = NULL;
+                                               stopIt=TRUE;
+                                               break;
+                                       }
 #endif
+                               case '-':
+                                               break;
                                default:
                                        usage(tar_usage);
+                       }
                }
        }
 
@@ -218,11 +226,12 @@ extern int tar_main(int argc, char **argv)
 #ifndef BB_FEATURE_TAR_CREATE
                fatalError( "This version of tar was not compiled with tar creation support.\n");
 #else
-               exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc-optind, &argv[optind], excludeList));
+               exit(writeTarFile(tarName, verboseFlag, argv, excludeList));
 #endif
        }
        if (listFlag == TRUE || extractFlag == TRUE) {
-               exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, &argv[optind], excludeList));
+               exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag,
+                                       verboseFlag, argv, excludeList));
        }
 
   flagError:
@@ -482,6 +491,7 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
 {
        int status, tarFd=-1;
        int errorFlag=FALSE;
+       int skipNextHeaderFlag=FALSE;
        TarHeader rawHeader;
        TarInfo header;
        char** tmpList;
@@ -503,7 +513,7 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
        /* Read the tar file, and iterate over it one file at a time */
        while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
 
-               /* First, try to read the header */
+               /* Try to read the header */
                if ( readTarHeader(&rawHeader, &header) == FALSE ) {
                        if ( *(header.name) == '\0' ) {
                                goto endgame;
@@ -517,6 +527,19 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                goto endgame;
                header.tarFd = tarFd;
 
+               /* Skip funky extra GNU headers that precede long files */
+               if ( (header.type == GNULONGNAME) || (header.type == GNULONGLINK) ) {
+                       skipNextHeaderFlag=TRUE;
+                       tarExtractRegularFile(&header, FALSE, FALSE);
+                       continue;
+               }
+               if ( skipNextHeaderFlag == TRUE ) { 
+                       skipNextHeaderFlag=FALSE;
+                       errorMsg(name_longer_than_foo, NAME_SIZE); 
+                       tarExtractRegularFile(&header, FALSE, FALSE);
+                       continue;
+               }
+
 #if defined BB_FEATURE_TAR_EXCLUDE
                {
                        int skipFlag=FALSE;
@@ -657,7 +680,15 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                if (tarExtractSpecial( &header, extractFlag, tostdoutFlag)==FALSE)
                                        errorFlag=TRUE;
                                break;
+#if 0
+                       /* Handled earlier */
+                       case GNULONGNAME:
+                       case GNULONGLINK:
+                               skipNextHeaderFlag=TRUE;
+                               break;
+#endif
                        default:
+                               errorMsg("Unknown file type '%c' in tar file\n", header.type);
                                close( tarFd);
                                return( FALSE);
                }
@@ -883,6 +914,11 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
                return( TRUE);
        }
 
+       if (strlen(fileName) >= NAME_SIZE) {
+               errorMsg(name_longer_than_foo, NAME_SIZE);
+               return ( TRUE);
+       }
+
        if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
                return( FALSE);
        } 
@@ -922,8 +958,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
        return( TRUE);
 }
 
-static int writeTarFile(const char* tarName, int tostdoutFlag, 
-               int verboseFlag, int argc, char **argv, char** excludeList)
+static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
+               char** excludeList)
 {
        int tarFd=-1;
        int errorFlag=FALSE;
@@ -932,11 +968,11 @@ static int writeTarFile(const char* tarName, int tostdoutFlag,
        tbInfo.verboseFlag = verboseFlag;
 
        /* Make sure there is at least one file to tar up.  */
-       if (argc <= 0)
+       if (*argv == NULL)
                fatalError("Cowardly refusing to create an empty archive\n");
 
        /* Open the tar file for writing.  */
-       if (tostdoutFlag == TRUE)
+       if (!strcmp(tarName, "-"))
                tbInfo.tarFd = fileno(stdout);
        else
                tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
@@ -955,7 +991,7 @@ static int writeTarFile(const char* tarName, int tostdoutFlag,
        umask(0);
 
        /* Read the directory/files and iterate over them one at a time */
-       while (argc-- > 0) {
+       while (*argv != NULL) {
                if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
                                        writeFileToTarball, writeFileToTarball, 
                                        (void*) &tbInfo) == FALSE) {
@@ -968,7 +1004,7 @@ static int writeTarFile(const char* tarName, int tostdoutFlag,
        }
 
        /* To be pedantically correct, we would check if the tarball
-        * is smaller then 20 tar blocks, and pad it if it was smaller,
+        * is smaller than 20 tar blocks, and pad it if it was smaller,
         * but that isn't necessary for GNU tar interoperability, and
         * so is considered a waste of space */