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.143 2011/03/20 20:36:52 christos Exp $")
44 # if (__COHERENT__ >= 0x420)
45 # include <sys/utime.h>
48 # include <sys/time.h>
55 #include <unistd.h> /* for read() */
64 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
66 #ifndef HAVE_GETOPT_LONG
67 int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
73 #include "patchlevel.h"
76 #define FILE_FLAGS "-bchikLlNnprsvz0"
78 #define FILE_FLAGS "-bciklNnprsvz0"
82 "Usage: %s [" FILE_FLAGS \
83 "] [--apple] [--mime-encoding] [--mime-type]\n" \
84 " [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \
86 " %s -C [-m magicfiles]\n" \
89 private int /* Global command-line options */
90 bflag = 0, /* brief output format */
91 nopad = 0, /* Don't pad output */
92 nobuffer = 0, /* Do not buffer stdout */
93 nulsep = 0; /* Append '\0' to the separator */
95 private const char *separator = ":"; /* Default field separator */
96 private const struct option long_options[] = {
97 #define OPT(shortname, longname, opt, doc) \
98 {longname, opt, NULL, shortname},
99 #define OPT_LONGONLY(longname, opt, doc) \
100 {longname, opt, NULL, 0},
101 #include "file_opts.h"
106 #define OPTSTRING "bcCde:f:F:hiklLm:nNprsvz0"
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 },
124 private char *progname; /* used throughout */
126 private void usage(void);
127 private void help(void);
128 int main(int, char *[]);
130 private int unwrap(struct magic_set *, const char *);
131 private int process(struct magic_set *ms, const char *, int);
132 private struct magic_set *load(const char *, int);
136 * main - parse arguments and handle options
139 main(int argc, char *argv[])
143 int action = 0, didsomefiles = 0, errflg = 0;
144 int flags = 0, e = 0;
145 struct magic_set *magic = NULL;
147 const char *magicfile = NULL; /* where the magic is */
149 /* makes islower etc work for other langs */
150 (void)setlocale(LC_CTYPE, "");
153 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
154 _wildcard(&argc, &argv);
157 if ((progname = strrchr(argv[0], '/')) != NULL)
163 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
165 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
174 flags |= MAGIC_APPLE;
177 flags |= MAGIC_MIME_TYPE;
180 flags |= MAGIC_MIME_ENCODING;
194 action = FILE_COMPILE;
197 flags |= MAGIC_DEBUG|MAGIC_CHECK;
200 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
201 if (strcmp(nv[i].name, optarg) == 0)
204 if (i == sizeof(nv) / sizeof(nv[0]))
207 flags |= nv[i].value;
214 if ((magic = load(magicfile, flags)) == NULL)
216 e |= unwrap(magic, optarg);
226 flags |= MAGIC_CONTINUE;
240 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
242 flags |= MAGIC_PRESERVE_ATIME;
249 flags |= MAGIC_DEVICES;
252 if (magicfile == NULL)
253 magicfile = magic_getpath(magicfile, action);
254 (void)fprintf(stdout, "%s-%d.%.2d\n", progname,
255 FILE_VERSION_MAJOR, patchlevel);
256 (void)fprintf(stdout, "magic file from %s\n",
260 flags |= MAGIC_COMPRESS;
264 flags |= MAGIC_SYMLINK;
267 flags &= ~MAGIC_SYMLINK;
287 * Don't try to check/compile ~/.magic unless we explicitly
290 magic = magic_open(flags|MAGIC_CHECK);
292 (void)fprintf(stderr, "%s: %s\n", progname,
298 c = magic_check(magic, magicfile);
301 c = magic_compile(magic, magicfile);
304 c = magic_list(magic, magicfile);
310 (void)fprintf(stderr, "%s: %s\n", progname,
317 if ((magic = load(magicfile, flags)) == NULL)
322 if (optind == argc) {
328 for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
329 nw = file_mbswidth(argv[j]);
334 * If bflag is only set twice, set it depending on
335 * number of files [this is undocumented, and subject to change]
338 bflag = optind >= argc - 1;
340 for (; optind < argc; optind++)
341 e |= process(magic, argv[optind], wid);
350 private struct magic_set *
352 load(const char *magicfile, int flags)
354 struct magic_set *magic = magic_open(flags);
356 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
359 if (magic_load(magic, magicfile) == -1) {
360 (void)fprintf(stderr, "%s: %s\n",
361 progname, magic_error(magic));
369 * unwrap -- read a file of filenames, do each one.
372 unwrap(struct magic_set *ms, const char *fn)
381 if (strcmp("-", fn) == 0) {
385 if ((f = fopen(fn, "r")) == NULL) {
386 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
387 progname, fn, strerror(errno));
391 while ((len = getline(&line, &llen, f)) > 0) {
392 if (line[len - 1] == '\n')
393 line[len - 1] = '\0';
394 cwid = file_mbswidth(line);
402 while ((len = getline(&line, &llen, f)) > 0) {
403 if (line[len - 1] == '\n')
404 line[len - 1] = '\0';
405 e |= process(ms, line, wid);
407 (void)fflush(stdout);
416 * Called for each input file on the command line (or in a list of files)
419 process(struct magic_set *ms, const char *inname, int wid)
422 int std_in = strcmp(inname, "-") == 0;
424 if (wid > 0 && !bflag) {
425 (void)printf("%s", std_in ? "/dev/stdin" : inname);
427 (void)putc('\0', stdout);
428 (void)printf("%s", separator);
430 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
433 type = magic_file(ms, std_in ? NULL : inname);
435 (void)printf("ERROR: %s\n", magic_error(ms));
438 (void)printf("%s\n", type);
444 file_mbswidth(const char *s)
446 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
447 size_t bytesconsumed, old_n, n, width = 0;
450 (void)memset(&state, 0, sizeof(mbstate_t));
451 old_n = n = strlen(s);
454 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
455 if (bytesconsumed == (size_t)(-1) ||
456 bytesconsumed == (size_t)(-2)) {
457 /* Something went wrong, return something reasonable */
462 * do what strlen() would do, so that caller
467 width += wcwidth(nextchar);
469 s += bytesconsumed, n -= bytesconsumed;
480 (void)fprintf(stderr, USAGE, progname, progname, progname);
488 "Usage: file [OPTION...] [FILE...]\n"
489 "Determine type of FILEs.\n"
491 #define OPT(shortname, longname, opt, doc) \
492 fprintf(stdout, " -%c, --" longname doc, shortname);
493 #define OPT_LONGONLY(longname, opt, doc) \
494 fprintf(stdout, " --" longname doc);
495 #include "file_opts.h"
498 fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n");