2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * file - find type of a file or files - main program.
35 FILE_RCSID("@(#)$File: file.c,v 1.172 2016/10/24 15:21:07 christos Exp $")
44 # if (__COHERENT__ >= 0x420)
45 # include <sys/utime.h>
48 # include <sys/time.h>
55 #include <unistd.h> /* for read() */
61 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
63 #ifndef HAVE_GETOPT_LONG
64 int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
71 #define FILE_FLAGS "-bcEhikLlNnprsvzZ0"
73 #define FILE_FLAGS "-bcEiklNnprsvzZ0"
77 "Usage: %s [" FILE_FLAGS \
78 "] [--apple] [--extension] [--mime-encoding] [--mime-type]\n" \
79 " [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \
81 " %s -C [-m magicfiles]\n" \
84 private int /* Global command-line options */
85 bflag = 0, /* brief output format */
86 nopad = 0, /* Don't pad output */
87 nobuffer = 0, /* Do not buffer stdout */
88 nulsep = 0; /* Append '\0' to the separator */
90 private const char *separator = ":"; /* Default field separator */
91 private const struct option long_options[] = {
94 #define OPT_EXTENSIONS 3
95 #define OPT_MIME_TYPE 4
96 #define OPT_MIME_ENCODING 5
97 #define OPT(shortname, longname, opt, def, doc) \
98 {longname, opt, NULL, shortname},
99 #define OPT_LONGONLY(longname, opt, def, doc, id) \
100 {longname, opt, NULL, id},
101 #include "file_opts.h"
106 #define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsvzZ0"
108 private const struct {
112 { "apptype", MAGIC_NO_CHECK_APPTYPE },
113 { "ascii", MAGIC_NO_CHECK_ASCII },
114 { "cdf", MAGIC_NO_CHECK_CDF },
115 { "compress", MAGIC_NO_CHECK_COMPRESS },
116 { "elf", MAGIC_NO_CHECK_ELF },
117 { "encoding", MAGIC_NO_CHECK_ENCODING },
118 { "soft", MAGIC_NO_CHECK_SOFT },
119 { "tar", MAGIC_NO_CHECK_TAR },
120 { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */
121 { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
129 { "indir", MAGIC_PARAM_INDIR_MAX, 0 },
130 { "name", MAGIC_PARAM_NAME_MAX, 0 },
131 { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 },
132 { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 },
133 { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0 },
134 { "regex", MAGIC_PARAM_REGEX_MAX, 0 },
135 { "bytes", MAGIC_PARAM_BYTES_MAX, 0 },
138 private char *progname; /* used throughout */
144 private void usage(void);
145 private void docprint(const char *, int);
149 private void help(void);
151 private int unwrap(struct magic_set *, const char *);
152 private int process(struct magic_set *ms, const char *, int);
153 private struct magic_set *load(const char *, int);
154 private void setparam(const char *);
155 private void applyparam(magic_t);
159 * main - parse arguments and handle options
162 main(int argc, char *argv[])
166 int action = 0, didsomefiles = 0, errflg = 0;
167 int flags = 0, e = 0;
168 struct magic_set *magic = NULL;
170 const char *magicfile = NULL; /* where the magic is */
172 /* makes islower etc work for other langs */
173 #ifdef HAVE_SETLOCALE
174 (void)setlocale(LC_CTYPE, "");
178 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
179 _wildcard(&argc, &argv);
182 if ((progname = strrchr(argv[0], '/')) != NULL)
188 posixly = getenv("POSIXLY_CORRECT") != NULL;
189 flags |= posixly ? MAGIC_SYMLINK : 0;
191 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
198 flags |= MAGIC_APPLE;
201 flags |= MAGIC_EXTENSION;
204 flags |= MAGIC_MIME_TYPE;
206 case OPT_MIME_ENCODING:
207 flags |= MAGIC_MIME_ENCODING;
219 action = FILE_COMPILE;
222 flags |= MAGIC_DEBUG|MAGIC_CHECK;
225 flags |= MAGIC_ERROR;
228 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
229 if (strcmp(nv[i].name, optarg) == 0)
232 if (i == sizeof(nv) / sizeof(nv[0]))
235 flags |= nv[i].value;
242 if ((magic = load(magicfile, flags)) == NULL)
245 e |= unwrap(magic, optarg);
255 flags |= MAGIC_CONTINUE;
269 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
271 flags |= MAGIC_PRESERVE_ATIME;
281 flags |= MAGIC_DEVICES;
284 if (magicfile == NULL)
285 magicfile = magic_getpath(magicfile, action);
286 (void)fprintf(stdout, "%s-%s\n", progname, VERSION);
287 (void)fprintf(stdout, "magic file from %s\n",
291 flags |= MAGIC_COMPRESS;
295 flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP;
299 flags |= MAGIC_SYMLINK;
302 flags &= ~MAGIC_SYMLINK;
317 if (MAGIC_VERSION != magic_version())
318 (void)fprintf(stderr, "%s: compiled magic version [%d] "
319 "does not match with shared library magic version [%d]\n",
320 progname, MAGIC_VERSION, magic_version());
327 * Don't try to check/compile ~/.magic unless we explicitly
330 magic = magic_open(flags|MAGIC_CHECK);
332 (void)fprintf(stderr, "%s: %s\n", progname,
340 c = magic_check(magic, magicfile);
343 c = magic_compile(magic, magicfile);
346 c = magic_list(magic, magicfile);
352 (void)fprintf(stderr, "%s: %s\n", progname,
360 if ((magic = load(magicfile, flags)) == NULL)
365 if (optind == argc) {
371 for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
372 nw = file_mbswidth(argv[j]);
377 * If bflag is only set twice, set it depending on
378 * number of files [this is undocumented, and subject to change]
381 bflag = optind >= argc - 1;
383 for (; optind < argc; optind++)
384 e |= process(magic, argv[optind], wid);
394 applyparam(magic_t magic)
398 for (i = 0; i < __arraycount(pm); i++) {
399 if (pm[i].value == 0)
401 if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
402 (void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
403 pm[i].name, strerror(errno));
410 setparam(const char *p)
415 if ((s = strchr(p, '=')) == NULL)
418 for (i = 0; i < __arraycount(pm); i++) {
419 if (strncmp(p, pm[i].name, s - p) != 0)
421 pm[i].value = atoi(s + 1);
425 (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
429 private struct magic_set *
431 load(const char *magicfile, int flags)
433 struct magic_set *magic = magic_open(flags);
437 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
440 if (magic_load(magic, magicfile) == -1) {
441 (void)fprintf(stderr, "%s: %s\n",
442 progname, magic_error(magic));
446 if ((e = magic_error(magic)) != NULL)
447 (void)fprintf(stderr, "%s: Warning: %s\n", progname, e);
452 * unwrap -- read a file of filenames, do each one.
455 unwrap(struct magic_set *ms, const char *fn)
464 if (strcmp("-", fn) == 0) {
468 if ((f = fopen(fn, "r")) == NULL) {
469 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
470 progname, fn, strerror(errno));
474 while ((len = getline(&line, &llen, f)) > 0) {
475 if (line[len - 1] == '\n')
476 line[len - 1] = '\0';
477 cwid = file_mbswidth(line);
485 while ((len = getline(&line, &llen, f)) > 0) {
486 if (line[len - 1] == '\n')
487 line[len - 1] = '\0';
488 e |= process(ms, line, wid);
490 (void)fflush(stdout);
499 * Called for each input file on the command line (or in a list of files)
502 process(struct magic_set *ms, const char *inname, int wid)
504 const char *type, c = nulsep > 1 ? '\0' : '\n';
505 int std_in = strcmp(inname, "-") == 0;
507 if (wid > 0 && !bflag) {
508 (void)printf("%s", std_in ? "/dev/stdin" : inname);
510 (void)putc('\0', stdout);
512 (void)printf("%s", separator);
514 (int) (nopad ? 0 : (wid - file_mbswidth(inname))),
519 type = magic_file(ms, std_in ? NULL : inname);
522 (void)printf("ERROR: %s%c", magic_error(ms), c);
525 (void)printf("%s%c", type, c);
531 file_mbswidth(const char *s)
533 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
534 size_t bytesconsumed, old_n, n, width = 0;
537 (void)memset(&state, 0, sizeof(mbstate_t));
538 old_n = n = strlen(s);
541 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
542 if (bytesconsumed == (size_t)(-1) ||
543 bytesconsumed == (size_t)(-2)) {
544 /* Something went wrong, return something reasonable */
549 * do what strlen() would do, so that caller
554 int w = wcwidth(nextchar);
559 s += bytesconsumed, n -= bytesconsumed;
570 (void)fprintf(stderr, USAGE, progname, progname, progname);
579 if (((def & 1) && posixly) || ((def & 2) && !posixly))
580 fprintf(stdout, " (default)");
585 docprint(const char *opts, int def)
591 p = strstr(opts, "%o");
593 fprintf(stdout, "%s", opts);
598 for (sp = p - 1; sp > opts && *sp == ' '; sp--)
601 fprintf(stdout, "%.*s", (int)(p - opts), opts);
604 for (i = 0; i < __arraycount(nv); i++) {
605 fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
606 if (i && i % 5 == 0) {
607 fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
612 fprintf(stdout, "%s", opts + (p - opts) + 2);
619 "Usage: file [OPTION...] [FILE...]\n"
620 "Determine type of FILEs.\n"
622 #define OPT(shortname, longname, opt, def, doc) \
623 fprintf(stdout, " -%c, --" longname, shortname), \
625 #define OPT_LONGONLY(longname, opt, def, doc, id) \
626 fprintf(stdout, " --" longname), \
628 #include "file_opts.h"
631 fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n");