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("@(#)$File: file.c,v 1.117 2007/12/27 16:35:58 christos Exp $")
79 #define SYMLINKFLAG "Lh"
81 #define SYMLINKFLAG ""
84 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-e test] [-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
[])
127 int action
= 0, didsomefiles
= 0, errflg
= 0;
129 char *home
, *usermagic
;
131 static const char hmagic
[] = "/.magic";
132 #define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0"
133 #ifdef HAVE_GETOPT_LONG
135 static const struct option long_options
[] =
137 #define OPT(shortname, longname, opt, doc) \
138 {longname, opt, NULL, shortname},
139 #define OPT_LONGONLY(longname, opt, doc) \
140 {longname, opt, NULL, 0},
141 #include "file_opts.h"
148 static const struct {
152 { "apptype", MAGIC_NO_CHECK_APPTYPE
},
153 { "ascii", MAGIC_NO_CHECK_ASCII
},
154 { "compress", MAGIC_NO_CHECK_COMPRESS
},
155 { "elf", MAGIC_NO_CHECK_ELF
},
156 { "soft", MAGIC_NO_CHECK_SOFT
},
157 { "tar", MAGIC_NO_CHECK_TAR
},
158 { "tokens", MAGIC_NO_CHECK_TOKENS
},
159 { "troff", MAGIC_NO_CHECK_TROFF
},
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
210 flags
|= MAGIC_MIME_TYPE
;
213 flags
|= MAGIC_MIME_ENCODING
;
228 action
= FILE_COMPILE
;
231 flags
|= MAGIC_DEBUG
|MAGIC_CHECK
;
234 for (i
= 0; i
< sizeof(nv
) / sizeof(nv
[0]); i
++)
235 if (strcmp(nv
[i
].name
, optarg
) == 0)
238 if (i
== sizeof(nv
) / sizeof(nv
[0]))
241 flags
|= nv
[i
].value
;
247 load(magicfile
, flags
);
258 flags
|= MAGIC_CONTINUE
;
269 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
271 flags
|= MAGIC_PRESERVE_ATIME
;
278 flags
|= MAGIC_DEVICES
;
281 (void)fprintf(stderr
, "%s-%d.%.2d\n", progname
,
282 FILE_VERSION_MAJOR
, patchlevel
);
283 (void)fprintf(stderr
, "magic file from %s\n",
287 flags
|= MAGIC_COMPRESS
;
291 flags
|= MAGIC_SYMLINK
;
294 flags
&= ~MAGIC_SYMLINK
;
310 magic
= magic_open(flags
|MAGIC_CHECK
);
312 (void)fprintf(stderr
, "%s: %s\n", progname
,
316 c
= action
== FILE_CHECK
? magic_check(magic
, magicfile
) :
317 magic_compile(magic
, magicfile
);
319 (void)fprintf(stderr
, "%s: %s\n", progname
,
325 load(magicfile
, flags
);
329 if (optind
== argc
) {
336 for (wid
= 0, j
= (size_t)optind
; j
< (size_t)argc
; j
++) {
337 nw
= file_mbswidth(argv
[j
]);
342 * If bflag is only set twice, set it depending on
343 * number of files [this is undocumented, and subject to change]
346 bflag
= optind
>= argc
- 1;
348 for (; optind
< argc
; optind
++)
349 process(argv
[optind
], wid
);
359 load(const char *m
, int flags
)
361 if (magic
|| m
== NULL
)
363 magic
= magic_open(flags
);
365 (void)fprintf(stderr
, "%s: %s\n", progname
, strerror(errno
));
368 if (magic_load(magic
, magicfile
) == -1) {
369 (void)fprintf(stderr
, "%s: %s\n",
370 progname
, magic_error(magic
));
376 * unwrap -- read a file of filenames, do each one.
381 char buf
[MAXPATHLEN
];
385 if (strcmp("-", fn
) == 0) {
389 if ((f
= fopen(fn
, "r")) == NULL
) {
390 (void)fprintf(stderr
, "%s: Cannot open `%s' (%s).\n",
391 progname
, fn
, strerror(errno
));
395 while (fgets(buf
, MAXPATHLEN
, f
) != NULL
) {
396 buf
[strcspn(buf
, "\n")] = '\0';
397 cwid
= file_mbswidth(buf
);
405 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
406 buf
[strcspn(buf
, "\n")] = '\0';
409 (void)fflush(stdout
);
416 * Called for each input file on the command line (or in a list of files)
419 process(const char *inname
, int wid
)
422 int std_in
= strcmp(inname
, "-") == 0;
424 if (wid
> 0 && !bflag
) {
425 (void)printf("%s", std_in
? "/dev/stdin" : inname
);
427 (void)putc('\0', stdout
);
429 (void)printf("%s", separator
);
431 (int) (nopad
? 0 : (wid
- file_mbswidth(inname
))), "");
434 type
= magic_file(magic
, std_in
? NULL
: inname
);
436 (void)printf("ERROR: %s\n", magic_error(magic
));
438 (void)printf("%s\n", type
);
446 * from 4 byte quantity to convert
447 * same whether to perform byte swapping
448 * big_endian whether we are a big endian host
451 byteconv4(int from
, int same
, int big_endian
)
455 else if (big_endian
) { /* lsb -> msb conversion on msb */
462 retval
.c
[0] = tmpval
.c
[3];
463 retval
.c
[1] = tmpval
.c
[2];
464 retval
.c
[2] = tmpval
.c
[1];
465 retval
.c
[3] = tmpval
.c
[0];
470 return ntohl(from
); /* msb -> lsb conversion on lsb */
475 * Same as byteconv4, but for shorts
478 byteconv2(int from
, int same
, int big_endian
)
482 else if (big_endian
) { /* lsb -> msb conversion on msb */
488 tmpval
.s
= (short) from
;
489 retval
.c
[0] = tmpval
.c
[1];
490 retval
.c
[1] = tmpval
.c
[0];
495 return ntohs(from
); /* msb -> lsb conversion on lsb */
500 file_mbswidth(const char *s
)
502 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
503 size_t bytesconsumed
, old_n
, n
, width
= 0;
506 (void)memset(&state
, 0, sizeof(mbstate_t));
507 old_n
= n
= strlen(s
);
510 bytesconsumed
= mbrtowc(&nextchar
, s
, n
, &state
);
511 if (bytesconsumed
== (size_t)(-1) ||
512 bytesconsumed
== (size_t)(-2)) {
513 /* Something went wrong, return something reasonable */
518 * do what strlen() would do, so that caller
523 width
+= wcwidth(nextchar
);
525 s
+= bytesconsumed
, n
-= bytesconsumed
;
536 (void)fprintf(stderr
, USAGE
, progname
, progname
);
537 #ifdef HAVE_GETOPT_LONG
538 (void)fputs("Try `file --help' for more information.\n", stderr
);
543 #ifdef HAVE_GETOPT_LONG
548 "Usage: file [OPTION...] [FILE...]\n"
549 "Determine type of FILEs.\n"
551 #define OPT(shortname, longname, opt, doc) \
552 fprintf(stderr, " -%c, --" longname doc, shortname);
553 #define OPT_LONGONLY(longname, opt, doc) \
554 fprintf(stderr, " --" longname doc);
555 #include "file_opts.h"