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.104 2006/11/25 17:28:54 christos Exp $")
79 #define SYMLINKFLAG "Lh"
81 #define SYMLINKFLAG ""
84 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-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 */
94 nulsep = 0; /* Append '\0' to the separator */
96 private const char *magicfile = 0; /* where the magic is */
97 private const char *default_magicfile = MAGIC;
98 private const char *separator = ":"; /* Default field separator */
100 private char *progname; /* used throughout */
102 private struct magic_set *magic;
104 private void unwrap(char *);
105 private void usage(void);
106 #ifdef HAVE_GETOPT_LONG
107 private void help(void);
110 private int byteconv4(int, int, int);
111 private short byteconv2(int, int, int);
114 int main(int, char *[]);
115 private void process(const char *, int);
116 private void load(const char *, int);
120 * main - parse arguments and handle options
123 main(int argc, char *argv[])
126 int action = 0, didsomefiles = 0, errflg = 0;
128 char *home, *usermagic;
130 static const char hmagic[] = "/.magic";
131 #define OPTSTRING "bcCdf:F:hikLm:nNprsvz0"
132 #ifdef HAVE_GETOPT_LONG
134 static const struct option long_options[] =
136 {"version", 0, 0, 'v'},
138 {"brief", 0, 0, 'b'},
139 {"checking-printout", 0, 0, 'c'},
140 {"debug", 0, 0, 'd'},
141 {"files-from", 1, 0, 'f'},
142 {"separator", 1, 0, 'F'},
144 {"keep-going", 0, 0, 'k'},
146 {"dereference", 0, 0, 'L'},
147 {"no-dereference", 0, 0, 'h'},
149 {"magic-file", 1, 0, 'm'},
150 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
151 {"preserve-date", 0, 0, 'p'},
153 {"uncompress", 0, 0, 'z'},
155 {"no-buffer", 0, 0, 'n'},
156 {"no-pad", 0, 0, 'N'},
157 {"special-files", 0, 0, 's'},
158 {"compile", 0, 0, 'C'},
159 {"print0", 0, 0, '0'},
165 /* makes islower etc work for other langs */
166 (void)setlocale(LC_CTYPE, "");
170 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
171 _wildcard(&argc, &argv);
174 if ((progname = strrchr(argv[0], '/')) != NULL)
179 magicfile = default_magicfile;
180 if ((usermagic = getenv("MAGIC")) != NULL)
181 magicfile = usermagic;
183 if ((home = getenv("HOME")) != NULL) {
184 if ((usermagic = malloc(strlen(home)
185 + sizeof(hmagic))) != NULL) {
186 (void)strcpy(usermagic, home);
187 (void)strcat(usermagic, hmagic);
188 if (stat(usermagic, &sb)<0)
191 magicfile = usermagic;
196 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
198 #ifndef HAVE_GETOPT_LONG
199 while ((c = getopt(argc, argv, OPTSTRING)) != -1)
201 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
205 #ifdef HAVE_GETOPT_LONG
221 action = FILE_COMPILE;
224 flags |= MAGIC_DEBUG|MAGIC_CHECK;
229 load(magicfile, flags);
240 flags |= MAGIC_CONTINUE;
251 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
253 flags |= MAGIC_PRESERVE_ATIME;
260 flags |= MAGIC_DEVICES;
263 (void)fprintf(stdout, "%s-%d.%.2d\n", progname,
264 FILE_VERSION_MAJOR, patchlevel);
265 (void)fprintf(stdout, "magic file from %s\n",
269 flags |= MAGIC_COMPRESS;
273 flags |= MAGIC_SYMLINK;
276 flags &= ~MAGIC_SYMLINK;
292 magic = magic_open(flags|MAGIC_CHECK);
294 (void)fprintf(stderr, "%s: %s\n", progname,
298 c = action == FILE_CHECK ? magic_check(magic, magicfile) :
299 magic_compile(magic, magicfile);
301 (void)fprintf(stderr, "%s: %s\n", progname,
307 load(magicfile, flags);
311 if (optind == argc) {
318 for (wid = 0, i = optind; i < argc; i++) {
319 nw = file_mbswidth(argv[i]);
323 for (; optind < argc; optind++)
324 process(argv[optind], wid);
334 load(const char *m, int flags)
338 magic = magic_open(flags);
340 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
343 if (magic_load(magic, magicfile) == -1) {
344 (void)fprintf(stderr, "%s: %s\n",
345 progname, magic_error(magic));
351 * unwrap -- read a file of filenames, do each one.
356 char buf[MAXPATHLEN];
361 if (strcmp("-", fn) == 0) {
365 if ((f = fopen(fn, "r")) == NULL) {
366 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
367 progname, fn, strerror(errno));
371 while (fgets(buf, MAXPATHLEN, f) != NULL) {
373 if (len > 0 && buf[len - 1] == '\n')
375 cwid = file_mbswidth(buf);
383 while (fgets(buf, MAXPATHLEN, f) != NULL) {
385 if (len > 0 && buf[len - 1] == '\n')
389 (void)fflush(stdout);
396 * Called for each input file on the command line (or in a list of files)
399 process(const char *inname, int wid)
402 int std_in = strcmp(inname, "-") == 0;
404 if (wid > 0 && !bflag) {
405 (void)printf("%s", std_in ? "/dev/stdin" : inname);
409 (void)printf("%s", separator);
411 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
414 type = magic_file(magic, std_in ? NULL : inname);
416 (void)printf("ERROR: %s\n", magic_error(magic));
418 (void)printf("%s\n", type);
426 * from 4 byte quantity to convert
427 * same whether to perform byte swapping
428 * big_endian whether we are a big endian host
431 byteconv4(int from, int same, int big_endian)
435 else if (big_endian) { /* lsb -> msb conversion on msb */
442 retval.c[0] = tmpval.c[3];
443 retval.c[1] = tmpval.c[2];
444 retval.c[2] = tmpval.c[1];
445 retval.c[3] = tmpval.c[0];
450 return ntohl(from); /* msb -> lsb conversion on lsb */
455 * Same as byteconv4, but for shorts
458 byteconv2(int from, int same, int big_endian)
462 else if (big_endian) { /* lsb -> msb conversion on msb */
468 tmpval.s = (short) from;
469 retval.c[0] = tmpval.c[1];
470 retval.c[1] = tmpval.c[0];
475 return ntohs(from); /* msb -> lsb conversion on lsb */
480 file_mbswidth(const char *s)
482 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
483 size_t bytesconsumed, old_n, n, width = 0;
486 (void)memset(&state, 0, sizeof(mbstate_t));
487 old_n = n = strlen(s);
490 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
491 if (bytesconsumed == (size_t)(-1) ||
492 bytesconsumed == (size_t)(-2)) {
493 /* Something went wrong, return something reasonable */
498 * do what strlen() would do, so that caller
503 width += wcwidth(nextchar);
505 s += bytesconsumed, n -= bytesconsumed;
516 (void)fprintf(stderr, USAGE, progname, progname);
517 #ifdef HAVE_GETOPT_LONG
518 (void)fputs("Try `file --help' for more information.\n", stderr);
523 #ifdef HAVE_GETOPT_LONG
528 "Usage: file [OPTION]... [FILE]...\n"
529 "Determine file type of FILEs.\n"
531 " -m, --magic-file LIST use LIST as a colon-separated list of magic\n"
533 " -z, --uncompress try to look inside compressed files\n"
534 " -b, --brief do not prepend filenames to output lines\n"
535 " -c, --checking-printout print the parsed form of the magic file, use in\n"
536 " conjunction with -m to debug a new magic file\n"
537 " before installing it\n"
538 " -f, --files-from FILE read the filenames to be examined from FILE\n"
539 " -F, --separator string use string as separator instead of `:'\n"
540 " -i, --mime output mime type strings\n"
541 " -k, --keep-going don't stop at the first match\n"
542 " -L, --dereference causes symlinks to be followed\n"
543 " -n, --no-buffer do not buffer output\n"
544 " -N, --no-pad do not pad output\n"
545 " -p, --preserve-date preserve access times on files\n"
546 " -r, --raw don't translate unprintable chars to \\ooo\n"
547 " -s, --special-files treat special (block/char devices) files as\n"
549 " --help display this help and exit\n"
550 " --version output version information and exit\n"