[hackers] [sbase] tar: Allow extracting only a given list of files || sin

From: <git_AT_suckless.org>
Date: Tue, 21 Apr 2015 16:43:58 +0200 (CEST)

commit 258d0793ac830d247f8c7b54c7749f04f583faa4
Author: sin <sin_AT_2f30.org>
Date: Tue Apr 21 15:29:22 2015 +0100

    tar: Allow extracting only a given list of files
    
    tar -xf foo.tar a b c

diff --git a/tar.c b/tar.c
index 44523ff..bb4677b 100644
--- a/tar.c
+++ b/tar.c
_AT_@ -313,26 +313,34 @@ sanitize(struct header *h)
 }
 
 static void
-xt(int (*fn)(char *, ssize_t, char[BLKSIZ]))
+xt(int argc, char *argv[], int (*fn)(char *, ssize_t, char[BLKSIZ]))
 {
         char b[BLKSIZ], fname[256 + 1], *p;
- struct header *h;
+ struct header *h = (struct header *)b;
         long size;
- int n;
+ int i, n;
 
- h = (void *)b;
+ while (fread(b, BLKSIZ, 1, tarfile) == 1 && *h->name) {
+ sanitize(h), n = 0;
 
- while (fread(b, BLKSIZ, 1, tarfile) == 1 && *(h->name)) {
- sanitize(h);
- n = 0;
+ /* small dance around non-null terminated fields */
                 if (h->prefix[0])
                         n = snprintf(fname, sizeof(fname), "%.*s/",
                                      (int)sizeof(h->prefix), h->prefix);
                 snprintf(fname + n, sizeof(fname) - n, "%.*s",
                          (int)sizeof(h->name), h->name);
+
+ if (argc) {
+ /* only extract the given files */
+ for (i = 0; i < argc; i++)
+ if (!strcmp(argv[i], fname))
+ break;
+ if (i == argc)
+ continue;
+ }
+
                 if ((size = strtol(h->size, &p, 8)) < 0 || *p != '\0')
                         eprintf("strtol %s: invalid number\n", h->size);
-
                 fn(fname, size, b);
         }
         if (ferror(tarfile))
_AT_@ -430,7 +438,7 @@ main(int argc, char *argv[])
 
                 if (chdir(dir) < 0)
                         eprintf("chdir %s:", dir);
- xt((mode == 'x') ? unarchive : print);
+ xt(argc, argv, (mode == 'x') ? unarchive : print);
                 break;
         }
 
Received on Tue Apr 21 2015 - 16:43:58 CEST

This archive was generated by hypermail 2.3.0 : Tue Apr 21 2015 - 16:48:23 CEST