]> granicus.if.org Git - file/commitdiff
Add --extension
authorChristos Zoulas <christos@zoulas.com>
Thu, 9 Apr 2015 20:01:40 +0000 (20:01 +0000)
committerChristos Zoulas <christos@zoulas.com>
Thu, 9 Apr 2015 20:01:40 +0000 (20:01 +0000)
16 files changed:
ChangeLog
TODO
doc/file.man
doc/libmagic.man
magic/Magdir/jpeg
magic/Magdir/mathematica
src/apprentice.c
src/ascmagic.c
src/file.c
src/file.h
src/file_opts.h
src/fsmagic.c
src/is_tar.c
src/readcdf.c
src/readelf.c
src/softmagic.c

index 9ee8a1fff0003ddee7a33b794f4c27b82a12f8b7..ac392bed9d8c1949899bdba64716b9dace9da342 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2015-04-09  15:59  Christos Zoulas <christos@zoulas.com>
+
+       * Add --extension to list the known extensions for this file type
+         Idea by Andrew J Roazen
+
 2015-02-14  12:23  Christos Zoulas <christos@zoulas.com>
 
        * Bump file search buffer size to 1M.
diff --git a/TODO b/TODO
index eef01edb241bbc1bb09f96ee3ca6153ef13fdb6c..5c2ed49e02f110c96ae98f7c79b2e605a21ec0fd 100644 (file)
--- a/TODO
+++ b/TODO
@@ -17,3 +17,6 @@ hard-wired routines). In this regard, note that hplip, which is
 BSD-licensed, has a magic reimplementation in Python.
 
 Read the kerberos magic entry for more ideas.
+
+Write a string merger to make magic entry sizes dynamic.
+Strings will be converted to offsets from the string table.
index 714f1928a496dcff77d100f2178591b97e432889..6a1fd9041b7370cd5a727f2fe80f68c5b5e22369 100644 (file)
@@ -1,5 +1,5 @@
-.\" $File: file.man,v 1.112 2015/02/14 17:24:30 christos Exp $
-.Dd February 14, 2015
+.\" $File: file.man,v 1.113 2015/04/09 20:01:40 christos Exp $
+.Dd April 9, 2015
 .Dt FILE __CSECTION__
 .Os
 .Sh NAME
@@ -10,6 +10,7 @@
 .Bk -words
 .Op Fl bcEhiklLNnprsvz0
 .Op Fl Fl apple
+.Op Fl Fl extension
 .Op Fl Fl mime-encoding
 .Op Fl Fl mime-type
 .Op Fl e Ar testname
@@ -213,6 +214,8 @@ Consults magic files.
 .It tar
 Examines tar files.
 .El
+.It Fl Fl extension 
+Print a comma-separated list of valid extensions for the file type found.
 .It Fl F , Fl Fl separator Ar separator
 Use the specified string as the separator between the filename and the
 file result returned.
index 3c907cad9b35f4a0924941f762d2d7febbb200ac..933e4233a9f71d9291268be9b83d9868fafcc9ae 100644 (file)
@@ -1,4 +1,4 @@
-.\" $File: libmagic.man,v 1.34 2014/12/16 23:18:40 christos Exp $
+.\" $File: libmagic.man,v 1.35 2015/04/09 20:01:40 christos Exp $
 .\"
 .\" Copyright (c) Christos Zoulas 2003.
 .\" All Rights Reserved.
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 16, 2014
+.Dd April 9, 2015
 .Dt LIBMAGIC 3
 .Os
 .Sh NAME
@@ -132,6 +132,8 @@ Treat operating system errors while trying to open files and follow symlinks
 as real errors, instead of printing them in the magic buffer.
 .It Dv MAGIC_APPLE
 Return the Apple creator and type.
+.It Dv MAGIC_EXTENSION
+Return a comma-separated list of extensions for this file type.
 .It Dv MAGIC_NO_CHECK_APPTYPE
 Don't check for
 .Dv EMX
index 1c5cc51753cb9ada0cf8e4c33f03f0b1c73dbbda..daa86602584664a4e2f6b30cda7c6bc1cb352bff 100644 (file)
@@ -1,6 +1,6 @@
 
 #------------------------------------------------------------------------------
-# $File: jpeg,v 1.27 2015/02/06 17:08:31 christos Exp $
+# $File: jpeg,v 1.28 2015/04/09 20:01:40 christos Exp $
 # JPEG images
 # SunOS 5.5.1 had
 #
@@ -13,6 +13,7 @@
 !:mime image/jpeg
 !:apple        8BIMJPEG
 !:strength *3
+!:ext jpeg,jpg,jpe,jfif
 >6     string          JFIF            \b, JFIF standard
 # The following added by Erik Rossen <rossen@freesurf.ch> 1999-09-06
 # in a vain attempt to add image size reporting for JFIF.  Note that these
index 3c10a57db5c82b7c50303423ef5126199631ff27..a93899e83c2e14873144c4b7e8d732b24e7891f9 100644 (file)
@@ -1,6 +1,6 @@
 
 #------------------------------------------------------------------------------
-# $File: mathematica,v 1.7 2009/09/19 16:28:10 christos Exp $
+# $File: mathematica,v 1.8 2015/04/09 20:01:40 christos Exp $
 # mathematica:  file(1) magic for mathematica files
 # "H. Nanosecond" <aldomel@ix.netcom.com>
 # Mathematica a multi-purpose math program
@@ -8,13 +8,16 @@
 
 #mathematica .mb
 0      string  \064\024\012\000\035\000\000\000        Mathematica version 2 notebook
+!:ext mb
 0      string  \064\024\011\000\035\000\000\000        Mathematica version 2 notebook
+!:ext mb
 
 # .ma
 # multiple possibilites:
 
 0      string  (*^\n\n::[\011frontEndVersion\ =\       Mathematica notebook
 #>41   string  >\0     %s
+!:ext mb
 
 #0     string  (*^\n\n::[\011palette   Mathematica notebook version 2.x
 
 
 # generic:
 0      string  (*^\r\r::[\011  Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\r\n\r\n::[\011      Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\015                 Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\n\r\n\r::[\011      Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\r::[\011    Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\r\n::[\011  Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\n\n::[\011  Mathematica notebook version 2.x
+!:ext mb
 0      string  (*^\n::[\011    Mathematica notebook version 2.x
+!:ext mb
 
 
 # Mathematica .mx files
index 5b076b3ba75cedcc219f196a4178076a9b5c37f4..e810ee76723748caf4c65146d4916359fba491e8 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.231 2015/02/06 17:08:58 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.232 2015/04/09 20:01:40 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -149,6 +149,7 @@ private int get_op(char);
 private int parse_mime(struct magic_set *, struct magic_entry *, const char *);
 private int parse_strength(struct magic_set *, struct magic_entry *, const char *);
 private int parse_apple(struct magic_set *, struct magic_entry *, const char *);
+private int parse_ext(struct magic_set *, struct magic_entry *, const char *);
 
 
 private size_t magicsize = sizeof(struct magic);
@@ -163,6 +164,7 @@ private struct {
 #define        DECLARE_FIELD(name) { # name, sizeof(# name) - 1, parse_ ## name }
        DECLARE_FIELD(mime),
        DECLARE_FIELD(apple),
+       DECLARE_FIELD(ext),
        DECLARE_FIELD(strength),
 #undef DECLARE_FIELD
        { NULL, 0, NULL }
@@ -2254,6 +2256,19 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line)
            sizeof(m->apple), "APPLE", "!+-./", 0);
 }
 
+/*
+ * Parse a comma-separated list of extensions
+ */
+private int
+parse_ext(struct magic_set *ms, struct magic_entry *me, const char *line)
+{
+       struct magic *m = &me->mp[0];
+
+       return parse_extra(ms, me, line,
+           CAST(off_t, offsetof(struct magic, ext)),
+           sizeof(m->ext), "EXTENSION", ",!+-/", 0);
+}
+
 /*
  * parse a MIME annotation line from magic file, put into magic[index - 1]
  * if valid
index 78a6dbb7c84884693d9d1cdfa564c9ffb3c0b755..9e0f663781695197be860bcaccf4368001eca631 100644 (file)
@@ -35,7 +35,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: ascmagic.c,v 1.91 2014/11/28 02:46:39 christos Exp $")
+FILE_RCSID("@(#)$File: ascmagic.c,v 1.92 2015/04/09 20:01:41 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -79,7 +79,7 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
        const char *code_mime = NULL;
        const char *type = NULL;
 
-       if (ms->flags & MAGIC_APPLE)
+       if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
                return 0;
 
        nbytes = trim_nuls(buf, nbytes);
@@ -123,7 +123,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
        size_t last_line_end = (size_t)-1;
        int has_long_lines = 0;
 
-       if (ms->flags & MAGIC_APPLE)
+       if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
                return 0;
 
        nbytes = trim_nuls(buf, nbytes);
index 81723789574891e3ecb9806641e21524503bec8a..45202a0daf4861bc1345a9b64221495f7fa280c4 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.162 2015/02/09 20:15:50 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.163 2015/04/09 20:01:41 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -75,7 +75,7 @@ int getopt_long(int argc, char * const *argv, const char *optstring, const struc
 
 # define USAGE  \
     "Usage: %s [" FILE_FLAGS \
-       "] [--apple] [--mime-encoding] [--mime-type]\n" \
+       "] [--apple] [--extension] [--mime-encoding] [--mime-type]\n" \
     "            [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \
     "file ...\n" \
     "       %s -C [-m magicfiles]\n" \
@@ -191,9 +191,12 @@ main(int argc, char *argv[])
                                flags |= MAGIC_APPLE;
                                break;
                        case 11:
-                               flags |= MAGIC_MIME_TYPE;
+                               flags |= MAGIC_EXTENSION;
                                break;
                        case 12:
+                               flags |= MAGIC_MIME_TYPE;
+                               break;
+                       case 13:
                                flags |= MAGIC_MIME_ENCODING;
                                break;
                        }
index 08bbd12b7ca063aae1645fac906a86f9b4ba63f5..1b4ef6f7a2cc800b4d2ecb35289827b975cc6c0f 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.167 2015/02/14 17:19:29 christos Exp $
+ * @(#)$File: file.h,v 1.168 2015/04/09 20:01:41 christos Exp $
  */
 
 #ifndef __file_h__
 #define MAXstring 64           /* max len of "string" types */
 
 #define MAGICNO                0xF11E041C
-#define VERSIONNO      12
-#define FILE_MAGICSIZE 248
+#define VERSIONNO      13
+#define FILE_MAGICSIZE 312
 
 #define        FILE_LOAD       0
 #define FILE_CHECK     1
@@ -307,7 +307,9 @@ struct magic {
        /* Words 33-52 */
        char mimetype[MAXMIME]; /* MIME type */
        /* Words 53-54 */
-       char apple[8];
+       char apple[8];          /* APPLE CREATOR/TYPE */
+       /* Words 55-63 */
+       char ext[64];           /* Popular extensions */
 };
 
 #define BIT(A)   (1 << (A))
index 3286ac607ec68201874a3553d1f04150f0e56662..79ced3a4018d43a50c8e148f4937702737d6af80 100644 (file)
@@ -29,6 +29,7 @@ OPT('F', "separator", 1, " STRING     use string as separator instead of `:'\n")
 OPT('i', "mime", 0, "                 output MIME type strings (--mime-type and\n"
     "                               --mime-encoding)\n")
 OPT_LONGONLY("apple", 0, "                output the Apple CREATOR/TYPE\n")
+OPT_LONGONLY("extension", 0, "            output a comma-separated list of extnsions\n")
 OPT_LONGONLY("mime-type", 0, "            output the MIME type\n")
 OPT_LONGONLY("mime-encoding", 0, "        output the MIME encoding\n")
 OPT('k', "keep-going", 0, "           don't stop at the first match\n")
index 1e8fd74d2a2077f4e7755fdb36ba706969902457..27f982a58348e1733b7ac3b4d70c615aa144a13a 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: fsmagic.c,v 1.75 2014/12/04 15:56:46 christos Exp $")
+FILE_RCSID("@(#)$File: fsmagic.c,v 1.76 2015/04/09 20:01:41 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -110,7 +110,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
        struct stat tstatbuf;
 #endif
 
-       if (ms->flags & MAGIC_APPLE)
+       if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
                return 0;
        if (fn == NULL)
                return 0;
index 876c631bfdcbc1135211e3ec8b6e44d5abd29c75..a3e5dbf24c36c9a7aad7f043c58f1fb67b3e406f 100644 (file)
@@ -40,7 +40,7 @@
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: is_tar.c,v 1.37 2010/11/30 14:58:53 rrt Exp $")
+FILE_RCSID("@(#)$File: is_tar.c,v 1.38 2015/04/09 20:01:41 christos Exp $")
 #endif
 
 #include "magic.h"
@@ -69,7 +69,7 @@ file_is_tar(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
        int tar;
        int mime = ms->flags & MAGIC_MIME;
 
-       if ((ms->flags & MAGIC_APPLE) != 0)
+       if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
                return 0;
 
        tar = is_tar(buf, nbytes);
index 50aa6d6833eeda9235f81754af4c075aa11257ff..99d8ec01800b172743e4e14d18daa87a93b469d9 100644 (file)
@@ -26,7 +26,7 @@
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: readcdf.c,v 1.52 2015/02/27 21:16:38 christos Exp $")
+FILE_RCSID("@(#)$File: readcdf.c,v 1.53 2015/04/09 20:01:41 christos Exp $")
 #endif
 
 #include <assert.h>
@@ -454,7 +454,7 @@ file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
         info.i_fd = fd;
         info.i_buf = buf;
         info.i_len = nbytes;
-        if (ms->flags & MAGIC_APPLE)
+        if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
                 return 0;
         if (cdf_read_header(&info, &h) == -1)
                 return 0;
index 49b101e489133e36cd63021fdcea0f5acca2161e..55009e80a08f58d007d1335cf5aa821625a8edd3 100644 (file)
@@ -27,7 +27,7 @@
 #include "file.h"
 
 #ifndef lint
-FILE_RCSID("@(#)$File: readelf.c,v 1.118 2015/01/02 21:29:39 christos Exp $")
+FILE_RCSID("@(#)$File: readelf.c,v 1.119 2015/04/09 20:01:41 christos Exp $")
 #endif
 
 #ifdef BUILTIN_ELF
@@ -1353,7 +1353,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
        Elf64_Ehdr elf64hdr;
        uint16_t type, phnum, shnum, notecount;
 
-       if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
+       if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION))
                return 0;
        /*
         * ELF executables have multiple section headers in arbitrary
index ad663af3d5a39d3b569507bcf8e43622c605be98..09c2ff62614ffbd9a4b726af95cd964cb42aacd4 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.213 2015/02/14 18:43:12 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.214 2015/04/09 20:01:41 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -147,7 +147,7 @@ match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
        unsigned int cont_level = 0;
        int returnvalv = 0, e; /* if a match is found it is set to 1*/
        int firstline = 1; /* a flag to print X\n  X\n- X */
-       int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
+       int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION)) == 0;
 
        if (returnval == NULL)
                returnval = &returnvalv;
@@ -1674,7 +1674,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
                        return -1;
 
                if (rv == 1) {
-                       if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
+                       if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION)) == 0 &&
                            file_printf(ms, F(ms, m, "%u"), offset) == -1) {
                                free(rbuf);
                                return -1;
@@ -2136,6 +2136,11 @@ handle_annotation(struct magic_set *ms, struct magic *m)
                        return -1;
                return 1;
        }
+       if (ms->flags & MAGIC_EXTENSION) {
+               if (file_printf(ms, "%s", m->ext) == -1)
+                       return -1;
+               return 1;
+       }
        if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
                if (file_printf(ms, "%s", m->mimetype) == -1)
                        return -1;