]> Dogcows Code - chaz/tar/blobdiff - src/suffix.c
Update copyright years.
[chaz/tar] / src / suffix.c
index 6dbc68e2d5a516eddb26f737cda704b9031299ee..cf4d954ad1839bd5eac6ef056fc6e4a32a96b27d 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of GNU tar.
-   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+   Copyright 2007, 2009, 2013-2014 Free Software Foundation, Inc.
 
    Written by Sergey Poznyakoff.
 
@@ -26,55 +26,89 @@ struct compression_suffix
   const char *program;
 };
 
-struct compression_suffix compression_suffixes[] = {
-#define S(s,p) #s, sizeof (#s) - 1, #p
-  { S(gz, gzip) },
-  { S(tgz, gzip) },
-  { S(taz, gzip) },
-  { S(Z, compress) },
-  { S(taZ, compress) },
-  { S(bz2, bzip2) },
-  { S(tbz, bzip2) },
-  { S(tbz2, bzip2) },
-  { S(tz2, bzip2) },
-  { S(lzma, lzma) },
-  { S(tlz, lzma) },
-  { S(lzo, lzop) },
-  { S(xz, xz) },
+static struct compression_suffix compression_suffixes[] = {
+#define __CAT2__(a,b) a ## b
+#define S(s,p) #s, sizeof (#s) - 1, __CAT2__(p,_PROGRAM)
+  { "tar", 3, NULL },
+  { S(gz,   GZIP) },
+  { S(tgz,  GZIP) },
+  { S(taz,  GZIP) },
+  { S(Z,    COMPRESS) },
+  { S(taZ,  COMPRESS) },
+  { S(bz2,  BZIP2) },
+  { S(tbz,  BZIP2) },
+  { S(tbz2, BZIP2) },
+  { S(tz2,  BZIP2) },
+  { S(lz,   LZIP) },
+  { S(lzma, LZMA) },
+  { S(tlz,  LZMA) },
+  { S(lzo,  LZOP) },
+  { S(xz,   XZ) },
+  { S(txz,  XZ) }, /* Slackware */
+  { NULL }
 #undef S
+#undef __CAT2__
 };
 
-int nsuffixes = sizeof (compression_suffixes) /
-                  sizeof (compression_suffixes[0]);
-
-static const char *
-find_compression_program (const char *name, const char *defprog)
+static struct compression_suffix const *
+find_compression_suffix (const char *name, size_t *base_len)
 {
   char *suf = strrchr (name, '.');
-    
+
   if (suf)
     {
-      int i;
       size_t len;
-
+      struct compression_suffix *p;
+      
       suf++;
       len = strlen (suf);
 
-      for (i = 0; i < nsuffixes; i++)
+      for (p = compression_suffixes; p->suffix; p++)
        {
-         if (compression_suffixes[i].length == len
-             && memcmp (compression_suffixes[i].suffix, suf, len) == 0)
-           return compression_suffixes[i].program;
+         if (p->length == len && memcmp (p->suffix, suf, len) == 0)
+           {
+             if (*base_len)
+               *base_len = strlen (name) - len - 1;
+             return p;
+           }
        }
     }
+  return NULL;
+}
+
+static const char *
+find_compression_program (const char *name, const char *defprog)
+{
+  struct compression_suffix const *p = find_compression_suffix (name, NULL);
+  if (p)
+    return p->program;
   return defprog;
 }
 
 void
-set_comression_program_by_suffix (const char *name, const char *defprog)
+set_compression_program_by_suffix (const char *name, const char *defprog)
 {
   const char *program = find_compression_program (name, defprog);
   if (program)
     use_compress_program_option = program;
 }
 
+char *
+strip_compression_suffix (const char *name)
+{
+  char *s = NULL;
+  size_t len;
+
+  if (find_compression_suffix (name, &len))
+    {
+      if (strncmp (name + len - 4, ".tar", 4) == 0)
+       len -= 4;
+      if (len == 0)
+       return NULL;
+      s = xmalloc (len + 1);
+      memcpy (s, name, len);
+      s[len] = 0;
+    }
+  return s;
+}
+  
This page took 0.018156 seconds and 4 git commands to generate.