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 e205763dbfc4108c403a7804ed6dbd4027d0f3f1..611bbd9a015271d52ec63829f639d5e6981aed3a 100644 (file)
--- a/tar.c
+++ b/tar.c
@@ -60,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
@@ -77,6 +78,7 @@ static const char tar_usage[] =
 #endif
        "\nInformative output:\n"
        "\tv\t\tverbosely list files processed\n"
+#endif
        ;
 
 /* Tar file constants  */
@@ -171,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;
@@ -183,15 +185,15 @@ extern int tar_main(int argc, char **argv)
                usage(tar_usage);
 
        /* Parse any options */
-       while (--argc > 0 && (**(++argv) != '\0')) {
+       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)
@@ -244,12 +246,16 @@ extern int tar_main(int argc, char **argv)
                                                break;
                                        }
 #endif
+                                       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);
                }
        }
 
@@ -263,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));
        }
 
@@ -296,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 */
@@ -638,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.  */
@@ -814,12 +825,15 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
 
        /* 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);
                }
+               buffer[link_size] = '\0';
                strncpy(header.linkname, buffer, sizeof(header.linkname)); 
        } else if (S_ISDIR(statbuf->st_mode)) {
                header.typeflag  = DIRTYPE;