4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * file - find type of a file or files - main program.
38 FILE_RCSID("@(#)$File: file.c,v 1.131 2009/02/13 18:48:05 christos Exp $")
50 # if (__COHERENT__ >= 0x420)
51 # include <sys/utime.h>
54 # include <sys/time.h>
61 #include <unistd.h> /* for read() */
70 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
75 #ifndef HAVE_GETOPT_LONG
76 int getopt_long(int argc
, char * const *argv
, const char *optstring
, const struct option
*longopts
, int *longindex
);
79 #include <netinet/in.h> /* for byte swapping */
81 #include "patchlevel.h"
84 #define SYMLINKFLAG "Lh"
86 #define SYMLINKFLAG ""
89 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
92 #define MAXPATHLEN 1024
95 private int /* Global command-line options */
96 bflag
= 0, /* brief output format */
97 nopad
= 0, /* Don't pad output */
98 nobuffer
= 0, /* Do not buffer stdout */
99 nulsep
= 0; /* Append '\0' to the separator */
101 private const char *default_magicfile
= MAGIC
;
102 private const char *separator
= ":"; /* Default field separator */
103 private const char hmagic
[] = "/.magic";
104 private const struct option long_options
[] = {
105 #define OPT(shortname, longname, opt, doc) \
106 {longname, opt, NULL, shortname},
107 #define OPT_LONGONLY(longname, opt, doc) \
108 {longname, opt, NULL, 0},
109 #include "file_opts.h"
114 #define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0"
116 private const struct {
120 { "apptype", MAGIC_NO_CHECK_APPTYPE
},
121 { "ascii", MAGIC_NO_CHECK_ASCII
},
122 { "cdf", MAGIC_NO_CHECK_CDF
},
123 { "compress", MAGIC_NO_CHECK_COMPRESS
},
124 { "elf", MAGIC_NO_CHECK_ELF
},
125 { "encoding", MAGIC_NO_CHECK_ENCODING
},
126 { "soft", MAGIC_NO_CHECK_SOFT
},
127 { "tar", MAGIC_NO_CHECK_TAR
},
128 { "tokens", MAGIC_NO_CHECK_TOKENS
},
131 private char *progname
; /* used throughout */
133 private void usage(void);
134 private void help(void);
135 int main(int, char *[]);
137 private int unwrap(struct magic_set
*, const char *);
138 private int process(struct magic_set
*ms
, const char *, int);
139 private struct magic_set
*load(const char *, int);
143 * main - parse arguments and handle options
146 main(int argc
, char *argv
[])
150 int action
= 0, didsomefiles
= 0, errflg
= 0;
151 int flags
= 0, e
= 0;
152 char *home
, *usermagic
;
153 struct magic_set
*magic
= NULL
;
154 char magicpath
[2 * MAXPATHLEN
+ 2];
156 const char *magicfile
; /* where the magic is */
158 /* makes islower etc work for other langs */
159 (void)setlocale(LC_CTYPE
, "");
162 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
163 _wildcard(&argc
, &argv
);
166 if ((progname
= strrchr(argv
[0], '/')) != NULL
)
171 magicfile
= default_magicfile
;
172 if ((usermagic
= getenv("MAGIC")) != NULL
)
173 magicfile
= usermagic
;
175 if ((home
= getenv("HOME")) != NULL
) {
176 (void)snprintf(magicpath
, sizeof(magicpath
), "%s%s",
178 if (access(magicpath
, R_OK
) == 0) {
179 (void)snprintf(magicpath
, sizeof(magicpath
),
180 "%s%s:%s", home
, hmagic
, magicfile
);
181 magicfile
= magicpath
;
186 flags
|= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK
: 0;
188 while ((c
= getopt_long(argc
, argv
, OPTSTRING
, long_options
,
197 flags
|= MAGIC_APPLE
;
200 flags
|= MAGIC_MIME_TYPE
;
203 flags
|= MAGIC_MIME_ENCODING
;
217 action
= FILE_COMPILE
;
220 flags
|= MAGIC_DEBUG
|MAGIC_CHECK
;
223 for (i
= 0; i
< sizeof(nv
) / sizeof(nv
[0]); i
++)
224 if (strcmp(nv
[i
].name
, optarg
) == 0)
227 if (i
== sizeof(nv
) / sizeof(nv
[0]))
230 flags
|= nv
[i
].value
;
237 if ((magic
= load(magicfile
, flags
)) == NULL
)
239 e
|= unwrap(magic
, optarg
);
249 flags
|= MAGIC_CONTINUE
;
260 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
262 flags
|= MAGIC_PRESERVE_ATIME
;
269 flags
|= MAGIC_DEVICES
;
272 (void)fprintf(stderr
, "%s-%d.%.2d\n", progname
,
273 FILE_VERSION_MAJOR
, patchlevel
);
274 (void)fprintf(stderr
, "magic file from %s\n",
278 flags
|= MAGIC_COMPRESS
;
282 flags
|= MAGIC_SYMLINK
;
285 flags
&= ~MAGIC_SYMLINK
;
304 * Don't try to check/compile ~/.magic unless we explicitly
307 if (magicfile
== magicpath
)
308 magicfile
= default_magicfile
;
309 magic
= magic_open(flags
|MAGIC_CHECK
);
311 (void)fprintf(stderr
, "%s: %s\n", progname
,
315 c
= action
== FILE_CHECK
? magic_check(magic
, magicfile
) :
316 magic_compile(magic
, magicfile
);
318 (void)fprintf(stderr
, "%s: %s\n", progname
,
325 if ((magic
= load(magicfile
, flags
)) == NULL
)
330 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 e
|= process(magic
, argv
[optind
], wid
);
358 private struct magic_set
*
360 load(const char *magicfile
, int flags
)
362 struct magic_set
*magic
= magic_open(flags
);
364 (void)fprintf(stderr
, "%s: %s\n", progname
, strerror(errno
));
367 if (magic_load(magic
, magicfile
) == -1) {
368 (void)fprintf(stderr
, "%s: %s\n",
369 progname
, magic_error(magic
));
377 * unwrap -- read a file of filenames, do each one.
380 unwrap(struct magic_set
*ms
, const char *fn
)
382 char buf
[MAXPATHLEN
];
387 if (strcmp("-", fn
) == 0) {
391 if ((f
= fopen(fn
, "r")) == NULL
) {
392 (void)fprintf(stderr
, "%s: Cannot open `%s' (%s).\n",
393 progname
, fn
, strerror(errno
));
397 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
398 buf
[strcspn(buf
, "\n")] = '\0';
399 cwid
= file_mbswidth(buf
);
407 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
408 buf
[strcspn(buf
, "\n")] = '\0';
409 e
|= process(ms
, buf
, wid
);
411 (void)fflush(stdout
);
419 * Called for each input file on the command line (or in a list of files)
422 process(struct magic_set
*ms
, const char *inname
, int wid
)
425 int std_in
= strcmp(inname
, "-") == 0;
427 if (wid
> 0 && !bflag
) {
428 (void)printf("%s", std_in
? "/dev/stdin" : inname
);
430 (void)putc('\0', stdout
);
432 (void)printf("%s", separator
);
434 (int) (nopad
? 0 : (wid
- file_mbswidth(inname
))), "");
437 type
= magic_file(ms
, std_in
? NULL
: inname
);
439 (void)printf("ERROR: %s\n", magic_error(ms
));
442 (void)printf("%s\n", type
);
448 file_mbswidth(const char *s
)
450 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
451 size_t bytesconsumed
, old_n
, n
, width
= 0;
454 (void)memset(&state
, 0, sizeof(mbstate_t));
455 old_n
= n
= strlen(s
);
458 bytesconsumed
= mbrtowc(&nextchar
, s
, n
, &state
);
459 if (bytesconsumed
== (size_t)(-1) ||
460 bytesconsumed
== (size_t)(-2)) {
461 /* Something went wrong, return something reasonable */
466 * do what strlen() would do, so that caller
471 width
+= wcwidth(nextchar
);
473 s
+= bytesconsumed
, n
-= bytesconsumed
;
484 (void)fprintf(stderr
, USAGE
, progname
, progname
);
485 (void)fputs("Try `file --help' for more information.\n", stderr
);
493 "Usage: file [OPTION...] [FILE...]\n"
494 "Determine type of FILEs.\n"
496 #define OPT(shortname, longname, opt, doc) \
497 fprintf(stderr, " -%c, --" longname doc, shortname);
498 #define OPT_LONGONLY(longname, opt, doc) \
499 fprintf(stderr, " --" longname doc);
500 #include "file_opts.h"