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() */
64 #include <getopt.h> /* for long options (is this portable?)*/
66 #undef HAVE_GETOPT_LONG
69 #include <netinet/in.h> /* for byte swapping */
71 #include "patchlevel.h"
74 FILE_RCSID("@(#)$Id: file.c,v 1.102 2006/06/02 00:07:58 ian Exp $")
79 #define SYMLINKFLAG "Lh"
81 #define SYMLINKFLAG ""
84 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
87 #define MAXPATHLEN 512
90 private int /* Global command-line options */
91 bflag = 0, /* brief output format */
92 nopad = 0, /* Don't pad output */
93 nobuffer = 0; /* Do not buffer stdout */
95 private const char *magicfile = 0; /* where the magic is */
96 private const char *default_magicfile = MAGIC;
97 private const char *separator = ":"; /* Default field separator */
99 private char *progname; /* used throughout */
101 private struct magic_set *magic;
103 private void unwrap(char *);
104 private void usage(void);
105 #ifdef HAVE_GETOPT_LONG
106 private void help(void);
109 private int byteconv4(int, int, int);
110 private short byteconv2(int, int, int);
113 int main(int, char *[]);
114 private void process(const char *, int);
115 private void load(const char *, int);
119 * main - parse arguments and handle options
122 main(int argc, char *argv[])
125 int action = 0, didsomefiles = 0, errflg = 0;
127 char *home, *usermagic;
129 static const char hmagic[] = "/.magic";
130 #define OPTSTRING "bcCdf:F:hikLm:nNprsvz"
131 #ifdef HAVE_GETOPT_LONG
133 static const struct option long_options[] =
135 {"version", 0, 0, 'v'},
137 {"brief", 0, 0, 'b'},
138 {"checking-printout", 0, 0, 'c'},
139 {"debug", 0, 0, 'd'},
140 {"files-from", 1, 0, 'f'},
141 {"separator", 1, 0, 'F'},
143 {"keep-going", 0, 0, 'k'},
145 {"dereference", 0, 0, 'L'},
146 {"no-dereference", 0, 0, 'h'},
148 {"magic-file", 1, 0, 'm'},
149 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
150 {"preserve-date", 0, 0, 'p'},
152 {"uncompress", 0, 0, 'z'},
154 {"no-buffer", 0, 0, 'n'},
155 {"no-pad", 0, 0, 'N'},
156 {"special-files", 0, 0, 's'},
157 {"compile", 0, 0, 'C'},
163 /* makes islower etc work for other langs */
164 (void)setlocale(LC_CTYPE, "");
168 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
169 _wildcard(&argc, &argv);
172 if ((progname = strrchr(argv[0], '/')) != NULL)
177 magicfile = default_magicfile;
178 if ((usermagic = getenv("MAGIC")) != NULL)
179 magicfile = usermagic;
181 if ((home = getenv("HOME")) != NULL) {
182 if ((usermagic = malloc(strlen(home)
183 + sizeof(hmagic))) != NULL) {
184 (void)strcpy(usermagic, home);
185 (void)strcat(usermagic, hmagic);
186 if (stat(usermagic, &sb)<0)
189 magicfile = usermagic;
194 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
196 #ifndef HAVE_GETOPT_LONG
197 while ((c = getopt(argc, argv, OPTSTRING)) != -1)
199 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
203 #ifdef HAVE_GETOPT_LONG
216 action = FILE_COMPILE;
219 flags |= MAGIC_DEBUG|MAGIC_CHECK;
224 load(magicfile, flags);
235 flags |= MAGIC_CONTINUE;
246 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
248 flags |= MAGIC_PRESERVE_ATIME;
255 flags |= MAGIC_DEVICES;
258 (void)fprintf(stdout, "%s-%d.%.2d\n", progname,
259 FILE_VERSION_MAJOR, patchlevel);
260 (void)fprintf(stdout, "magic file from %s\n",
264 flags |= MAGIC_COMPRESS;
268 flags |= MAGIC_SYMLINK;
271 flags &= ~MAGIC_SYMLINK;
287 magic = magic_open(flags|MAGIC_CHECK);
289 (void)fprintf(stderr, "%s: %s\n", progname,
293 c = action == FILE_CHECK ? magic_check(magic, magicfile) :
294 magic_compile(magic, magicfile);
296 (void)fprintf(stderr, "%s: %s\n", progname,
302 load(magicfile, flags);
306 if (optind == argc) {
313 for (wid = 0, i = optind; i < argc; i++) {
314 nw = file_mbswidth(argv[i]);
318 for (; optind < argc; optind++)
319 process(argv[optind], wid);
329 load(const char *m, int flags)
333 magic = magic_open(flags);
335 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
338 if (magic_load(magic, magicfile) == -1) {
339 (void)fprintf(stderr, "%s: %s\n",
340 progname, magic_error(magic));
346 * unwrap -- read a file of filenames, do each one.
351 char buf[MAXPATHLEN];
356 if (strcmp("-", fn) == 0) {
360 if ((f = fopen(fn, "r")) == NULL) {
361 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
362 progname, fn, strerror(errno));
366 while (fgets(buf, MAXPATHLEN, f) != NULL) {
368 if (len > 0 && buf[len - 1] == '\n')
370 cwid = file_mbswidth(buf);
378 while (fgets(buf, MAXPATHLEN, f) != NULL) {
380 if (len > 0 && buf[len - 1] == '\n')
384 (void)fflush(stdout);
391 * Called for each input file on the command line (or in a list of files)
394 process(const char *inname, int wid)
397 int std_in = strcmp(inname, "-") == 0;
399 if (wid > 0 && !bflag)
400 (void)printf("%s%s%*s ", std_in ? "/dev/stdin" : inname,
401 separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
403 type = magic_file(magic, std_in ? NULL : inname);
405 (void)printf("ERROR: %s\n", magic_error(magic));
407 (void)printf("%s\n", type);
415 * from 4 byte quantity to convert
416 * same whether to perform byte swapping
417 * big_endian whether we are a big endian host
420 byteconv4(int from, int same, int big_endian)
424 else if (big_endian) { /* lsb -> msb conversion on msb */
431 retval.c[0] = tmpval.c[3];
432 retval.c[1] = tmpval.c[2];
433 retval.c[2] = tmpval.c[1];
434 retval.c[3] = tmpval.c[0];
439 return ntohl(from); /* msb -> lsb conversion on lsb */
444 * Same as byteconv4, but for shorts
447 byteconv2(int from, int same, int big_endian)
451 else if (big_endian) { /* lsb -> msb conversion on msb */
457 tmpval.s = (short) from;
458 retval.c[0] = tmpval.c[1];
459 retval.c[1] = tmpval.c[0];
464 return ntohs(from); /* msb -> lsb conversion on lsb */
469 file_mbswidth(const char *s)
471 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
472 size_t bytesconsumed, old_n, n, width = 0;
475 (void)memset(&state, 0, sizeof(mbstate_t));
476 old_n = n = strlen(s);
479 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
480 if (bytesconsumed == (size_t)(-1) ||
481 bytesconsumed == (size_t)(-2)) {
482 /* Something went wrong, return something reasonable */
487 * do what strlen() would do, so that caller
492 width += wcwidth(nextchar);
494 s += bytesconsumed, n -= bytesconsumed;
505 (void)fprintf(stderr, USAGE, progname, progname);
506 #ifdef HAVE_GETOPT_LONG
507 (void)fputs("Try `file --help' for more information.\n", stderr);
512 #ifdef HAVE_GETOPT_LONG
517 "Usage: file [OPTION]... [FILE]...\n"
518 "Determine file type of FILEs.\n"
520 " -m, --magic-file LIST use LIST as a colon-separated list of magic\n"
522 " -z, --uncompress try to look inside compressed files\n"
523 " -b, --brief do not prepend filenames to output lines\n"
524 " -c, --checking-printout print the parsed form of the magic file, use in\n"
525 " conjunction with -m to debug a new magic file\n"
526 " before installing it\n"
527 " -f, --files-from FILE read the filenames to be examined from FILE\n"
528 " -F, --separator string use string as separator instead of `:'\n"
529 " -i, --mime output mime type strings\n"
530 " -k, --keep-going don't stop at the first match\n"
531 " -L, --dereference causes symlinks to be followed\n"
532 " -n, --no-buffer do not buffer output\n"
533 " -N, --no-pad do not pad output\n"
534 " -p, --preserve-date preserve access times on files\n"
535 " -r, --raw don't translate unprintable chars to \\ooo\n"
536 " -s, --special-files treat special (block/char devices) files as\n"
538 " --help display this help and exit\n"
539 " --version output version information and exit\n"