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.160 2014/12/16 23:18:40 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 "-bcEhikLlNnprsvz0"
73 #define FILE_FLAGS "-bcEiklNnprsvz0"
77 "Usage: %s [" FILE_FLAGS \
78 "] [--apple] [--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[] = {
92 #define OPT(shortname, longname, opt, doc) \
93 {longname, opt, NULL, shortname},
94 #define OPT_LONGONLY(longname, opt, doc) \
95 {longname, opt, NULL, 0},
96 #include "file_opts.h"
101 #define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsvz0"
103 private const struct {
107 { "apptype", MAGIC_NO_CHECK_APPTYPE },
108 { "ascii", MAGIC_NO_CHECK_ASCII },
109 { "cdf", MAGIC_NO_CHECK_CDF },
110 { "compress", MAGIC_NO_CHECK_COMPRESS },
111 { "elf", MAGIC_NO_CHECK_ELF },
112 { "encoding", MAGIC_NO_CHECK_ENCODING },
113 { "soft", MAGIC_NO_CHECK_SOFT },
114 { "tar", MAGIC_NO_CHECK_TAR },
115 { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */
116 { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
124 { "indir", MAGIC_PARAM_INDIR_MAX, 0 },
125 { "name", MAGIC_PARAM_NAME_MAX, 0 },
126 { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 },
127 { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 },
128 { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0 },
131 private char *progname; /* used throughout */
133 private void usage(void);
134 private void docprint(const char *);
135 private void help(void);
137 private int unwrap(struct magic_set *, const char *);
138 private int process(struct magic_set *ms, const char *, int);
139 private struct magic_set *load(const char *, int);
140 private void setparam(const char *);
141 private void applyparam(magic_t);
145 * main - parse arguments and handle options
148 main(int argc, char *argv[])
152 int action = 0, didsomefiles = 0, errflg = 0;
153 int flags = 0, e = 0;
154 struct magic_set *magic = NULL;
156 const char *magicfile = NULL; /* where the magic is */
158 /* makes islower etc work for other langs */
159 #ifdef HAVE_SETLOCALE
160 (void)setlocale(LC_CTYPE, "");
164 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
165 _wildcard(&argc, &argv);
168 if ((progname = strrchr(argv[0], '/')) != NULL)
174 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
176 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
185 flags |= MAGIC_APPLE;
188 flags |= MAGIC_MIME_TYPE;
191 flags |= MAGIC_MIME_ENCODING;
205 action = FILE_COMPILE;
208 flags |= MAGIC_DEBUG|MAGIC_CHECK;
211 flags |= MAGIC_ERROR;
214 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
215 if (strcmp(nv[i].name, optarg) == 0)
218 if (i == sizeof(nv) / sizeof(nv[0]))
221 flags |= nv[i].value;
228 if ((magic = load(magicfile, flags)) == NULL)
230 e |= unwrap(magic, optarg);
240 flags |= MAGIC_CONTINUE;
254 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
256 flags |= MAGIC_PRESERVE_ATIME;
267 flags |= MAGIC_DEVICES;
270 if (magicfile == NULL)
271 magicfile = magic_getpath(magicfile, action);
272 (void)fprintf(stdout, "%s-%s\n", progname, VERSION);
273 (void)fprintf(stdout, "magic file from %s\n",
277 flags |= MAGIC_COMPRESS;
281 flags |= MAGIC_SYMLINK;
284 flags &= ~MAGIC_SYMLINK;
299 if (MAGIC_VERSION != magic_version())
300 (void)fprintf(stderr, "%s: compiled magic version [%d] "
301 "does not match with shared library magic version [%d]\n",
302 progname, MAGIC_VERSION, magic_version());
309 * Don't try to check/compile ~/.magic unless we explicitly
312 magic = magic_open(flags|MAGIC_CHECK);
314 (void)fprintf(stderr, "%s: %s\n", progname,
322 c = magic_check(magic, magicfile);
325 c = magic_compile(magic, magicfile);
328 c = magic_list(magic, magicfile);
334 (void)fprintf(stderr, "%s: %s\n", progname,
341 if ((magic = load(magicfile, flags)) == NULL)
346 if (optind == argc) {
352 for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
353 nw = file_mbswidth(argv[j]);
358 * If bflag is only set twice, set it depending on
359 * number of files [this is undocumented, and subject to change]
362 bflag = optind >= argc - 1;
364 for (; optind < argc; optind++)
365 e |= process(magic, argv[optind], wid);
374 applyparam(magic_t magic)
378 for (i = 0; i < __arraycount(pm); i++) {
379 if (pm[i].value == 0)
381 if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
382 (void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
383 pm[i].name, strerror(errno));
390 setparam(const char *p)
395 if ((s = strchr(p, '=')) == NULL)
398 for (i = 0; i < __arraycount(pm); i++) {
399 if (strncmp(p, pm[i].name, s - p) != 0)
401 pm[i].value = atoi(s + 1);
405 (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
409 private struct magic_set *
411 load(const char *magicfile, int flags)
413 struct magic_set *magic = magic_open(flags);
415 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
418 if (magic_load(magic, magicfile) == -1) {
419 (void)fprintf(stderr, "%s: %s\n",
420 progname, magic_error(magic));
428 * unwrap -- read a file of filenames, do each one.
431 unwrap(struct magic_set *ms, const char *fn)
440 if (strcmp("-", fn) == 0) {
444 if ((f = fopen(fn, "r")) == NULL) {
445 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
446 progname, fn, strerror(errno));
450 while ((len = getline(&line, &llen, f)) > 0) {
451 if (line[len - 1] == '\n')
452 line[len - 1] = '\0';
453 cwid = file_mbswidth(line);
461 while ((len = getline(&line, &llen, f)) > 0) {
462 if (line[len - 1] == '\n')
463 line[len - 1] = '\0';
464 e |= process(ms, line, wid);
466 (void)fflush(stdout);
475 * Called for each input file on the command line (or in a list of files)
478 process(struct magic_set *ms, const char *inname, int wid)
481 int std_in = strcmp(inname, "-") == 0;
483 if (wid > 0 && !bflag) {
484 (void)printf("%s", std_in ? "/dev/stdin" : inname);
486 (void)putc('\0', stdout);
487 (void)printf("%s", separator);
489 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
492 type = magic_file(ms, std_in ? NULL : inname);
494 (void)printf("ERROR: %s\n", magic_error(ms));
497 (void)printf("%s\n", type);
503 file_mbswidth(const char *s)
505 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
506 size_t bytesconsumed, old_n, n, width = 0;
509 (void)memset(&state, 0, sizeof(mbstate_t));
510 old_n = n = strlen(s);
513 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
514 if (bytesconsumed == (size_t)(-1) ||
515 bytesconsumed == (size_t)(-2)) {
516 /* Something went wrong, return something reasonable */
521 * do what strlen() would do, so that caller
526 int w = wcwidth(nextchar);
531 s += bytesconsumed, n -= bytesconsumed;
542 (void)fprintf(stderr, USAGE, progname, progname, progname);
547 docprint(const char *opts)
553 p = strstr(opts, "%o");
555 fprintf(stdout, "%s", opts);
559 for (sp = p - 1; sp > opts && *sp == ' '; sp--)
562 fprintf(stdout, "%.*s", (int)(p - opts), opts);
565 for (i = 0; i < __arraycount(nv); i++) {
566 fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
567 if (i && i % 5 == 0) {
568 fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
573 fprintf(stdout, "%s", opts + (p - opts) + 2);
580 "Usage: file [OPTION...] [FILE...]\n"
581 "Determine type of FILEs.\n"
583 #define OPT(shortname, longname, opt, doc) \
584 fprintf(stdout, " -%c, --" longname, shortname), \
586 #define OPT_LONGONLY(longname, opt, doc) \
587 fprintf(stdout, " --" longname), \
589 #include "file_opts.h"
592 fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n");