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.100 2005/10/17 18:41:44 christos Exp $")
79 #define SYMLINKFLAG "Lh"
81 #define SYMLINKFLAG ""
84 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNsvz] [-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 #define OPTSTRING "bcCdf:F:hikLm:nNprsvz"
130 #ifdef HAVE_GETOPT_LONG
132 private struct option long_options[] =
134 {"version", 0, 0, 'v'},
136 {"brief", 0, 0, 'b'},
137 {"checking-printout", 0, 0, 'c'},
138 {"debug", 0, 0, 'd'},
139 {"files-from", 1, 0, 'f'},
140 {"separator", 1, 0, 'F'},
142 {"keep-going", 0, 0, 'k'},
144 {"dereference", 0, 0, 'L'},
145 {"no-dereference", 0, 0, 'h'},
147 {"magic-file", 1, 0, 'm'},
148 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
149 {"preserve-date", 0, 0, 'p'},
151 {"uncompress", 0, 0, 'z'},
153 {"no-buffer", 0, 0, 'n'},
154 {"no-pad", 0, 0, 'N'},
155 {"special-files", 0, 0, 's'},
156 {"compile", 0, 0, 'C'},
162 /* makes islower etc work for other langs */
163 (void)setlocale(LC_CTYPE, "");
167 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
168 _wildcard(&argc, &argv);
171 if ((progname = strrchr(argv[0], '/')) != NULL)
176 magicfile = default_magicfile;
177 if ((usermagic = getenv("MAGIC")) != NULL)
178 magicfile = usermagic;
180 if ((home = getenv("HOME")) != NULL) {
181 if ((usermagic = malloc(strlen(home) + 8)) != NULL) {
182 (void)strcpy(usermagic, home);
183 (void)strcat(usermagic, "/.magic");
184 if (stat(usermagic, &sb)<0)
187 magicfile = usermagic;
192 flags |= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK : 0;
194 #ifndef HAVE_GETOPT_LONG
195 while ((c = getopt(argc, argv, OPTSTRING)) != -1)
197 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
201 #ifdef HAVE_GETOPT_LONG
214 action = FILE_COMPILE;
217 flags |= MAGIC_DEBUG|MAGIC_CHECK;
222 load(magicfile, flags);
233 flags |= MAGIC_CONTINUE;
244 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
246 flags |= MAGIC_PRESERVE_ATIME;
253 flags |= MAGIC_DEVICES;
256 (void)fprintf(stdout, "%s-%d.%.2d\n", progname,
257 FILE_VERSION_MAJOR, patchlevel);
258 (void)fprintf(stdout, "magic file from %s\n",
262 flags |= MAGIC_COMPRESS;
266 flags |= MAGIC_SYMLINK;
269 flags &= ~MAGIC_SYMLINK;
285 magic = magic_open(flags|MAGIC_CHECK);
287 (void)fprintf(stderr, "%s: %s\n", progname,
291 c = action == FILE_CHECK ? magic_check(magic, magicfile) :
292 magic_compile(magic, magicfile);
294 (void)fprintf(stderr, "%s: %s\n", progname,
300 load(magicfile, flags);
304 if (optind == argc) {
311 for (wid = 0, i = optind; i < argc; i++) {
312 nw = file_mbswidth(argv[i]);
316 for (; optind < argc; optind++)
317 process(argv[optind], wid);
327 load(const char *m, int flags)
331 magic = magic_open(flags);
333 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
336 if (magic_load(magic, magicfile) == -1) {
337 (void)fprintf(stderr, "%s: %s\n",
338 progname, magic_error(magic));
344 * unwrap -- read a file of filenames, do each one.
349 char buf[MAXPATHLEN];
354 if (strcmp("-", fn) == 0) {
358 if ((f = fopen(fn, "r")) == NULL) {
359 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
360 progname, fn, strerror(errno));
364 while (fgets(buf, MAXPATHLEN, f) != NULL) {
366 if (len > 0 && buf[len - 1] == '\n')
368 cwid = file_mbswidth(buf);
376 while (fgets(buf, MAXPATHLEN, f) != NULL) {
378 if (len > 0 && buf[len - 1] == '\n')
382 (void)fflush(stdout);
389 process(const char *inname, int wid)
392 int std_in = strcmp(inname, "-") == 0;
394 if (wid > 0 && !bflag)
395 (void)printf("%s%s%*s ", std_in ? "/dev/stdin" : inname,
396 separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
398 type = magic_file(magic, std_in ? NULL : inname);
400 (void)printf("ERROR: %s\n", magic_error(magic));
402 (void)printf("%s\n", type);
410 * from 4 byte quantity to convert
411 * same whether to perform byte swapping
412 * big_endian whether we are a big endian host
415 byteconv4(int from, int same, int big_endian)
419 else if (big_endian) { /* lsb -> msb conversion on msb */
426 retval.c[0] = tmpval.c[3];
427 retval.c[1] = tmpval.c[2];
428 retval.c[2] = tmpval.c[1];
429 retval.c[3] = tmpval.c[0];
434 return ntohl(from); /* msb -> lsb conversion on lsb */
439 * Same as byteconv4, but for shorts
442 byteconv2(int from, int same, int big_endian)
446 else if (big_endian) { /* lsb -> msb conversion on msb */
452 tmpval.s = (short) from;
453 retval.c[0] = tmpval.c[1];
454 retval.c[1] = tmpval.c[0];
459 return ntohs(from); /* msb -> lsb conversion on lsb */
464 file_mbswidth(const char *s)
466 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
467 size_t bytesconsumed, old_n, n, width = 0;
470 (void)memset(&state, 0, sizeof(mbstate_t));
471 old_n = n = strlen(s);
474 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
475 if (bytesconsumed == (size_t)(-1) ||
476 bytesconsumed == (size_t)(-2)) {
477 /* Something went wrong, return something reasonable */
482 * do what strlen() would do, so that caller
487 width += wcwidth(nextchar);
489 s += bytesconsumed, n -= bytesconsumed;
500 (void)fprintf(stderr, USAGE, progname, progname);
501 #ifdef HAVE_GETOPT_LONG
502 (void)fputs("Try `file --help' for more information.\n", stderr);
507 #ifdef HAVE_GETOPT_LONG
512 "Usage: file [OPTION]... [FILE]...\n"
513 "Determine file type of FILEs.\n"
515 " -m, --magic-file LIST use LIST as a colon-separated list of magic\n"
517 " -z, --uncompress try to look inside compressed files\n"
518 " -b, --brief do not prepend filenames to output lines\n"
519 " -c, --checking-printout print the parsed form of the magic file, use in\n"
520 " conjunction with -m to debug a new magic file\n"
521 " before installing it\n"
522 " -f, --files-from FILE read the filenames to be examined from FILE\n"
523 " -F, --separator string use string as separator instead of `:'\n"
524 " -i, --mime output mime type strings\n"
525 " -k, --keep-going don't stop at the first match\n"
526 " -L, --dereference causes symlinks to be followed\n"
527 " -n, --no-buffer do not buffer output\n"
528 " -N, --no-pad do not pad output\n"
529 " -p, --preserve-date preserve access times on files\n"
530 " -r, --raw don't translate unprintable chars to \\ooo\n"
531 " -s, --special-files treat special (block/char devices) files as\n"
533 " --help display this help and exit\n"
534 " --version output version information and exit\n"