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 6784d80ff0cd2ee05a5ca3f78233687267656ba5..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>
 #include <utime.h>
 #include <sys/types.h>
 #include <sys/sysmacros.h>
-
-
-static const char tar_usage[] =
-#ifdef BB_FEATURE_TAR_CREATE
-       "tar -[cxtvO] "
-#else
-       "tar -[xtvO] "
-#endif
-#if defined BB_FEATURE_TAR_EXCLUDE
-       "[--exclude File] "
-#endif
-       "[-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
-       "\tc\t\tcreate\n"
-#endif
-       "\tx\t\textract\n"
-       "\tt\t\tlist\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
-       "\nInformative output:\n"
-       "\tv\t\tverbosely list files processed\n"
-#endif
-       ;
+#include <getopt.h>
 
 /* Tar file constants  */
 #ifndef MAJOR
@@ -87,12 +58,13 @@ static const char tar_usage[] =
 #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 */
@@ -100,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 */
@@ -132,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;
 
@@ -156,101 +130,90 @@ typedef struct TarInfo TarInfo;
 
 /* Local procedures to restore files from a tar file.  */
 static int readTarFile(const char* tarName, int extractFlag, int listFlag, 
-               int tostdoutFlag, int verboseFlag, char** excludeList);
+               int tostdoutFlag, int verboseFlag, char** extractList,
+               char** excludeList);
 
 
 
 #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);
+static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
+               char** excludeList);
 #endif
 
-
 extern int tar_main(int argc, char **argv)
 {
        char** excludeList=NULL;
 #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;
        int verboseFlag  = FALSE;
        int tostdoutFlag = FALSE;
+       int firstOpt = TRUE;
        int stopIt;
+                                                                                                                                                  
 
        if (argc <= 1)
                usage(tar_usage);
 
-       /* Parse any options */
-       while (--argc > 0 && **(++argv) == '-') {
+       while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) {
+               firstOpt=FALSE;
                stopIt=FALSE;
-               while (stopIt==FALSE && *(++(*argv))) {
-                       switch (**argv) {
-                               case 'f':
-                                       if (--argc == 0) {
-                                               fatalError( "Option requires an argument: No file specified\n");
-                                       }
-                                       if (tarName != NULL)
-                                               fatalError( "Only one 'f' option allowed\n");
-                                       tarName = *(++argv);
-                                       if (tarName == NULL)
-                                               fatalError( "Option requires an argument: No file specified\n");
-                                       stopIt=TRUE;
-                                       break;
-
-                               case 't':
-                                       if (extractFlag == TRUE || createFlag == TRUE)
+               while (stopIt==FALSE && **argv) {
+                       switch (*((*argv)++)) {
+                               case 'c':
+                                       if (extractFlag == TRUE || listFlag == TRUE)
                                                goto flagError;
-                                       listFlag = TRUE;
+                                       createFlag = TRUE;
                                        break;
-
                                case 'x':
                                        if (listFlag == TRUE || createFlag == TRUE)
                                                goto flagError;
                                        extractFlag = TRUE;
                                        break;
-                               case 'c':
-                                       if (extractFlag == TRUE || listFlag == TRUE)
+                               case 't':
+                                       if (extractFlag == TRUE || createFlag == TRUE)
                                                goto flagError;
-                                       createFlag = TRUE;
+                                       listFlag = TRUE;
                                        break;
-
                                case 'v':
                                        verboseFlag = TRUE;
                                        break;
-
                                case 'O':
                                        tostdoutFlag = TRUE;
-                                       tarName = "-";
+                                       break;                                  
+                               case 'f':
+                                       if (*tarName != '-')
+                                               fatalError( "Only one 'f' option allowed\n");
+                                       tarName = *(++argv);
+                                       if (tarName == NULL)
+                                               fatalError( "Option requires an argument: No file specified\n");
+                                       stopIt=TRUE;
                                        break;
-                               case '-':
 #if defined BB_FEATURE_TAR_EXCLUDE
-                                       if (strcmp(*argv, "-exclude")==0) {
-                                               if (--argc == 0) {
-                                                       fatalError( "Option requires an argument: No file specified\n");
-                                               }
-                                               excludeList=realloc( excludeList, sizeof(char**) * (excludeListSize+2));
+                               case 'e':
+                                       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] =='/') {
+                                               if (*excludeList[excludeListSize] =='/')
                                                        excludeList[excludeListSize] = (excludeList[excludeListSize])+1;
-                                               }
-                                               if (excludeList[excludeListSize++] == NULL)
-                                                       fatalError( "Option requires an argument: No file specified\n");
                                                /* Tack a NULL onto the end of the list */
-                                               excludeList[excludeListSize] = NULL;
+                                               excludeList[++excludeListSize] = NULL;
                                                stopIt=TRUE;
                                                break;
                                        }
 #endif
-                                       break;
-
+                               case '-':
+                                               break;
                                default:
-                                       fatalError( "Unknown tar flag '%c'\n" 
-                                                       "Try `tar --help' for more information\n", **argv);
+                                       usage(tar_usage);
                        }
                }
        }
@@ -263,10 +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, argv, excludeList));
+               exit(writeTarFile(tarName, verboseFlag, argv, excludeList));
 #endif
-       } else {
-               exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, excludeList));
+       }
+       if (listFlag == TRUE || extractFlag == TRUE) {
+               exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag,
+                                       verboseFlag, argv, excludeList));
        }
 
   flagError:
@@ -298,11 +263,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 */
@@ -320,7 +288,7 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
                }
                if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
                        /* Tarball seems to have a problem */
-                       errorMsg("tar: Unexpected EOF in archive\n"); 
+                       errorMsg("Unexpected EOF in archive\n"); 
                        return( FALSE);
                }
                if ( readSize < writeSize )
@@ -358,7 +326,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
                return( TRUE);
 
        if (createPath(header->name, header->mode) != TRUE) {
-               errorMsg("tar: %s: Cannot mkdir: %s\n", 
+               errorMsg("%s: Cannot mkdir: %s\n", 
                                header->name, strerror(errno)); 
                return( FALSE);
        }
@@ -378,7 +346,7 @@ tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
                return( TRUE);
 
        if (link(header->linkname, header->name) < 0) {
-               errorMsg("tar: %s: Cannot create hard link to '%s': %s\n", 
+               errorMsg("%s: Cannot create hard link to '%s': %s\n", 
                                header->name, header->linkname, strerror(errno)); 
                return( FALSE);
        }
@@ -396,7 +364,7 @@ tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
 
 #ifdef S_ISLNK
        if (symlink(header->linkname, header->name) < 0) {
-               errorMsg("tar: %s: Cannot create symlink to '%s': %s\n", 
+               errorMsg("%s: Cannot create symlink to '%s': %s\n", 
                                header->name, header->linkname, strerror(errno)); 
                return( FALSE);
        }
@@ -411,7 +379,7 @@ tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
        /* Do not change permissions or date on symlink,
         * since it changes the pointed to file instead.  duh. */
 #else
-       errorMsg("tar: %s: Cannot create symlink to '%s': %s\n", 
+       errorMsg("%s: Cannot create symlink to '%s': %s\n", 
                        header->name, header->linkname, 
                        "symlinks not supported"); 
 #endif
@@ -426,13 +394,13 @@ tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
 
        if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
                if (mknod(header->name, header->mode, makedev(header->devmajor, header->devminor)) < 0) {
-                       errorMsg("tar: %s: Cannot mknod: %s\n",
+                       errorMsg("%s: Cannot mknod: %s\n",
                                header->name, strerror(errno)); 
                        return( FALSE);
                }
        } else if (S_ISFIFO(header->mode)) {
                if (mkfifo(header->name, header->mode) < 0) {
-                       errorMsg("tar: %s: Cannot mkfifo: %s\n",
+                       errorMsg("%s: Cannot mkfifo: %s\n",
                                header->name, strerror(errno)); 
                        return( FALSE);
                }
@@ -480,7 +448,7 @@ readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
                        ++*(header->name);
 
                if (alreadyWarned == FALSE) {
-                       errorMsg("tar: Removing leading '/' from member names\n");
+                       errorMsg("Removing leading '/' from member names\n");
                        alreadyWarned = TRUE;
                }
        }
@@ -518,15 +486,15 @@ readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
  * If the list is empty than all files are extracted or listed.
  */
 static int readTarFile(const char* tarName, int extractFlag, int listFlag, 
-               int tostdoutFlag, int verboseFlag, char** excludeList)
+               int tostdoutFlag, int verboseFlag, char** extractList,
+               char** excludeList)
 {
        int status, tarFd=-1;
        int errorFlag=FALSE;
+       int skipNextHeaderFlag=FALSE;
        TarHeader rawHeader;
        TarInfo header;
-#if defined BB_FEATURE_TAR_EXCLUDE
        char** tmpList;
-#endif
 
        /* Open the tar file for reading.  */
        if (!strcmp(tarName, "-"))
@@ -545,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;
@@ -559,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;
@@ -585,62 +566,89 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                                continue;
                }
 #endif
-               /* Special treatment if the list (-t) flag is on */
-               if (verboseFlag == TRUE && extractFlag == FALSE) {
-                       int len, len1;
-                       char buf[35];
-                       struct tm *tm = localtime (&(header.mtime));
-
-                       len=printf("%s ", modeString(header.mode));
-                       memset(buf, 0, 8*sizeof(char));
-                       my_getpwuid(buf, header.uid);
-                       if (! *buf)
-                               len+=printf("%d", header.uid);
-                       else
-                               len+=printf("%s", buf);
-                       memset(buf, 0, 8*sizeof(char));
-                       my_getgrgid(buf, header.gid);
-                       if (! *buf)
-                               len+=printf("/%-d ", header.gid);
-                       else
-                               len+=printf("/%-s ", buf);
-
-                       if (header.type==CHRTYPE || header.type==BLKTYPE) {
-                               len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
-                                               header.devmajor, header.devminor);
-                       } else {
-                               len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
+               if (*extractList != NULL) {
+                       int skipFlag = TRUE;
+                       for (tmpList = extractList; *tmpList != NULL; tmpList++) {
+                               if (strncmp( *tmpList, header.name, strlen(*tmpList))==0 || (
+                                                       header.name[strlen(header.name)-1]=='/'
+                                                       && strncmp( *tmpList, header.name, 
+                                                               MIN(strlen(header.name)-1, strlen(*tmpList)))==0)) {
+                                       /* If it is a regular file, pretend to extract it with
+                                        * the extractFlag set to FALSE, so the junk in the tarball
+                                        * is properly skipped over */
+                                       skipFlag = FALSE;
+                                       break;
+                               }
                        }
-                       /* Jump through some hoops to make the columns match up */
-                       for(;(len+len1)<31;len++)
-                               printf(" ");
-                       printf(buf);
-
-                       /* Use ISO 8610 time format */
-                       if (tm) { 
-                               printf ("%04d-%02d-%02d %02d:%02d:%02d ", 
-                                               tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
-                                               tm->tm_hour, tm->tm_min, tm->tm_sec);
+                       /* There are not the droids you're looking for, move along */
+                       if (skipFlag == TRUE) {
+                               if ( header.type==REGTYPE || header.type==REGTYPE0 )
+                                               tarExtractRegularFile(&header, FALSE, FALSE);
+                               continue;
                        }
                }
-               /* List contents if we are supposed to do that */
-               if (verboseFlag == TRUE || listFlag == TRUE) {
-                       /* Now the normal listing */
+
+               if (listFlag == TRUE) {
+                       /* Special treatment if the list (-t) flag is on */
+                       if (verboseFlag == TRUE) {
+                               int len, len1;
+                               char buf[35];
+                               struct tm *tm = localtime (&(header.mtime));
+
+                               len=printf("%s ", modeString(header.mode));
+                               memset(buf, 0, 8*sizeof(char));
+                               my_getpwuid(buf, header.uid);
+                               if (! *buf)
+                                       len+=printf("%d", header.uid);
+                               else
+                                       len+=printf("%s", buf);
+                               memset(buf, 0, 8*sizeof(char));
+                               my_getgrgid(buf, header.gid);
+                               if (! *buf)
+                                       len+=printf("/%-d ", header.gid);
+                               else
+                                       len+=printf("/%-s ", buf);
+
+                               if (header.type==CHRTYPE || header.type==BLKTYPE) {
+                                       len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
+                                                       header.devmajor, header.devminor);
+                               } else {
+                                       len1=snprintf(buf, sizeof(buf), "%lu ", (long)header.size);
+                               }
+                               /* Jump through some hoops to make the columns match up */
+                               for(;(len+len1)<31;len++)
+                                       printf(" ");
+                               printf(buf);
+
+                               /* Use ISO 8610 time format */
+                               if (tm) { 
+                                       printf ("%04d-%02d-%02d %02d:%02d:%02d ", 
+                                                       tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
+                                                       tm->tm_hour, tm->tm_min, tm->tm_sec);
+                               }
+                       }
                        printf("%s", header.name);
-               }
-               if (verboseFlag == TRUE && listFlag == TRUE) {
-                       /* If this is a link, say so */
-                       if (header.type==LNKTYPE)
-                               printf(" link to %s", header.linkname);
-                       else if (header.type==SYMTYPE)
-                               printf(" -> %s", header.linkname);
-               }
-               if (verboseFlag == TRUE || listFlag == TRUE) {
+                       if (verboseFlag == TRUE) {
+                               if (header.type==LNKTYPE)       /* If this is a link, say so */
+                                       printf(" link to %s", header.linkname);
+                               else if (header.type==SYMTYPE)
+                                       printf(" -> %s", header.linkname);
+                       }
                        printf("\n");
                }
 
-               /* Remove any clutter lying in our way */
-               unlink( header.name);
+               /* List contents if we are supposed to do that */
+               if (verboseFlag == TRUE && extractFlag == TRUE) {
+                       /* Now the normal listing */
+                       FILE *vbFd = stdout;
+                       if (tostdoutFlag == TRUE)       // If the archive goes to stdout, verbose to stderr
+                               vbFd = stderr;
+                       fprintf(vbFd, "%s\n", header.name);
+               }
+                       
+               /* Remove files if we would overwrite them */
+               if (extractFlag == TRUE && tostdoutFlag == FALSE)
+                       unlink(header.name);
 
                /* If we got here, we can be certain we have a legitimate 
                 * header to work with.  So work with it.  */
@@ -672,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);
                }
@@ -684,7 +700,7 @@ static int readTarFile(const char* tarName, int extractFlag, int listFlag,
                return ( FALSE);
        }
        else if (errorFlag==TRUE) {
-               errorMsg( "tar: Error exit delayed from previous errors\n");
+               errorMsg( "Error exit delayed from previous errors\n");
                return( FALSE);
        } else 
                return( status);
@@ -694,7 +710,7 @@ endgame:
        close( tarFd);
        if ( *(header.name) == '\0' ) {
                if (errorFlag==TRUE)
-                       errorMsg( "tar: Error exit delayed from previous errors\n");
+                       errorMsg( "Error exit delayed from previous errors\n");
                else
                        return( TRUE);
        } 
@@ -765,13 +781,13 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
 #endif
        const unsigned char *cp = (const unsigned char *) &header;
        ssize_t size = sizeof(struct TarHeader);
-
+               
        memset( &header, 0, size);
 
        if (*fileName=='/') {
                static int alreadyWarned=FALSE;
                if (alreadyWarned==FALSE) {
-                       errorMsg("tar: Removing leading '/' from member names\n");
+                       errorMsg("Removing leading '/' from member names\n");
                        alreadyWarned=TRUE;
                }
                strncpy(header.name, fileName+1, sizeof(header.name)); 
@@ -816,12 +832,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;
@@ -840,7 +859,7 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
                header.typeflag  = REGTYPE;
                putOctal(header.size, sizeof(header.size), statbuf->st_size);
        } else {
-               errorMsg("tar: %s: Unknown file type\n", fileName);
+               errorMsg("%s: Unknown file type\n", fileName);
                return ( FALSE);
        }
 
@@ -865,8 +884,12 @@ writeTarHeader(struct TarBallInfo *tbInfo, const char *fileName, struct stat *st
                write(tbInfo->tarFd, "\0", 1);
        }
        /* Now do the verbose thing (or not) */
-       if (tbInfo->verboseFlag==TRUE)
-               fprintf(stdout, "%s\n", header.name);
+       if (tbInfo->verboseFlag==TRUE) {
+               FILE *vbFd = stdout;
+               if (tbInfo->tarFd == fileno(stdout))    // If the archive goes to stdout, verbose to stderr
+                       vbFd = stderr;
+               fprintf(vbFd, "%s\n", header.name);
+       }
 
        return ( TRUE);
 }
@@ -878,7 +901,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
 
        /* It is against the rules to archive a socket */
        if (S_ISSOCK(statbuf->st_mode)) {
-               errorMsg("tar: %s: socket ignored\n", fileName);
+               errorMsg("%s: socket ignored\n", fileName);
                return( TRUE);
        }
 
@@ -887,10 +910,15 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
         * the new tarball */
        if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
                        tbInfo->statBuf.st_ino == statbuf->st_ino) {
-               errorMsg("tar: %s: file is the archive; skipping\n", fileName);
+               errorMsg("%s: file is the archive; skipping\n", fileName);
                return( TRUE);
        }
 
+       if (strlen(fileName) >= NAME_SIZE) {
+               errorMsg(name_longer_than_foo, NAME_SIZE);
+               return ( TRUE);
+       }
+
        if (writeTarHeader(tbInfo, fileName, statbuf)==FALSE) {
                return( FALSE);
        } 
@@ -903,7 +931,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf, void*
 
                /* open the file we want to archive, and make sure all is well */
                if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
-                       errorMsg("tar: %s: Cannot open: %s\n", fileName, strerror(errno));
+                       errorMsg("%s: Cannot open: %s\n", fileName, strerror(errno));
                        return( FALSE);
                }
                
@@ -930,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;
@@ -940,16 +968,16 @@ 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)
-               fatalError("tar: Cowardly refusing to create an empty archive\n");
+       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);
        if (tbInfo.tarFd < 0) {
-               errorMsg( "tar: Error opening '%s': %s\n", tarName, strerror(errno));
+               errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
                return ( FALSE);
        }
        tbInfo.excludeList=excludeList;
@@ -963,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) {
@@ -976,14 +1004,14 @@ 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 */
 
        /* Hang up the tools, close up shop, head home */
        close(tarFd);
        if (errorFlag == TRUE) {
-               errorMsg("tar: Error exit delayed from previous errors\n");
+               errorMsg("Error exit delayed from previous errors\n");
                return(FALSE);
        }
        return( TRUE);