Added support for ignoring '-g' per GNU ls, thanks to David Vrabel
[oweals/busybox.git] / tar.c
diff --git a/tar.c b/tar.c
index 732f26ddca21490572ad8651a32d25ba64967715..611bbd9a015271d52ec63829f639d5e6981aed3a 100644 (file)
--- a/tar.c
+++ b/tar.c
@@ -2,9 +2,9 @@
 /*
  * Mini tar implementation for busybox 
  *
- * Note, that as of BusyBox 0.43 tar has been completely rewritten from the
- * ground up.  It still has remnents of the old code lying about, but it pretty
- * different (i.e. cleaner, less global variables, etc)
+ * Note, that as of BusyBox-0.43, tar has been completely rewritten from the
+ * ground up.  It still has remnents of the old code lying about, but it is
+ * very different now (i.e. cleaner, less global variables, etc)
  *
  * Copyright (C) 2000 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
@@ -49,7 +49,6 @@
 #include <utime.h>
 #include <sys/types.h>
 #include <sys/sysmacros.h>
-#include <sys/param.h>                 /* for PATH_MAX */
 
 
 static const char tar_usage[] =
@@ -61,8 +60,9 @@ static const char tar_usage[] =
 #if defined BB_FEATURE_TAR_EXCLUDE
        "[--exclude File] "
 #endif
-       "[-f tarFile] [FILE] ...\n\n"
-       "Create, extract, or list files from a tar file.  Note that\n"
+       "[-f tarFile] [FILE] ...\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nCreate, extract, or list files from a tar file.  Note that\n"
        "this version of tar treats hard links as separate files.\n\n"
        "Main operation mode:\n"
 #ifdef BB_FEATURE_TAR_CREATE
@@ -70,14 +70,15 @@ static const char tar_usage[] =
 #endif
        "\tx\t\textract\n"
        "\tt\t\tlist\n"
-       "File selection:\n"
+       "\nFile selection:\n"
        "\tf\t\tname of tarfile or \"-\" for stdin\n"
        "\tO\t\textract to stdout\n"
 #if defined BB_FEATURE_TAR_EXCLUDE
        "\t--exclude\tfile to exclude\n"
 #endif
-       "Informative output:\n"
+       "\nInformative output:\n"
        "\tv\t\tverbosely list files processed\n"
+#endif
        ;
 
 /* Tar file constants  */
@@ -172,7 +173,7 @@ extern int tar_main(int argc, char **argv)
 #if defined BB_FEATURE_TAR_EXCLUDE
        int excludeListSize=0;
 #endif
-       const char *tarName=NULL;
+       const char *tarName="-";
        int listFlag     = FALSE;
        int extractFlag  = FALSE;
        int createFlag   = FALSE;
@@ -184,15 +185,15 @@ extern int tar_main(int argc, char **argv)
                usage(tar_usage);
 
        /* Parse any options */
-       while (--argc > 0 && **(++argv) == '-') {
+       while (--argc > 0 && strspn(*(++argv), "-cxt") >0 ) {
                stopIt=FALSE;
-               while (stopIt==FALSE && *(++(*argv))) {
+               while (stopIt==FALSE && *argv && **argv) {
                        switch (**argv) {
                                case 'f':
                                        if (--argc == 0) {
                                                fatalError( "Option requires an argument: No file specified\n");
                                        }
-                                       if (tarName != NULL)
+                                       if (*tarName != '-')
                                                fatalError( "Only one 'f' option allowed\n");
                                        tarName = *(++argv);
                                        if (tarName == NULL)
@@ -245,13 +246,16 @@ extern int tar_main(int argc, char **argv)
                                                break;
                                        }
 #endif
-                                       usage(tar_usage);
+                                       if (strcmp(*argv, "-help")==0) {
+                                               usage(tar_usage);
+                                       }
                                        break;
 
                                default:
                                        fatalError( "Unknown tar flag '%c'\n" 
                                                        "Try `tar --help' for more information\n", **argv);
                        }
+                       ++(*argv);
                }
        }
 
@@ -265,7 +269,8 @@ extern int tar_main(int argc, char **argv)
 #else
                exit(writeTarFile(tarName, tostdoutFlag, verboseFlag, argc, argv, excludeList));
 #endif
-       } else {
+       }
+       if (listFlag == TRUE || extractFlag == TRUE) {
                exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, excludeList));
        }
 
@@ -298,11 +303,14 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
 
        /* Open the file to be written, if a file is supposed to be written */
        if (extractFlag==TRUE && tostdoutFlag==FALSE) {
-               if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0)
-                       errorMsg(io_error, header->name, strerror(errno)); 
                /* Create the path to the file, just in case it isn't there...
                 * This should not screw up path permissions or anything. */
                createPath(header->name, 0777);
+               if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, 
+                                               header->mode & ~S_IFMT)) < 0) {
+                       errorMsg(io_error, header->name, strerror(errno)); 
+                       return( FALSE);
+               }
        }
 
        /* Write out the file, if we are supposed to be doing that */
@@ -493,18 +501,6 @@ readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
        chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
        header->type  = rawHeader->typeflag;
        header->linkname  = rawHeader->linkname;
-       /* Check for and relativify any absolute paths */
-       if ( *(header->linkname) == '/' ) {
-               static int alreadyWarned=FALSE;
-
-               while (*(header->linkname) == '/')
-                       ++*(header->linkname);
-
-               if (alreadyWarned == FALSE) {
-                       errorMsg("tar: Removing leading '/' from link names\n");
-                       alreadyWarned = TRUE;
-               }
-       }
        header->devmajor  = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
        header->devminor  = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
 
@@ -621,7 +617,7 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
                                                header.devmajor, header.devminor);
                        } else {
-                               len1=snprintf(buf, sizeof(buf), "%d ", header.size);
+                               len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
                        }
                        /* Jump through some hoops to make the columns match up */
                        for(;(len+len1)<31;len++)
@@ -652,7 +648,8 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                }
 
                /* Remove any clutter lying in our way */
-               unlink( header.name);
+               if (extractFlag == TRUE)        /* .. but only if we are extracting (as */
+                       unlink( header.name);   /* opposed to listing) (rob@sysgo.de)   */
 
                /* If we got here, we can be certain we have a legitimate 
                 * header to work with.  So work with it.  */
@@ -826,25 +823,18 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
        if (! *header.uname)
                strcpy(header.uname, "root");
 
-       // FIXME (or most likely not): I break Hard Links
+       /* WARNING/NOTICE: I break Hard Links */
        if (S_ISLNK(statbuf->st_mode)) {
+               int link_size=0;
                char buffer[BUFSIZ];
                header.typeflag  = SYMTYPE;
-               if ( readlink(fileName, buffer, sizeof(buffer) - 1) < 0) {
+               link_size = readlink(fileName, buffer, sizeof(buffer) - 1);
+               if ( link_size < 0) {
                        errorMsg("Error reading symlink '%s': %s\n", header.name, strerror(errno));
                        return ( FALSE);
                }
-               if (*buffer=='/') {
-                       static int alreadyWarned=FALSE;
-                       if (alreadyWarned==FALSE) {
-                               errorMsg("tar: Removing leading '/' from link names\n");
-                               alreadyWarned=TRUE;
-                       }
-                       strncpy(header.linkname, buffer+1, sizeof(header.linkname)); 
-               }
-               else {
-                       strncpy(header.linkname, buffer, sizeof(header.linkname)); 
-               }
+               buffer[link_size] = '\0';
+               strncpy(header.linkname, buffer, sizeof(header.linkname)); 
        } else if (S_ISDIR(statbuf->st_mode)) {
                header.typeflag  = DIRTYPE;
                strncat(header.name, "/", sizeof(header.name));