summaryrefslogtreecommitdiff
path: root/src/file.c
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2015-01-02 20:23:27 +0000
committer <>2015-02-03 17:27:18 +0000
commit670c2bbcffe873a2b8589ed140c12e7923ef20c0 (patch)
tree41044880e826d60621a2d636ed71283de5e0e291 /src/file.c
parent3b49db406667ee7189b9ea69b9d9e0bdcc43c5b7 (diff)
downloadfile-670c2bbcffe873a2b8589ed140c12e7923ef20c0.tar.gz
Imported from /home/lorry/working-area/delta_file/file-5.22.tar.gz.file-5.22
Diffstat (limited to 'src/file.c')
-rw-r--r--src/file.c80
1 files changed, 70 insertions, 10 deletions
diff --git a/src/file.c b/src/file.c
index aa7d5b3..546fd8b 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.149 2013/01/07 18:20:19 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.160 2014/12/16 23:18:40 christos Exp $")
#endif /* lint */
#include "magic.h"
@@ -54,9 +54,6 @@ FILE_RCSID("@(#)$File: file.c,v 1.149 2013/01/07 18:20:19 christos Exp $")
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for read() */
#endif
-#ifdef HAVE_LOCALE_H
-#include <locale.h>
-#endif
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
@@ -71,9 +68,9 @@ int getopt_long(int argc, char * const *argv, const char *optstring, const struc
#endif
#ifdef S_IFLNK
-#define FILE_FLAGS "-bchikLlNnprsvz0"
+#define FILE_FLAGS "-bcEhikLlNnprsvz0"
#else
-#define FILE_FLAGS "-bciklNnprsvz0"
+#define FILE_FLAGS "-bcEiklNnprsvz0"
#endif
# define USAGE \
@@ -101,7 +98,7 @@ private const struct option long_options[] = {
#undef OPT_LONGONLY
{0, 0, NULL, 0}
};
-#define OPTSTRING "bcCde:f:F:hiklLm:nNprsvz0"
+#define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsvz0"
private const struct {
const char *name;
@@ -119,6 +116,18 @@ private const struct {
{ "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
};
+private struct {
+ const char *name;
+ int tag;
+ size_t value;
+} pm[] = {
+ { "indir", MAGIC_PARAM_INDIR_MAX, 0 },
+ { "name", MAGIC_PARAM_NAME_MAX, 0 },
+ { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 },
+ { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 },
+ { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0 },
+};
+
private char *progname; /* used throughout */
private void usage(void);
@@ -128,6 +137,8 @@ private void help(void);
private int unwrap(struct magic_set *, const char *);
private int process(struct magic_set *ms, const char *, int);
private struct magic_set *load(const char *, int);
+private void setparam(const char *);
+private void applyparam(magic_t);
/*
@@ -145,7 +156,9 @@ main(int argc, char *argv[])
const char *magicfile = NULL; /* where the magic is */
/* makes islower etc work for other langs */
+#ifdef HAVE_SETLOCALE
(void)setlocale(LC_CTYPE, "");
+#endif
#ifdef __EMX__
/* sh-like wildcard expansion! Shouldn't hurt at least ... */
@@ -194,6 +207,9 @@ main(int argc, char *argv[])
case 'd':
flags |= MAGIC_DEBUG|MAGIC_CHECK;
break;
+ case 'E':
+ flags |= MAGIC_ERROR;
+ break;
case 'e':
for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
if (strcmp(nv[i].name, optarg) == 0)
@@ -240,9 +256,13 @@ main(int argc, char *argv[])
flags |= MAGIC_PRESERVE_ATIME;
break;
#endif
+ case 'P':
+ setparam(optarg);
+ break;
case 'r':
flags |= MAGIC_RAW;
break;
+ break;
case 's':
flags |= MAGIC_DEVICES;
break;
@@ -295,6 +315,8 @@ main(int argc, char *argv[])
strerror(errno));
return 1;
}
+
+
switch(action) {
case FILE_CHECK:
c = magic_check(magic, magicfile);
@@ -318,7 +340,7 @@ main(int argc, char *argv[])
if (magic == NULL)
if ((magic = load(magicfile, flags)) == NULL)
return 1;
- break;
+ applyparam(magic);
}
if (optind == argc) {
@@ -348,6 +370,41 @@ main(int argc, char *argv[])
return e;
}
+private void
+applyparam(magic_t magic)
+{
+ size_t i;
+
+ for (i = 0; i < __arraycount(pm); i++) {
+ if (pm[i].value == 0)
+ continue;
+ if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
+ (void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
+ pm[i].name, strerror(errno));
+ exit(1);
+ }
+ }
+}
+
+private void
+setparam(const char *p)
+{
+ size_t i;
+ char *s;
+
+ if ((s = strchr(p, '=')) == NULL)
+ goto badparm;
+
+ for (i = 0; i < __arraycount(pm); i++) {
+ if (strncmp(p, pm[i].name, s - p) != 0)
+ continue;
+ pm[i].value = atoi(s + 1);
+ return;
+ }
+badparm:
+ (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
+ exit(1);
+}
private struct magic_set *
/*ARGSUSED*/
@@ -465,8 +522,11 @@ file_mbswidth(const char *s)
* is always right
*/
width++;
- } else
- width += wcwidth(nextchar);
+ } else {
+ int w = wcwidth(nextchar);
+ if (w > 0)
+ width += w;
+ }
s += bytesconsumed, n -= bytesconsumed;
}