xref: /freebsd/contrib/file/src/file.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*
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.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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  *
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
26  * SUCH DAMAGE.
27  */
28 /*
29  * file - find type of a file or files - main program.
30  */
31 
32 #include "file.h"
33 
34 #ifndef	lint
35 FILE_RCSID("@(#)$File: file.c,v 1.172 2016/10/24 15:21:07 christos Exp $")
36 #endif	/* lint */
37 
38 #include "magic.h"
39 
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #ifdef RESTORE_TIME
44 # if (__COHERENT__ >= 0x420)
45 #  include <sys/utime.h>
46 # else
47 #  ifdef USE_UTIMES
48 #   include <sys/time.h>
49 #  else
50 #   include <utime.h>
51 #  endif
52 # endif
53 #endif
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>	/* for read() */
56 #endif
57 #ifdef HAVE_WCHAR_H
58 #include <wchar.h>
59 #endif
60 
61 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
62 #include <getopt.h>
63 #ifndef HAVE_GETOPT_LONG
64 int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
65 #endif
66 #else
67 #include "mygetopt.h"
68 #endif
69 
70 #ifdef S_IFLNK
71 #define FILE_FLAGS "-bcEhikLlNnprsvzZ0"
72 #else
73 #define FILE_FLAGS "-bcEiklNnprsvzZ0"
74 #endif
75 
76 # define USAGE  \
77     "Usage: %s [" FILE_FLAGS \
78 	"] [--apple] [--extension] [--mime-encoding] [--mime-type]\n" \
79     "            [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \
80     "file ...\n" \
81     "       %s -C [-m magicfiles]\n" \
82     "       %s [--help]\n"
83 
84 private int 		/* Global command-line options 		*/
85 	bflag = 0,	/* brief output format	 		*/
86 	nopad = 0,	/* Don't pad output			*/
87 	nobuffer = 0,   /* Do not buffer stdout 		*/
88 	nulsep = 0;	/* Append '\0' to the separator		*/
89 
90 private const char *separator = ":";	/* Default field separator	*/
91 private const struct option long_options[] = {
92 #define OPT_HELP		1
93 #define OPT_APPLE		2
94 #define OPT_EXTENSIONS		3
95 #define OPT_MIME_TYPE		4
96 #define OPT_MIME_ENCODING	5
97 #define OPT(shortname, longname, opt, def, doc)      \
98     {longname, opt, NULL, shortname},
99 #define OPT_LONGONLY(longname, opt, def, doc, id)        \
100     {longname, opt, NULL, id},
101 #include "file_opts.h"
102 #undef OPT
103 #undef OPT_LONGONLY
104     {0, 0, NULL, 0}
105 };
106 #define OPTSTRING	"bcCde:Ef:F:hiklLm:nNpP:rsvzZ0"
107 
108 private const struct {
109 	const char *name;
110 	int value;
111 } nv[] = {
112 	{ "apptype",	MAGIC_NO_CHECK_APPTYPE },
113 	{ "ascii",	MAGIC_NO_CHECK_ASCII },
114 	{ "cdf",	MAGIC_NO_CHECK_CDF },
115 	{ "compress",	MAGIC_NO_CHECK_COMPRESS },
116 	{ "elf",	MAGIC_NO_CHECK_ELF },
117 	{ "encoding",	MAGIC_NO_CHECK_ENCODING },
118 	{ "soft",	MAGIC_NO_CHECK_SOFT },
119 	{ "tar",	MAGIC_NO_CHECK_TAR },
120 	{ "text",	MAGIC_NO_CHECK_TEXT },	/* synonym for ascii */
121 	{ "tokens",	MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */
122 };
123 
124 private struct {
125 	const char *name;
126 	int tag;
127 	size_t value;
128 } pm[] = {
129 	{ "indir",	MAGIC_PARAM_INDIR_MAX, 0 },
130 	{ "name",	MAGIC_PARAM_NAME_MAX, 0 },
131 	{ "elf_phnum",	MAGIC_PARAM_ELF_PHNUM_MAX, 0 },
132 	{ "elf_shnum",	MAGIC_PARAM_ELF_SHNUM_MAX, 0 },
133 	{ "elf_notes",	MAGIC_PARAM_ELF_NOTES_MAX, 0 },
134 	{ "regex",	MAGIC_PARAM_REGEX_MAX, 0 },
135 	{ "bytes",	MAGIC_PARAM_BYTES_MAX, 0 },
136 };
137 
138 private char *progname;		/* used throughout 		*/
139 private int posixly;
140 
141 #ifdef __dead
142 __dead
143 #endif
144 private void usage(void);
145 private void docprint(const char *, int);
146 #ifdef __dead
147 __dead
148 #endif
149 private void help(void);
150 
151 private int unwrap(struct magic_set *, const char *);
152 private int process(struct magic_set *ms, const char *, int);
153 private struct magic_set *load(const char *, int);
154 private void setparam(const char *);
155 private void applyparam(magic_t);
156 
157 
158 /*
159  * main - parse arguments and handle options
160  */
161 int
162 main(int argc, char *argv[])
163 {
164 	int c;
165 	size_t i;
166 	int action = 0, didsomefiles = 0, errflg = 0;
167 	int flags = 0, e = 0;
168 	struct magic_set *magic = NULL;
169 	int longindex;
170 	const char *magicfile = NULL;		/* where the magic is	*/
171 
172 	/* makes islower etc work for other langs */
173 #ifdef HAVE_SETLOCALE
174 	(void)setlocale(LC_CTYPE, "");
175 #endif
176 
177 #ifdef __EMX__
178 	/* sh-like wildcard expansion! Shouldn't hurt at least ... */
179 	_wildcard(&argc, &argv);
180 #endif
181 
182 	if ((progname = strrchr(argv[0], '/')) != NULL)
183 		progname++;
184 	else
185 		progname = argv[0];
186 
187 #ifdef S_IFLNK
188 	posixly = getenv("POSIXLY_CORRECT") != NULL;
189 	flags |=  posixly ? MAGIC_SYMLINK : 0;
190 #endif
191 	while ((c = getopt_long(argc, argv, OPTSTRING, long_options,
192 	    &longindex)) != -1)
193 		switch (c) {
194 		case OPT_HELP:
195 			help();
196 			break;
197 		case OPT_APPLE:
198 			flags |= MAGIC_APPLE;
199 			break;
200 		case OPT_EXTENSIONS:
201 			flags |= MAGIC_EXTENSION;
202 			break;
203 		case OPT_MIME_TYPE:
204 			flags |= MAGIC_MIME_TYPE;
205 			break;
206 		case OPT_MIME_ENCODING:
207 			flags |= MAGIC_MIME_ENCODING;
208 			break;
209 		case '0':
210 			nulsep++;
211 			break;
212 		case 'b':
213 			bflag++;
214 			break;
215 		case 'c':
216 			action = FILE_CHECK;
217 			break;
218 		case 'C':
219 			action = FILE_COMPILE;
220 			break;
221 		case 'd':
222 			flags |= MAGIC_DEBUG|MAGIC_CHECK;
223 			break;
224 		case 'E':
225 			flags |= MAGIC_ERROR;
226 			break;
227 		case 'e':
228 			for (i = 0; i < sizeof(nv) / sizeof(nv[0]); i++)
229 				if (strcmp(nv[i].name, optarg) == 0)
230 					break;
231 
232 			if (i == sizeof(nv) / sizeof(nv[0]))
233 				errflg++;
234 			else
235 				flags |= nv[i].value;
236 			break;
237 
238 		case 'f':
239 			if(action)
240 				usage();
241 			if (magic == NULL)
242 				if ((magic = load(magicfile, flags)) == NULL)
243 					return 1;
244 			applyparam(magic);
245 			e |= unwrap(magic, optarg);
246 			++didsomefiles;
247 			break;
248 		case 'F':
249 			separator = optarg;
250 			break;
251 		case 'i':
252 			flags |= MAGIC_MIME;
253 			break;
254 		case 'k':
255 			flags |= MAGIC_CONTINUE;
256 			break;
257 		case 'l':
258 			action = FILE_LIST;
259 			break;
260 		case 'm':
261 			magicfile = optarg;
262 			break;
263 		case 'n':
264 			++nobuffer;
265 			break;
266 		case 'N':
267 			++nopad;
268 			break;
269 #if defined(HAVE_UTIME) || defined(HAVE_UTIMES)
270 		case 'p':
271 			flags |= MAGIC_PRESERVE_ATIME;
272 			break;
273 #endif
274 		case 'P':
275 			setparam(optarg);
276 			break;
277 		case 'r':
278 			flags |= MAGIC_RAW;
279 			break;
280 		case 's':
281 			flags |= MAGIC_DEVICES;
282 			break;
283 		case 'v':
284 			if (magicfile == NULL)
285 				magicfile = magic_getpath(magicfile, action);
286 			(void)fprintf(stdout, "%s-%s\n", progname, VERSION);
287 			(void)fprintf(stdout, "magic file from %s\n",
288 				       magicfile);
289 			return 0;
290 		case 'z':
291 			flags |= MAGIC_COMPRESS;
292 			break;
293 
294 		case 'Z':
295 			flags |= MAGIC_COMPRESS|MAGIC_COMPRESS_TRANSP;
296 			break;
297 #ifdef S_IFLNK
298 		case 'L':
299 			flags |= MAGIC_SYMLINK;
300 			break;
301 		case 'h':
302 			flags &= ~MAGIC_SYMLINK;
303 			break;
304 #endif
305 		case '?':
306 		default:
307 			errflg++;
308 			break;
309 		}
310 
311 	if (errflg) {
312 		usage();
313 	}
314 	if (e)
315 		return e;
316 
317 	if (MAGIC_VERSION != magic_version())
318 		(void)fprintf(stderr, "%s: compiled magic version [%d] "
319 		    "does not match with shared library magic version [%d]\n",
320 		    progname, MAGIC_VERSION, magic_version());
321 
322 	switch(action) {
323 	case FILE_CHECK:
324 	case FILE_COMPILE:
325 	case FILE_LIST:
326 		/*
327 		 * Don't try to check/compile ~/.magic unless we explicitly
328 		 * ask for it.
329 		 */
330 		magic = magic_open(flags|MAGIC_CHECK);
331 		if (magic == NULL) {
332 			(void)fprintf(stderr, "%s: %s\n", progname,
333 			    strerror(errno));
334 			return 1;
335 		}
336 
337 
338 		switch(action) {
339 		case FILE_CHECK:
340 			c = magic_check(magic, magicfile);
341 			break;
342 		case FILE_COMPILE:
343 			c = magic_compile(magic, magicfile);
344 			break;
345 		case FILE_LIST:
346 			c = magic_list(magic, magicfile);
347 			break;
348 		default:
349 			abort();
350 		}
351 		if (c == -1) {
352 			(void)fprintf(stderr, "%s: %s\n", progname,
353 			    magic_error(magic));
354 			e = 1;
355 			goto out;
356 		}
357 		goto out;
358 	default:
359 		if (magic == NULL)
360 			if ((magic = load(magicfile, flags)) == NULL)
361 				return 1;
362 		applyparam(magic);
363 	}
364 
365 	if (optind == argc) {
366 		if (!didsomefiles)
367 			usage();
368 	}
369 	else {
370 		size_t j, wid, nw;
371 		for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
372 			nw = file_mbswidth(argv[j]);
373 			if (nw > wid)
374 				wid = nw;
375 		}
376 		/*
377 		 * If bflag is only set twice, set it depending on
378 		 * number of files [this is undocumented, and subject to change]
379 		 */
380 		if (bflag == 2) {
381 			bflag = optind >= argc - 1;
382 		}
383 		for (; optind < argc; optind++)
384 			e |= process(magic, argv[optind], wid);
385 	}
386 
387 out:
388 	if (magic)
389 		magic_close(magic);
390 	return e;
391 }
392 
393 private void
394 applyparam(magic_t magic)
395 {
396 	size_t i;
397 
398 	for (i = 0; i < __arraycount(pm); i++) {
399 		if (pm[i].value == 0)
400 			continue;
401 		if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
402 			(void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
403 				pm[i].name, strerror(errno));
404 			exit(1);
405 		}
406 	}
407 }
408 
409 private void
410 setparam(const char *p)
411 {
412 	size_t i;
413 	char *s;
414 
415 	if ((s = strchr(p, '=')) == NULL)
416 		goto badparm;
417 
418 	for (i = 0; i < __arraycount(pm); i++) {
419 		if (strncmp(p, pm[i].name, s - p) != 0)
420 			continue;
421 		pm[i].value = atoi(s + 1);
422 		return;
423 	}
424 badparm:
425 	(void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
426 	exit(1);
427 }
428 
429 private struct magic_set *
430 /*ARGSUSED*/
431 load(const char *magicfile, int flags)
432 {
433 	struct magic_set *magic = magic_open(flags);
434 	const char *e;
435 
436 	if (magic == NULL) {
437 		(void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
438 		return NULL;
439 	}
440 	if (magic_load(magic, magicfile) == -1) {
441 		(void)fprintf(stderr, "%s: %s\n",
442 		    progname, magic_error(magic));
443 		magic_close(magic);
444 		return NULL;
445 	}
446 	if ((e = magic_error(magic)) != NULL)
447 		(void)fprintf(stderr, "%s: Warning: %s\n", progname, e);
448 	return magic;
449 }
450 
451 /*
452  * unwrap -- read a file of filenames, do each one.
453  */
454 private int
455 unwrap(struct magic_set *ms, const char *fn)
456 {
457 	FILE *f;
458 	ssize_t len;
459 	char *line = NULL;
460 	size_t llen = 0;
461 	int wid = 0, cwid;
462 	int e = 0;
463 
464 	if (strcmp("-", fn) == 0) {
465 		f = stdin;
466 		wid = 1;
467 	} else {
468 		if ((f = fopen(fn, "r")) == NULL) {
469 			(void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
470 			    progname, fn, strerror(errno));
471 			return 1;
472 		}
473 
474 		while ((len = getline(&line, &llen, f)) > 0) {
475 			if (line[len - 1] == '\n')
476 				line[len - 1] = '\0';
477 			cwid = file_mbswidth(line);
478 			if (cwid > wid)
479 				wid = cwid;
480 		}
481 
482 		rewind(f);
483 	}
484 
485 	while ((len = getline(&line, &llen, f)) > 0) {
486 		if (line[len - 1] == '\n')
487 			line[len - 1] = '\0';
488 		e |= process(ms, line, wid);
489 		if(nobuffer)
490 			(void)fflush(stdout);
491 	}
492 
493 	free(line);
494 	(void)fclose(f);
495 	return e;
496 }
497 
498 /*
499  * Called for each input file on the command line (or in a list of files)
500  */
501 private int
502 process(struct magic_set *ms, const char *inname, int wid)
503 {
504 	const char *type, c = nulsep > 1 ? '\0' : '\n';
505 	int std_in = strcmp(inname, "-") == 0;
506 
507 	if (wid > 0 && !bflag) {
508 		(void)printf("%s", std_in ? "/dev/stdin" : inname);
509 		if (nulsep)
510 			(void)putc('\0', stdout);
511 		if (nulsep < 2) {
512 			(void)printf("%s", separator);
513 			(void)printf("%*s ",
514 			    (int) (nopad ? 0 : (wid - file_mbswidth(inname))),
515 			    "");
516 		}
517 	}
518 
519 	type = magic_file(ms, std_in ? NULL : inname);
520 
521 	if (type == NULL) {
522 		(void)printf("ERROR: %s%c", magic_error(ms), c);
523 		return 1;
524 	} else {
525 		(void)printf("%s%c", type, c);
526 		return 0;
527 	}
528 }
529 
530 protected size_t
531 file_mbswidth(const char *s)
532 {
533 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
534 	size_t bytesconsumed, old_n, n, width = 0;
535 	mbstate_t state;
536 	wchar_t nextchar;
537 	(void)memset(&state, 0, sizeof(mbstate_t));
538 	old_n = n = strlen(s);
539 
540 	while (n > 0) {
541 		bytesconsumed = mbrtowc(&nextchar, s, n, &state);
542 		if (bytesconsumed == (size_t)(-1) ||
543 		    bytesconsumed == (size_t)(-2)) {
544 			/* Something went wrong, return something reasonable */
545 			return old_n;
546 		}
547 		if (s[0] == '\n') {
548 			/*
549 			 * do what strlen() would do, so that caller
550 			 * is always right
551 			 */
552 			width++;
553 		} else {
554 			int w = wcwidth(nextchar);
555 			if (w > 0)
556 				width += w;
557 		}
558 
559 		s += bytesconsumed, n -= bytesconsumed;
560 	}
561 	return width;
562 #else
563 	return strlen(s);
564 #endif
565 }
566 
567 private void
568 usage(void)
569 {
570 	(void)fprintf(stderr, USAGE, progname, progname, progname);
571 	exit(1);
572 }
573 
574 private void
575 defprint(int def)
576 {
577 	if (!def)
578 		return;
579 	if (((def & 1) && posixly) || ((def & 2) && !posixly))
580 		fprintf(stdout, " (default)");
581 	fputc('\n', stdout);
582 }
583 
584 private void
585 docprint(const char *opts, int def)
586 {
587 	size_t i;
588 	int comma;
589 	char *sp, *p;
590 
591 	p = strstr(opts, "%o");
592 	if (p == NULL) {
593 		fprintf(stdout, "%s", opts);
594 		defprint(def);
595 		return;
596 	}
597 
598 	for (sp = p - 1; sp > opts && *sp == ' '; sp--)
599 		continue;
600 
601 	fprintf(stdout, "%.*s", (int)(p - opts), opts);
602 
603 	comma = 0;
604 	for (i = 0; i < __arraycount(nv); i++) {
605 		fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
606 		if (i && i % 5 == 0) {
607 			fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
608 			comma = 0;
609 		}
610 	}
611 
612 	fprintf(stdout, "%s", opts + (p - opts) + 2);
613 }
614 
615 private void
616 help(void)
617 {
618 	(void)fputs(
619 "Usage: file [OPTION...] [FILE...]\n"
620 "Determine type of FILEs.\n"
621 "\n", stdout);
622 #define OPT(shortname, longname, opt, def, doc)      \
623 	fprintf(stdout, "  -%c, --" longname, shortname), \
624 	docprint(doc, def);
625 #define OPT_LONGONLY(longname, opt, def, doc, id)        \
626 	fprintf(stdout, "      --" longname),	\
627 	docprint(doc, def);
628 #include "file_opts.h"
629 #undef OPT
630 #undef OPT_LONGONLY
631 	fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n");
632 	exit(0);
633 }
634