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.
35 FILE_RCSID("@(#)$File: file.c,v 1.131 2009/02/13 18:48:05 christos Exp $")
44 # if (__COHERENT__ >= 0x420)
45 # include <sys/utime.h>
48 # include <sys/time.h>
55 #include <unistd.h> /* for read() */
64 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
69 #ifndef HAVE_GETOPT_LONG
70 int getopt_long(int argc
, char * const *argv
, const char *optstring
, const struct option
*longopts
, int *longindex
);
73 #include <netinet/in.h> /* for byte swapping */
75 #include "patchlevel.h"
78 #define SYMLINKFLAG "Lh"
80 #define SYMLINKFLAG ""
83 # define USAGE "Usage: %s [-bcik" SYMLINKFLAG "nNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file...\n %s -C -m magicfiles\n"
86 #define MAXPATHLEN 1024
89 private int /* Global command-line options */
90 bflag
= 0, /* brief output format */
91 nopad
= 0, /* Don't pad output */
92 nobuffer
= 0, /* Do not buffer stdout */
93 nulsep
= 0; /* Append '\0' to the separator */
95 private const char *default_magicfile
= MAGIC
;
96 private const char *separator
= ":"; /* Default field separator */
97 private const char hmagic
[] = "/.magic";
98 private const struct option long_options
[] = {
99 #define OPT(shortname, longname, opt, doc) \
100 {longname, opt, NULL, shortname},
101 #define OPT_LONGONLY(longname, opt, doc) \
102 {longname, opt, NULL, 0},
103 #include "file_opts.h"
108 #define OPTSTRING "bcCde:f:F:hikLm:nNprsvz0"
110 private const struct {
114 { "apptype", MAGIC_NO_CHECK_APPTYPE
},
115 { "ascii", MAGIC_NO_CHECK_ASCII
},
116 { "cdf", MAGIC_NO_CHECK_CDF
},
117 { "compress", MAGIC_NO_CHECK_COMPRESS
},
118 { "elf", MAGIC_NO_CHECK_ELF
},
119 { "encoding", MAGIC_NO_CHECK_ENCODING
},
120 { "soft", MAGIC_NO_CHECK_SOFT
},
121 { "tar", MAGIC_NO_CHECK_TAR
},
122 { "tokens", MAGIC_NO_CHECK_TOKENS
},
125 private char *progname
; /* used throughout */
127 private void usage(void);
128 private void help(void);
129 int main(int, char *[]);
131 private int unwrap(struct magic_set
*, const char *);
132 private int process(struct magic_set
*ms
, const char *, int);
133 private struct magic_set
*load(const char *, int);
137 * main - parse arguments and handle options
140 main(int argc
, char *argv
[])
144 int action
= 0, didsomefiles
= 0, errflg
= 0;
145 int flags
= 0, e
= 0;
146 char *home
, *usermagic
;
147 struct magic_set
*magic
= NULL
;
148 char magicpath
[2 * MAXPATHLEN
+ 2];
150 const char *magicfile
; /* where the magic is */
152 /* makes islower etc work for other langs */
153 (void)setlocale(LC_CTYPE
, "");
156 /* sh-like wildcard expansion! Shouldn't hurt at least ... */
157 _wildcard(&argc
, &argv
);
160 if ((progname
= strrchr(argv
[0], '/')) != NULL
)
165 magicfile
= default_magicfile
;
166 if ((usermagic
= getenv("MAGIC")) != NULL
)
167 magicfile
= usermagic
;
169 if ((home
= getenv("HOME")) != NULL
) {
170 (void)snprintf(magicpath
, sizeof(magicpath
), "%s%s",
172 if (access(magicpath
, R_OK
) == 0) {
173 (void)snprintf(magicpath
, sizeof(magicpath
),
174 "%s%s:%s", home
, hmagic
, magicfile
);
175 magicfile
= magicpath
;
180 flags
|= getenv("POSIXLY_CORRECT") ? MAGIC_SYMLINK
: 0;
182 while ((c
= getopt_long(argc
, argv
, OPTSTRING
, long_options
,
191 flags
|= MAGIC_APPLE
;
194 flags
|= MAGIC_MIME_TYPE
;
197 flags
|= MAGIC_MIME_ENCODING
;
211 action
= FILE_COMPILE
;
214 flags
|= MAGIC_DEBUG
|MAGIC_CHECK
;
217 for (i
= 0; i
< sizeof(nv
) / sizeof(nv
[0]); i
++)
218 if (strcmp(nv
[i
].name
, optarg
) == 0)
221 if (i
== sizeof(nv
) / sizeof(nv
[0]))
224 flags
|= nv
[i
].value
;
231 if ((magic
= load(magicfile
, flags
)) == NULL
)
233 e
|= unwrap(magic
, optarg
);
243 flags
|= MAGIC_CONTINUE
;
254 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
256 flags
|= MAGIC_PRESERVE_ATIME
;
263 flags
|= MAGIC_DEVICES
;
266 (void)fprintf(stderr
, "%s-%d.%.2d\n", progname
,
267 FILE_VERSION_MAJOR
, patchlevel
);
268 (void)fprintf(stderr
, "magic file from %s\n",
272 flags
|= MAGIC_COMPRESS
;
276 flags
|= MAGIC_SYMLINK
;
279 flags
&= ~MAGIC_SYMLINK
;
298 * Don't try to check/compile ~/.magic unless we explicitly
301 if (magicfile
== magicpath
)
302 magicfile
= default_magicfile
;
303 magic
= magic_open(flags
|MAGIC_CHECK
);
305 (void)fprintf(stderr
, "%s: %s\n", progname
,
309 c
= action
== FILE_CHECK
? magic_check(magic
, magicfile
) :
310 magic_compile(magic
, magicfile
);
312 (void)fprintf(stderr
, "%s: %s\n", progname
,
319 if ((magic
= load(magicfile
, flags
)) == NULL
)
324 if (optind
== argc
) {
330 for (wid
= 0, j
= (size_t)optind
; j
< (size_t)argc
; j
++) {
331 nw
= file_mbswidth(argv
[j
]);
336 * If bflag is only set twice, set it depending on
337 * number of files [this is undocumented, and subject to change]
340 bflag
= optind
>= argc
- 1;
342 for (; optind
< argc
; optind
++)
343 e
|= process(magic
, argv
[optind
], wid
);
352 private struct magic_set
*
354 load(const char *magicfile
, int flags
)
356 struct magic_set
*magic
= magic_open(flags
);
358 (void)fprintf(stderr
, "%s: %s\n", progname
, strerror(errno
));
361 if (magic_load(magic
, magicfile
) == -1) {
362 (void)fprintf(stderr
, "%s: %s\n",
363 progname
, magic_error(magic
));
371 * unwrap -- read a file of filenames, do each one.
374 unwrap(struct magic_set
*ms
, const char *fn
)
376 char buf
[MAXPATHLEN
];
381 if (strcmp("-", fn
) == 0) {
385 if ((f
= fopen(fn
, "r")) == NULL
) {
386 (void)fprintf(stderr
, "%s: Cannot open `%s' (%s).\n",
387 progname
, fn
, strerror(errno
));
391 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
392 buf
[strcspn(buf
, "\n")] = '\0';
393 cwid
= file_mbswidth(buf
);
401 while (fgets(buf
, sizeof(buf
), f
) != NULL
) {
402 buf
[strcspn(buf
, "\n")] = '\0';
403 e
|= process(ms
, buf
, wid
);
405 (void)fflush(stdout
);
413 * Called for each input file on the command line (or in a list of files)
416 process(struct magic_set
*ms
, const char *inname
, int wid
)
419 int std_in
= strcmp(inname
, "-") == 0;
421 if (wid
> 0 && !bflag
) {
422 (void)printf("%s", std_in
? "/dev/stdin" : inname
);
424 (void)putc('\0', stdout
);
426 (void)printf("%s", separator
);
428 (int) (nopad
? 0 : (wid
- file_mbswidth(inname
))), "");
431 type
= magic_file(ms
, std_in
? NULL
: inname
);
433 (void)printf("ERROR: %s\n", magic_error(ms
));
436 (void)printf("%s\n", type
);
442 file_mbswidth(const char *s
)
444 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
445 size_t bytesconsumed
, old_n
, n
, width
= 0;
448 (void)memset(&state
, 0, sizeof(mbstate_t));
449 old_n
= n
= strlen(s
);
452 bytesconsumed
= mbrtowc(&nextchar
, s
, n
, &state
);
453 if (bytesconsumed
== (size_t)(-1) ||
454 bytesconsumed
== (size_t)(-2)) {
455 /* Something went wrong, return something reasonable */
460 * do what strlen() would do, so that caller
465 width
+= wcwidth(nextchar
);
467 s
+= bytesconsumed
, n
-= bytesconsumed
;
478 (void)fprintf(stderr
, USAGE
, progname
, progname
);
479 (void)fputs("Try `file --help' for more information.\n", stderr
);
487 "Usage: file [OPTION...] [FILE...]\n"
488 "Determine type of FILEs.\n"
490 #define OPT(shortname, longname, opt, doc) \
491 fprintf(stderr, " -%c, --" longname doc, shortname);
492 #define OPT_LONGONLY(longname, opt, doc) \
493 fprintf(stderr, " --" longname doc);
494 #include "file_opts.h"