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.136 2009/12/06 23:18:04 rrt 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 "-bchikLNnprsvz0"
78 #define FILE_FLAGS "-bcikNnprsvz0"
82 "Usage: %s [" FILE_FLAGS \
83 "] [--apple] [--mime-encoding] [--mime-type]\n" \
84 " [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...\n" \
85 " %s -C [-m magicfiles]\n" \
89 #define MAXPATHLEN 1024
92 private int /* Global command-line options */
93 bflag = 0, /* brief output format */
94 nopad = 0, /* Don't pad output */
95 nobuffer = 0, /* Do not buffer stdout */
96 nulsep = 0; /* Append '\0' to the separator */
98 private const char *separator = ":"; /* Default field separator */
99 private const struct option long_options[] = {
100 #define OPT(shortname, longname, opt, doc) \
101 {longname, opt, NULL, shortname},
102 #define OPT_LONGONLY(longname, opt, doc) \
103 {longname, opt, NULL, 0},
104 #include "file_opts.h"
109 #define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0"
111 private const struct {
115 { "apptype", MAGIC_NO_CHECK_APPTYPE },
116 { "ascii", MAGIC_NO_CHECK_ASCII },
117 { "cdf", MAGIC_NO_CHECK_CDF },
118 { "compress", MAGIC_NO_CHECK_COMPRESS },
119 { "elf", MAGIC_NO_CHECK_ELF },
120 { "encoding", MAGIC_NO_CHECK_ENCODING },
121 { "soft", MAGIC_NO_CHECK_SOFT },
122 { "tar", MAGIC_NO_CHECK_TAR },
123 { "tokens", MAGIC_NO_CHECK_TOKENS },
126 private char *progname; /* used throughout */
128 private void usage(void);
129 private void help(void);
130 int main(int, char *[]);
132 private int unwrap(struct magic_set *, const char *);
133 private int process(struct magic_set *ms, const char *, int);
134 private struct magic_set *load(const char *, int);
138 * main - parse arguments and handle options
141 main(int argc, char *argv[])
145 int action = 0, didsomefiles = 0, errflg = 0;
146 int flags = 0, e = 0;
147 struct magic_set *magic = NULL;
149 const char *magicfile = NULL; /* where the magic is */
151 /* makes islower etc work for other langs */
152 (void)setlocale(LC_CTYPE, "");
155 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
156 _wildcard(&argc, &argv);
159 if ((progname = strrchr(argv[0], '/')) != NULL)
165 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
167 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
176 flags |= MAGIC_APPLE;
179 flags |= MAGIC_MIME_TYPE;
182 flags |= MAGIC_MIME_ENCODING;
196 action = FILE_COMPILE;
199 flags |= MAGIC_DEBUG|MAGIC_CHECK;
202 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
203 if (strcmp(nv[i].name, optarg) == 0)
206 if (i == sizeof(nv) / sizeof(nv[0]))
209 flags |= nv[i].value;
216 if ((magic = load(magicfile, flags)) == NULL)
218 e |= unwrap(magic, optarg);
228 flags |= MAGIC_CONTINUE;
239 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
241 flags |= MAGIC_PRESERVE_ATIME;
248 flags |= MAGIC_DEVICES;
251 if (magicfile == NULL)
252 magicfile = magic_getpath(magicfile, action);
253 (void)fprintf(stderr, "%s-%d.%.2d\n", progname,
254 FILE_VERSION_MAJOR, patchlevel);
255 (void)fprintf(stderr, "magic file from %s\n",
259 flags |= MAGIC_COMPRESS;
263 flags |= MAGIC_SYMLINK;
266 flags &= ~MAGIC_SYMLINK;
285 * Don't try to check/compile ~/.magic unless we explicitly
288 magic = magic_open(flags|MAGIC_CHECK);
290 (void)fprintf(stderr, "%s: %s\n", progname,
294 c = action == FILE_CHECK ? magic_check(magic, magicfile) :
295 magic_compile(magic, magicfile);
297 (void)fprintf(stderr, "%s: %s\n", progname,
304 if ((magic = load(magicfile, flags)) == NULL)
309 if (optind == argc) {
315 for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
316 nw = file_mbswidth(argv[j]);
321 * If bflag is only set twice, set it depending on
322 * number of files [this is undocumented, and subject to change]
325 bflag = optind >= argc - 1;
327 for (; optind < argc; optind++)
328 e |= process(magic, argv[optind], wid);
337 private struct magic_set *
339 load(const char *magicfile, int flags)
341 struct magic_set *magic = magic_open(flags);
343 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
346 if (magic_load(magic, magicfile) == -1) {
347 (void)fprintf(stderr, "%s: %s\n",
348 progname, magic_error(magic));
356 * unwrap -- read a file of filenames, do each one.
359 unwrap(struct magic_set *ms, const char *fn)
361 char buf[MAXPATHLEN];
366 if (strcmp("-", fn) == 0) {
370 if ((f = fopen(fn, "r")) == NULL) {
371 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
372 progname, fn, strerror(errno));
376 while (fgets(buf, sizeof(buf), f) != NULL) {
377 buf[strcspn(buf, "\n")] = '\0';
378 cwid = file_mbswidth(buf);
386 while (fgets(buf, sizeof(buf), f) != NULL) {
387 buf[strcspn(buf, "\n")] = '\0';
388 e |= process(ms, buf, wid);
390 (void)fflush(stdout);
398 * Called for each input file on the command line (or in a list of files)
401 process(struct magic_set *ms, const char *inname, int wid)
404 int std_in = strcmp(inname, "-") == 0;
406 if (wid > 0 && !bflag) {
407 (void)printf("%s", std_in ? "/dev/stdin" : inname);
409 (void)putc('\0', stdout);
411 (void)printf("%s", separator);
413 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
416 type = magic_file(ms, std_in ? NULL : inname);
418 (void)printf("ERROR: %s\n", magic_error(ms));
421 (void)printf("%s\n", type);
427 file_mbswidth(const char *s)
429 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
430 size_t bytesconsumed, old_n, n, width = 0;
433 (void)memset(&state, 0, sizeof(mbstate_t));
434 old_n = n = strlen(s);
437 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
438 if (bytesconsumed == (size_t)(-1) ||
439 bytesconsumed == (size_t)(-2)) {
440 /* Something went wrong, return something reasonable */
445 * do what strlen() would do, so that caller
450 width += wcwidth(nextchar);
452 s += bytesconsumed, n -= bytesconsumed;
463 (void)fprintf(stderr, USAGE, progname, progname, progname);
471 "Usage: file [OPTION...] [FILE...]\n"
472 "Determine type of FILEs.\n"
474 #define OPT(shortname, longname, opt, doc) \
475 fprintf(stderr, " -%c, --" longname doc, shortname);
476 #define OPT_LONGONLY(longname, opt, doc) \
477 fprintf(stderr, " --" longname doc);
478 #include "file_opts.h"