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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Ian F. Darwin and others.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * file - find type of a file or files - main program.
44 #include <sys/types.h>
45 #include <sys/param.h> /* for MAXPATHLEN */
47 #include <fcntl.h> /* for open() */
49 # if (__COHERENT__ >= 0x420)
50 # include <sys/utime.h>
53 # include <sys/time.h>
60 #include <unistd.h> /* for read() */
70 #include <getopt.h> /* for long options (is this portable?)*/
72 #undef HAVE_GETOPT_LONG
75 #include <netinet/in.h> /* for byte swapping */
77 #include "patchlevel.h"
80 FILE_RCSID("@(#)$Id: file.c,v 1.93 2004/04/07 14:23:55 christos Exp $")
85 #define SYMLINKFLAG "L"
87 #define SYMLINKFLAG ""
90 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNsvz] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
93 #define MAXPATHLEN 512
96 private int /* Global command-line options */
97 bflag = 0, /* brief output format */
98 nopad = 0, /* Don't pad output */
99 nobuffer = 0; /* Do not buffer stdout */
101 private const char *magicfile = 0; /* where the magic is */
102 private const char *default_magicfile = MAGIC;
103 private char *separator = ":"; /* Default field separator */
105 private char *progname; /* used throughout */
107 private struct magic_set *magic;
109 private void unwrap(char *);
110 private void usage(void);
111 #ifdef HAVE_GETOPT_LONG
112 private void help(void);
115 private int byteconv4(int, int, int);
116 private short byteconv2(int, int, int);
119 int main(int, char *[]);
120 private void process(const char *, int);
121 private void load(const char *, int);
125 * main - parse arguments and handle options
128 main(int argc, char *argv[])
131 int action = 0, didsomefiles = 0, errflg = 0;
133 char *home, *usermagic;
135 #define OPTSTRING "bcCdf:F:ikLm:nNprsvz"
136 #ifdef HAVE_GETOPT_LONG
138 private struct option long_options[] =
140 {"version", 0, 0, 'v'},
142 {"brief", 0, 0, 'b'},
143 {"checking-printout", 0, 0, 'c'},
144 {"debug", 0, 0, 'd'},
145 {"files-from", 1, 0, 'f'},
146 {"separator", 1, 0, 'F'},
148 {"keep-going", 0, 0, 'k'},
150 {"dereference", 0, 0, 'L'},
152 {"magic-file", 1, 0, 'm'},
153 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
154 {"preserve-date", 0, 0, 'p'},
156 {"uncompress", 0, 0, 'z'},
158 {"no-buffer", 0, 0, 'n'},
159 {"no-pad", 0, 0, 'N'},
160 {"special-files", 0, 0, 's'},
161 {"compile", 0, 0, 'C'},
167 setlocale(LC_CTYPE, ""); /* makes islower etc work for other langs */
171 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
172 _wildcard(&argc, &argv);
175 if ((progname = strrchr(argv[0], '/')) != NULL)
180 magicfile = default_magicfile;
181 if ((usermagic = getenv("MAGIC")) != NULL)
182 magicfile = usermagic;
184 if ((home = getenv("HOME")) != NULL) {
185 if ((usermagic = malloc(strlen(home) + 8)) != NULL) {
186 (void)strcpy(usermagic, home);
187 (void)strcat(usermagic, "/.magic");
188 if (stat(usermagic, &sb)<0)
191 magicfile = usermagic;
195 #ifndef HAVE_GETOPT_LONG
196 while ((c = getopt(argc, argv, OPTSTRING)) != -1)
198 while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
202 #ifdef HAVE_GETOPT_LONG
215 action = FILE_COMPILE;
218 flags |= MAGIC_DEBUG|MAGIC_CHECK;
223 load(magicfile, flags);
234 flags |= MAGIC_CONTINUE;
245 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
247 flags |= MAGIC_PRESERVE_ATIME;
254 flags |= MAGIC_DEVICES;
257 (void) fprintf(stdout, "%s-%d.%.2d\n", progname,
258 FILE_VERSION_MAJOR, patchlevel);
259 (void) fprintf(stdout, "magic file from %s\n",
263 flags |= MAGIC_COMPRESS;
267 flags |= MAGIC_SYMLINK;
283 magic = magic_open(flags|MAGIC_CHECK);
285 (void)fprintf(stderr, "%s: %s\n", progname,
289 c = action == FILE_CHECK ? magic_check(magic, magicfile) :
290 magic_compile(magic, magicfile);
292 (void)fprintf(stderr, "%s: %s\n", progname,
298 load(magicfile, flags);
302 if (optind == argc) {
309 for (wid = 0, i = optind; i < argc; i++) {
310 nw = file_mbswidth(argv[i]);
314 for (; optind < argc; optind++)
315 process(argv[optind], wid);
323 load(const char *m, int flags)
327 magic = magic_open(flags);
329 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
332 if (magic_load(magic, magicfile) == -1) {
333 (void)fprintf(stderr, "%s: %s\n",
334 progname, magic_error(magic));
340 * unwrap -- read a file of filenames, do each one.
345 char buf[MAXPATHLEN];
349 if (strcmp("-", fn) == 0) {
353 if ((f = fopen(fn, "r")) == NULL) {
354 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
355 progname, fn, strerror(errno));
359 while (fgets(buf, MAXPATHLEN, f) != NULL) {
360 cwid = file_mbswidth(buf) - 1;
368 while (fgets(buf, MAXPATHLEN, f) != NULL) {
369 buf[file_mbswidth(buf)-1] = '\0';
372 (void) fflush(stdout);
379 process(const char *inname, int wid)
382 int std_in = strcmp(inname, "-") == 0;
384 if (wid > 0 && !bflag)
385 (void) printf("%s%s%*s ", std_in ? "/dev/stdin" : inname,
386 separator, (int) (nopad ? 0 : (wid - file_mbswidth(inname))), "");
388 type = magic_file(magic, std_in ? NULL : inname);
390 printf("ERROR: %s\n", magic_error(magic));
392 printf("%s\n", type);
400 * from 4 byte quantity to convert
401 * same whether to perform byte swapping
402 * big_endian whether we are a big endian host
405 byteconv4(int from, int same, int big_endian)
409 else if (big_endian) { /* lsb -> msb conversion on msb */
416 retval.c[0] = tmpval.c[3];
417 retval.c[1] = tmpval.c[2];
418 retval.c[2] = tmpval.c[1];
419 retval.c[3] = tmpval.c[0];
424 return ntohl(from); /* msb -> lsb conversion on lsb */
429 * Same as byteconv4, but for shorts
432 byteconv2(int from, int same, int big_endian)
436 else if (big_endian) { /* lsb -> msb conversion on msb */
442 tmpval.s = (short) from;
443 retval.c[0] = tmpval.c[1];
444 retval.c[1] = tmpval.c[0];
449 return ntohs(from); /* msb -> lsb conversion on lsb */
454 file_mbswidth(const char *s)
456 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
457 size_t bytesconsumed, old_n, n, width = 0;
460 (void)memset(&state, 0, sizeof(mbstate_t));
461 old_n = n = strlen(s);
464 bytesconsumed = mbrtowc(&nextchar, s, n, &state);
465 if (bytesconsumed == (size_t)(-1) ||
466 bytesconsumed == (size_t)(-2)) {
467 /* Something went wrong, return something reasonable */
472 * do what strlen() would do, so that caller
477 width += wcwidth(nextchar);
479 s += bytesconsumed, n -= bytesconsumed;
490 (void)fprintf(stderr, USAGE, progname, progname);
491 #ifdef HAVE_GETOPT_LONG
492 (void)fputs("Try `file --help' for more information.\n", stderr);
497 #ifdef HAVE_GETOPT_LONG
502 "Usage: file [OPTION]... [FILE]...\n"
503 "Determine file type of FILEs.\n"
505 " -m, --magic-file LIST use LIST as a colon-separated list of magic\n"
507 " -z, --uncompress try to look inside compressed files\n"
508 " -b, --brief do not prepend filenames to output lines\n"
509 " -c, --checking-printout print the parsed form of the magic file, use in\n"
510 " conjunction with -m to debug a new magic file\n"
511 " before installing it\n"
512 " -f, --files-from FILE read the filenames to be examined from FILE\n"
513 " -F, --separator string use string as separator instead of `:'\n"
514 " -i, --mime output mime type strings\n"
515 " -k, --keep-going don't stop at the first match\n"
516 " -L, --dereference causes symlinks to be followed\n"
517 " -n, --no-buffer do not buffer output\n"
518 " -N, --no-pad do not pad output\n"
519 " -p, --preserve-date preserve access times on files\n"
520 " -r, --raw don't translate unprintable chars to \\ooo\n"
521 " -s, --special-files treat special (block/char devices) files as\n"
523 " --help display this help and exit\n"
524 " --version output version information and exit\n"