From: Christos Zoulas Date: Mon, 20 Sep 2010 14:14:49 +0000 (+0000) Subject: add list functionality. X-Git-Tag: FILE5_05~68 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=46b33ba7195d466a3ce0ad6f446aaca0da14a072;p=file add list functionality. fix path for windows. --- diff --git a/src/apprentice.c b/src/apprentice.c index 6c9d3018..45b9b296 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.159 2010/07/21 16:47:17 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.160 2010/09/20 14:14:49 christos Exp $") #endif /* lint */ #include "magic.h" @@ -97,6 +97,7 @@ private void eatsize(const char **); private int apprentice_1(struct magic_set *, const char *, int, struct mlist *); private size_t apprentice_magic_strength(const struct magic *); private int apprentice_sort(const void *, const void *); +private void apprentice_list(struct mlist *, int ); private int apprentice_load(struct magic_set *, struct magic **, uint32_t *, const char *, int); private void byteswap(struct magic *, uint32_t); @@ -314,6 +315,13 @@ apprentice_1(struct magic_set *ms, const char *fn, int action, ml->next = mlist; mlist->prev = ml; + if (action == FILE_LIST) { + printf("Binary patterns:\n"); + apprentice_list(mlist, BINTEST); + printf("Text patterns:\n"); + apprentice_list(mlist, TEXTTEST); + } + return 0; #endif /* COMPILE_ONLY */ } @@ -548,6 +556,43 @@ apprentice_sort(const void *a, const void *b) return 1; } +/* + * Shows sorted patterns list in the order which is used for the matching + */ +private void +apprentice_list(struct mlist *mlist, int mode) +{ + uint32_t magindex = 0; + struct mlist *ml; + for (ml = mlist->next; ml != mlist; ml = ml->next) { + for (magindex = 0; magindex < ml->nmagic; magindex++) { + struct magic *m = &ml->magic[magindex]; + if ((m->flag & mode) != mode) { + /* Skip sub-tests */ + while (ml->magic[magindex + 1].cont_level != 0 + && ++magindex < ml->nmagic) + continue; + continue; /* Skip to next top-level test*/ + } + + /* + * Try to iterate over the tree until we find item with + * description/mimetype. + */ + while (ml->magic[magindex + 1].cont_level != 0 && + *ml->magic[magindex].desc == '\0' && + *ml->magic[magindex].mimetype == '\0' && + magindex + 1 < ml->nmagic) + magindex++; + + printf("Strength = %3" SIZE_T_FORMAT "u : %s [%s]\n", + apprentice_magic_strength(m), + ml->magic[magindex].desc, + ml->magic[magindex].mimetype); + } + } +} + private void set_test_type(struct magic *mstart, struct magic *m) { @@ -816,7 +861,8 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, if (marray[i].mp->cont_level == 0) break; if (i != marraycount) { - ms->line = marray[i].mp->lineno; /* XXX - Ugh! */ + /* XXX - Ugh! */ + ms->line = marray[i].mp->lineno; file_magwarn(ms, "level 0 \"default\" did not sort last"); } @@ -1341,7 +1387,8 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp, l = t - 1; break; case CHAR_COMPACT_WHITESPACE: - m->str_flags |= STRING_COMPACT_WHITESPACE; + m->str_flags |= + STRING_COMPACT_WHITESPACE; break; case CHAR_COMPACT_OPTIONAL_WHITESPACE: m->str_flags |= @@ -1365,8 +1412,8 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp, default: if (ms->flags & MAGIC_CHECK) file_magwarn(ms, - "string extension `%c' invalid", - *l); + "string extension `%c' " + "invalid", *l); return -1; } /* allow multiple '/' for readability */ @@ -1533,7 +1580,8 @@ out: } /* - * Parse an Apple CREATOR/TYPE annotation from magic file and put it into magic[index - 1] + * Parse an Apple CREATOR/TYPE annotation from magic file and put it into + * magic[index - 1] */ private int parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) @@ -1543,14 +1591,15 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; if (m->apple[0] != '\0') { - file_magwarn(ms, "Current entry already has a APPLE type `%.8s'," - " new type `%s'", m->mimetype, l); + file_magwarn(ms, "Current entry already has a APPLE type " + "`%.8s', new type `%s'", m->mimetype, l); return -1; } EATAB; - for (i = 0; *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l)) - || strchr("-+/.", *l)) && i < sizeof(m->apple); m->apple[i++] = *l++) + for (i = 0; *l && ((isascii((unsigned char)*l) && + isalnum((unsigned char)*l)) || strchr("-+/.", *l)) && + i < sizeof(m->apple); m->apple[i++] = *l++) continue; if (i == sizeof(m->apple) && *l) { /* We don't need to NUL terminate here, printing handles it */ @@ -1583,8 +1632,9 @@ parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) } EATAB; - for (i = 0; *l && ((isascii((unsigned char)*l) && isalnum((unsigned char)*l)) - || strchr("-+/.", *l)) && i < sizeof(m->mimetype); m->mimetype[i++] = *l++) + for (i = 0; *l && ((isascii((unsigned char)*l) && + isalnum((unsigned char)*l)) || strchr("-+/.", *l)) && + i < sizeof(m->mimetype); m->mimetype[i++] = *l++) continue; if (i == sizeof(m->mimetype)) { m->mimetype[sizeof(m->mimetype) - 1] = '\0'; diff --git a/src/file.c b/src/file.c index 3b73c566..4d3fbf89 100644 --- a/src/file.c +++ b/src/file.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.136 2009/12/06 23:18:04 rrt Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.137 2010/09/20 14:14:49 christos Exp $") #endif /* lint */ #include "magic.h" @@ -73,15 +73,16 @@ int getopt_long(int argc, char * const *argv, const char *optstring, const struc #include "patchlevel.h" #ifdef S_IFLNK -#define FILE_FLAGS "-bchikLNnprsvz0" +#define FILE_FLAGS "-bchikLlNnprsvz0" #else -#define FILE_FLAGS "-bcikNnprsvz0" +#define FILE_FLAGS "-bciklNnprsvz0" #endif # define USAGE \ "Usage: %s [" FILE_FLAGS \ "] [--apple] [--mime-encoding] [--mime-type]\n" \ - " [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...\n" \ + " [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \ + "file ...\n" \ " %s -C [-m magicfiles]\n" \ " %s [--help]\n" @@ -106,7 +107,7 @@ private const struct option long_options[] = { #undef OPT_LONGONLY {0, 0, NULL, 0} }; -#define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0" +#define OPTSTRING "bcCde:f:F:hiklLm:nNprsvz0" private const struct { const char *name; @@ -227,6 +228,9 @@ main(int argc, char *argv[]) case 'k': flags |= MAGIC_CONTINUE; break; + case 'l': + action = FILE_LIST; + break; case 'm': magicfile = optarg; break; @@ -281,6 +285,7 @@ main(int argc, char *argv[]) switch(action) { case FILE_CHECK: case FILE_COMPILE: + case FILE_LIST: /* * Don't try to check/compile ~/.magic unless we explicitly * ask for it. @@ -291,8 +296,19 @@ main(int argc, char *argv[]) strerror(errno)); return 1; } - c = action == FILE_CHECK ? magic_check(magic, magicfile) : - magic_compile(magic, magicfile); + switch(action) { + case FILE_CHECK: + c = magic_check(magic, magicfile); + break; + case FILE_COMPILE: + c = magic_compile(magic, magicfile); + break; + case FILE_LIST: + c = magic_list(magic, magicfile); + break; + default: + abort(); + } if (c == -1) { (void)fprintf(stderr, "%s: %s\n", progname, magic_error(magic)); diff --git a/src/file.h b/src/file.h index 0aaa2dcf..cfe5f45d 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.125 2010/07/21 16:47:17 christos Exp $ + * @(#)$File: file.h,v 1.126 2010/09/20 14:14:49 christos Exp $ */ #ifndef __file_h__ @@ -125,6 +125,7 @@ #define FILE_LOAD 0 #define FILE_CHECK 1 #define FILE_COMPILE 2 +#define FILE_LIST 3 union VALUETYPE { uint8_t b; diff --git a/src/file_opts.h b/src/file_opts.h index 1a73e873..bb8d0a0a 100644 --- a/src/file_opts.h +++ b/src/file_opts.h @@ -33,6 +33,7 @@ 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") #ifdef S_IFLNK +OPT('l', "list", 0, " list magic strength\n") OPT('L', "dereference", 0, " follow symlinks (default)\n") OPT('h', "no-dereference", 0, " don't follow symlinks\n") #endif diff --git a/src/magic.c b/src/magic.c index 632bb540..bcb7000e 100644 --- a/src/magic.c +++ b/src/magic.c @@ -33,7 +33,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.68 2010/08/20 21:17:06 christos Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.69 2010/09/20 14:14:49 christos Exp $") #endif /* lint */ #include "magic.h" @@ -110,8 +110,7 @@ get_default_magic(void) static const char hmagic[] = "/.magic/magic.mgc"; static char default_magic[2 * MAXPATHLEN + 2]; char *home; - char hmagicpath[MAXPATHLEN + 1] = {0}; - static const char pathsep[] = { PATHSEP, '\0' }; + char hmagicpath[MAXPATHLEN + 1] = { 0 }; #ifndef WIN32 if ((home = getenv("HOME")) == NULL) @@ -126,8 +125,9 @@ get_default_magic(void) hmagicpath, MAGIC); #else char *hmagicp = hmagicpath; - char tmppath[MAXPATHLEN + 1] = {0}; + char tmppath[MAXPATHLEN + 1] = { 0 }; char *hmagicend = &hmagicpath[sizeof(hmagicpath) - 1]; + static const char pathsep[] = { PATHSEP, '\0' }; #define APPENDPATH() \ if (access(tmppath, R_OK) != -1) diff --git a/src/magic.h b/src/magic.h index 5a6af44b..d87523de 100644 --- a/src/magic.h +++ b/src/magic.h @@ -84,6 +84,7 @@ int magic_setflags(magic_t, int); int magic_load(magic_t, const char *); int magic_compile(magic_t, const char *); int magic_check(magic_t, const char *); +int magic_list(magic_t, const char *); int magic_errno(magic_t); #ifdef __cplusplus