implemented numeric sort (sort -g)
[oweals/busybox.git] / tar.c
diff --git a/tar.c b/tar.c
index c23805993a00d0625708ca8a7f9c5d0e5c7ebe46..438770c034460f6253a11ed1811bb4a86cee89da 100644 (file)
--- a/tar.c
+++ b/tar.c
@@ -10,6 +10,9 @@
  * Modified for busybox by Erik Andersen <andersee@debian.org>
  * Adjusted to grok stdin/stdout options.
  *
+ * Modified to handle device special files by Matt Porter
+ * <porter@debian.org>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
 #include <fcntl.h>
 #include <signal.h>
 #include <time.h>
-
-/* Note that tar.c expects TRUE and FALSE to be defined
- * exactly the opposite of how they are used everywhere else.
- * Some time this should be integrated a bit better, but this
- * does the job for now.
- */
-//#undef FALSE
-//#undef TRUE
-//#define FALSE ((int) 0)
-//#define TRUE  ((int) 1)
+#include <sys/types.h>
+#include <sys/sysmacros.h>
 
 
 static const char tar_usage[] =
-    "tar -[cxtvOf] [tarFileName] [FILE] ...\n"
-    "Create, extract, or list files from a tar file\n\n"
-    "\tc=create, x=extract, t=list contents, v=verbose,\n"
-    "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
+"tar -[cxtvOf] [tarFileName] [FILE] ...\n\n"
+"Create, extract, or list files from a tar file\n\n"
+"Options:\n"
+"\tc=create, x=extract, t=list contents, v=verbose,\n"
+"\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
 
 
 
@@ -96,18 +92,18 @@ typedef struct {
 /*
  * Static data.
  */
-static int listFlag; //
-static int extractFlag; //
-static int createFlag; //
-static int verboseFlag; //
-static int tostdoutFlag; //
+static int listFlag;
+static int extractFlag;
+static int createFlag;
+static int verboseFlag;
+static int tostdoutFlag;
 
 static int inHeader; // <- check me
-static int badHeader; //
-static int errorFlag; //
-static int skipFileFlag; //
-static int warnedRoot; //
-static int eofFlag; //
+static int badHeader;
+static int errorFlag;
+static int skipFileFlag;
+static int warnedRoot;
+static int eofFlag;
 static long dataCc;
 static int outFd;
 static char outName[TAR_NAME_SIZE];
@@ -136,7 +132,7 @@ static void readHeader (const TarHeader * hp,
 /*
  * Local procedures to save files into a tar file.
  */
-static void saveFile (const char *fileName, int seeLinks); //
+static void saveFile (const char *fileName, int seeLinks);
 
 static void saveRegularFile (const char *fileName,
                             const struct stat *statbuf);
@@ -145,13 +141,13 @@ static void saveDirectory (const char *fileName,
                           const struct stat *statbuf);
 
 static int wantFileName (const char *fileName,
-                        int fileCount, char **fileTable); //
+                        int fileCount, char **fileTable);
 
 static void writeHeader (const char *fileName, const struct stat *statbuf);
 
 static void writeTarFile (int fileCount, char **fileTable);
 static void writeTarBlock (const char *buf, int len);
-static int putOctal (char *cp, int len, long value); //
+static int putOctal (char *cp, int len, long value);
 
 
 extern int tar_main (int argc, char **argv)
@@ -217,10 +213,13 @@ extern int tar_main (int argc, char **argv)
                break;
 
            case '-':
+               usage( tar_usage);
                break;
 
            default:
-               fprintf (stderr, "Unknown tar flag '%c'\n", *options);
+               fprintf (stderr, "Unknown tar flag '%c'\n"
+                       "Try `tar --help' for more information\n", 
+                       *options);
 
                exit (FALSE);
            }
@@ -230,7 +229,6 @@ extern int tar_main (int argc, char **argv)
     /* 
      * Validate the options.
      */
-    fprintf(stderr, "TRUE=%d FALSE=%d\n", TRUE, FALSE);
     if (extractFlag + listFlag + createFlag != (TRUE+FALSE+FALSE)) {
        fprintf (stderr,
                 "Exactly one of 'c', 'x' or 't' must be specified\n");
@@ -279,7 +277,7 @@ static void readTarFile (int fileCount, char **fileTable)
      * Open the tar file for reading.
      */
     if ((tarName == NULL) || !strcmp (tarName, "-")) {
-       tarFd = STDIN;
+       tarFd = fileno(stdin);
     } else
        tarFd = open (tarName, O_RDONLY);
 
@@ -384,12 +382,15 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
     int uid;
     int gid;
     int checkSum;
+    unsigned int major;
+    unsigned int minor;
     long size;
     time_t mtime;
     const char *name;
     int cc;
     int hardLink;
     int softLink;
+    int devFileFlag;
 
     /* 
      * If the block is completely empty, then this is the end of the
@@ -418,6 +419,8 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
     size = getOctal (hp->size, sizeof (hp->size));
     mtime = getOctal (hp->mtime, sizeof (hp->mtime));
     checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum));
+    major = getOctal (hp->devMajor, sizeof (hp->devMajor));
+    minor = getOctal (hp->devMinor, sizeof (hp->devMinor));
 
     if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) {
        if (badHeader==FALSE)
@@ -430,6 +433,7 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
 
     badHeader = FALSE;
     skipFileFlag = FALSE;
+    devFileFlag = FALSE;
 
     /* 
      * Check for the file modes.
@@ -441,12 +445,10 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
                (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0'));
 
     /* 
-     * Check for a directory or a regular file.
+     * Check for a directory.
      */
     if (name[strlen (name) - 1] == '/')
        mode |= S_IFDIR;
-    else if ((mode & S_IFMT) == 0)
-       mode |= S_IFREG;
 
     /* 
      * Check for absolute paths in the file.
@@ -469,7 +471,8 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
      * If not, then set up to skip it.
      */
     if (wantFileName (name, fileCount, fileTable) == FALSE) {
-       if (!hardLink && !softLink && S_ISREG (mode)) {
+       if ( !hardLink && !softLink && (S_ISREG (mode) || S_ISCHR (mode)
+                   || S_ISBLK (mode) || S_ISSOCK(mode) || S_ISFIFO(mode) ) ) {
            inHeader = (size == 0)? TRUE : FALSE;
            dataCc = size;
        }
@@ -485,16 +488,20 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
      */
     if (extractFlag==FALSE) {
        if (verboseFlag==TRUE) {
-           printf ("%s %3d/%-d %9ld %s %s", modeString (mode),
-                   uid, gid, size, timeString (mtime), name);
-       } else
-           printf ("%s", name);
+           printf ("%s %3d/%-d ", modeString (mode), uid, gid);
+           if( S_ISCHR (mode) || S_ISBLK (mode) )
+               printf ("%4d,%4d %s ", major,minor, timeString (mtime));
+           else
+               printf ("%9ld %s ", size, timeString (mtime));
+       }
+       printf ("%s", name);
 
        if (hardLink)
            printf (" (link to \"%s\")", hp->linkName);
        else if (softLink)
            printf (" (symlink to \"%s\")", hp->linkName);
-       else if (S_ISREG (mode)) {
+       else if (S_ISREG (mode) || S_ISCHR (mode) || S_ISBLK (mode) || 
+               S_ISSOCK(mode) || S_ISFIFO(mode) ) {
            inHeader = (size == 0)? TRUE : FALSE;
            dataCc = size;
        }
@@ -513,7 +520,8 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
     if (hardLink) {
        if (link (hp->linkName, name) < 0)
            perror (name);
-
+       chmod(name, mode);
+       chown(name, uid, gid);
        return;
     }
 
@@ -521,24 +529,32 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
 #ifdef S_ISLNK
        if (symlink (hp->linkName, name) < 0)
            perror (name);
+       chmod(name, mode);
+       chown(name, uid, gid);
 #else
        fprintf (stderr, "Cannot create symbolic links\n");
 #endif
        return;
     }
 
+    /* Set the umask for this process so it doesn't 
+     * screw things up. */
+    umask(0);
+
     /* 
      * If the file is a directory, then just create the path.
      */
     if (S_ISDIR (mode)) {
        createPath (name, mode);
+       chmod(name, mode);
+       chown(name, uid, gid);
 
        return;
     }
 
     /* 
      * There is a file to write.
-     * First create the path to it if necessary with a default permission.
+     * First create the path to it if necessary with default permissions.
      */
     createPath (name, 0777);
 
@@ -549,20 +565,34 @@ readHeader (const TarHeader * hp, int fileCount, char **fileTable)
      * Start the output file.
      */
     if (tostdoutFlag == TRUE)
-       outFd = STDOUT;
-    else
-       outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode);
+       outFd = fileno(stdout);
+    else {
+       if ( S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) ) {
+           devFileFlag = TRUE;
+           outFd = mknod (name, mode, makedev(major, minor) );
+       }
+       else if (S_ISFIFO(mode) ) {
+           devFileFlag = TRUE;
+           outFd = mkfifo(name, mode);
+       } else {
+           outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode);
+       }
+    }
 
     if (outFd < 0) {
        perror (name);
        skipFileFlag = TRUE;
        return;
     }
+    if (tostdoutFlag == FALSE) {
+       fchmod(outFd, mode);
+       fchown(outFd, uid, gid);
+    }
 
     /* 
      * If the file is empty, then that's all we need to do.
      */
-    if (size == 0 && tostdoutFlag == FALSE) {
+    if (size == 0 && (tostdoutFlag == FALSE) && (devFileFlag == FALSE)) {
        (void) close (outFd);
        outFd = -1;
     }
@@ -637,7 +667,7 @@ static void writeTarFile (int fileCount, char **fileTable)
      */
     if ((tarName == NULL) || !strcmp (tarName, "-")) {
        tostdoutFlag = TRUE;
-       tarFd = STDOUT;
+       tarFd = fileno(stdout);
     } else
        tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666);
 
@@ -658,7 +688,7 @@ static void writeTarFile (int fileCount, char **fileTable)
 
     tarDev = statbuf.st_dev;
     tarInode = statbuf.st_ino;
-
+               
     /* 
      * Append each file name into the archive file.
      * Follow symbolic links for these top level file names.
@@ -742,12 +772,16 @@ static void saveFile (const char *fileName, int seeLinks)
 
        return;
     }
-
     if (S_ISREG (mode)) {
        saveRegularFile (fileName, &statbuf);
 
        return;
     }
+    
+    /* Some day add support for tarring these up... but not today. :) */
+//  if (S_ISLNK(mode) || S_ISFIFO(mode) || S_ISBLK(mode) || S_ISCHR (mode) ) {
+//     fprintf (stderr, "%s: This version of tar can't store this type of file\n", fileName);
+//  }
 
     /* 
      * The file is a strange type of file, ignore it.