#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"
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);
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 */
}
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)
{
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");
}
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 |=
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 */
}
/*
- * 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)
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 */
}
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';
#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"
#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"
#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;
case 'k':
flags |= MAGIC_CONTINUE;
break;
+ case 'l':
+ action = FILE_LIST;
+ break;
case 'm':
magicfile = optarg;
break;
switch(action) {
case FILE_CHECK:
case FILE_COMPILE:
+ case FILE_LIST:
/*
* Don't try to check/compile ~/.magic unless we explicitly
* ask for it.
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));
#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"
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)
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)