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.
39 #include <sys/types.h>
40 #include <sys/param.h> /* for MAXPATHLEN */
43 # if (__COHERENT__ >= 0x420)
44 # include <sys/utime.h>
47 # include <sys/time.h>
54 #include <unistd.h> /* for read() */
68 #ifndef HAVE_GETOPT_LONG
69 int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
72 #include <netinet/in.h> /* for byte swapping */
74 #include "patchlevel.h"
77 FILE_RCSID("@(#)$File: file.c,v 1.121 2008/07/03 15:48:18 christos Exp $")
82 #define SYMLINKFLAG "Lh"
84 #define SYMLINKFLAG ""
87 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
90 #define MAXPATHLEN 512
93 private int /* Global command-line options */
94 bflag = 0, /* brief output format */
95 nopad = 0, /* Don't pad output */
96 nobuffer = 0, /* Do not buffer stdout */
97 nulsep = 0; /* Append '\0' to the separator */
99 private const char *magicfile = 0; /* where the magic is */
100 private const char *default_magicfile = MAGIC;
101 private const char *separator = ":"; /* Default field separator */
103 private char *progname; /* used throughout */
105 private struct magic_set *magic;
107 private void unwrap(char *);
108 private void usage(void);
109 private void help(void);
111 int main(int, char *[]);
112 private void process(const char *, int);
113 private void load(const char *, int);
117 * main - parse arguments and handle options
120 main(int argc, char *argv[])
124 int action = 0, didsomefiles = 0, errflg = 0;
126 char *home, *usermagic;
128 static const char hmagic[] = "/.magic";
129 #define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0"
131 static const struct option long_options[] =
133 #define OPT(shortname, longname, opt, doc) \
134 {longname, opt, NULL, shortname},
135 #define OPT_LONGONLY(longname, opt, doc) \
136 {longname, opt, NULL, 0},
137 #include "file_opts.h"
143 static const struct {
147 { "apptype", MAGIC_NO_CHECK_APPTYPE },
148 { "ascii", MAGIC_NO_CHECK_ASCII },
149 { "compress", MAGIC_NO_CHECK_COMPRESS },
150 { "elf", MAGIC_NO_CHECK_ELF },
151 { "soft", MAGIC_NO_CHECK_SOFT },
152 { "tar", MAGIC_NO_CHECK_TAR },
153 { "tokens", MAGIC_NO_CHECK_TOKENS },
156 /* makes islower etc work for other langs */
157 (void)setlocale(LC_CTYPE, "");
160 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
161 _wildcard(&argc, &argv);
164 if ((progname = strrchr(argv[0], '/')) != NULL)
169 magicfile = default_magicfile;
170 if ((usermagic = getenv("MAGIC")) != NULL)
171 magicfile = usermagic;
173 if ((home = getenv("HOME")) != NULL) {
174 if ((usermagic = malloc(strlen(home)
175 + sizeof(hmagic))) != NULL) {
176 (void)strcpy(usermagic, home);
177 (void)strcat(usermagic, hmagic);
178 if (stat(usermagic, &sb)<0)
181 magicfile = usermagic;
186 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
188 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
197 flags |= MAGIC_MIME_TYPE;
200 flags |= MAGIC_MIME_ENCODING;
214 action = FILE_COMPILE;
217 flags |= MAGIC_DEBUG|MAGIC_CHECK;
220 for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
221 if (strcmp(nv[i].name, optarg) == 0)
224 if (i == sizeof(nv) / sizeof(nv[0]))
227 flags |= nv[i].value;
233 load(magicfile, flags);
244 flags |= MAGIC_CONTINUE;
255 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
257 flags |= MAGIC_PRESERVE_ATIME;
264 flags |= MAGIC_DEVICES;
267 (void)fprintf(stderr, "%s-%d.%.2d\n", progname,
268 FILE_VERSION_MAJOR, patchlevel);
269 (void)fprintf(stderr, "magic file from %s\n",
273 flags |= MAGIC_COMPRESS;
277 flags |= MAGIC_SYMLINK;
280 flags &= ~MAGIC_SYMLINK;
296 magic = magic_open(flags|MAGIC_CHECK);
298 (void)fprintf(stderr, "%s: %s\n", progname,
302 c = action == FILE_CHECK ? magic_check(magic, magicfile) :
303 magic_compile(magic, magicfile);
305 (void)fprintf(stderr, "%s: %s\n", progname,
311 load(magicfile, flags);
315 if (optind == argc) {
322 for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
323 nw = file_mbswidth(argv[j]);
328 * If bflag is only set twice, set it depending on
329 * number of files [this is undocumented, and subject to change]
332 bflag = optind >= argc - 1;
334 for (; optind < argc; optind++)
335 process(argv[optind], wid);
338 c = magic->haderr ? 1 : 0;
346 load(const char *m, int flags)
348 if (magic || m == NULL)
350 magic = magic_open(flags);
352 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
355 if (magic_load(magic, magicfile) == -1) {
356 (void)fprintf(stderr, "%s: %s\n",
357 progname, magic_error(magic));
363 * unwrap -- read a file of filenames, do each one.
368 char buf[MAXPATHLEN];
372 if (strcmp("-", fn) == 0) {
376 if ((f = fopen(fn, "r")) == NULL) {
377 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
378 progname, fn, strerror(errno));
382 while (fgets(buf, sizeof(buf), f) != NULL) {
383 buf[strcspn(buf, "\n")] = '\0';
384 cwid = file_mbswidth(buf);
392 while (fgets(buf, sizeof(buf), f) != NULL) {
393 buf[strcspn(buf, "\n")] = '\0';
396 (void)fflush(stdout);
403 * Called for each input file on the command line (or in a list of files)
406 process(const char *inname, int wid)
409 int std_in = strcmp(inname, "-") == 0;
411 if (wid > 0 && !bflag) {
412 (void)printf("%s", std_in ? "/dev/stdin" : inname);
414 (void)putc('\0', stdout);
416 (void)printf("%s", separator);
418 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
421 type = magic_file(magic, std_in ? NULL : inname);
423 (void)printf("ERROR: %s\n", magic_error(magic));
425 (void)printf("%s\n", type);
429 file_mbswidth(const char *s)
431 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
432 size_t bytesconsumed, old_n, n, width = 0;
435 (void)memset(&state, 0, sizeof(mbstate_t));
436 old_n = n = strlen(s);
439 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
440 if (bytesconsumed == (size_t)(-1) ||
441 bytesconsumed == (size_t)(-2)) {
442 /* Something went wrong, return something reasonable */
447 * do what strlen() would do, so that caller
452 width += wcwidth(nextchar);
454 s += bytesconsumed, n -= bytesconsumed;
465 (void)fprintf(stderr, USAGE, progname, progname);
466 (void)fputs("Try `file --help' for more information.\n", stderr);
474 "Usage: file [OPTION...] [FILE...]\n"
475 "Determine type of FILEs.\n"
477 #define OPT(shortname, longname, opt, doc) \
478 fprintf(stderr, " -%c, --" longname doc, shortname);
479 #define OPT_LONGONLY(longname, opt, doc) \
480 fprintf(stderr, " --" longname doc);
481 #include "file_opts.h"