xref: /freebsd/contrib/file/src/file.c (revision 30b72b6871140f0b29c64d41fc85c4c1d4d4b3f4)
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.170 2016/03/31 17:51:12 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 			return 1;
355 		}
356 		return 0;
357 	default:
358 		if (magic == NULL)
359 			if ((magic = load(magicfile, flags)) == NULL)
360 				return 1;
361 		applyparam(magic);
362 	}
363 
364 	if (optind == argc) {
365 		if (!didsomefiles)
366 			usage();
367 	}
368 	else {
369 		size_t j, wid, nw;
370 		for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
371 			nw = file_mbswidth(argv[j]);
372 			if (nw > wid)
373 				wid = nw;
374 		}
375 		/*
376 		 * If bflag is only set twice, set it depending on
377 		 * number of files [this is undocumented, and subject to change]
378 		 */
379 		if (bflag == 2) {
380 			bflag = optind >= argc - 1;
381 		}
382 		for (; optind < argc; optind++)
383 			e |= process(magic, argv[optind], wid);
384 	}
385 
386 	if (magic)
387 		magic_close(magic);
388 	return e;
389 }
390 
391 private void
392 applyparam(magic_t magic)
393 {
394 	size_t i;
395 
396 	for (i = 0; i < __arraycount(pm); i++) {
397 		if (pm[i].value == 0)
398 			continue;
399 		if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) {
400 			(void)fprintf(stderr, "%s: Can't set %s %s\n", progname,
401 				pm[i].name, strerror(errno));
402 			exit(1);
403 		}
404 	}
405 }
406 
407 private void
408 setparam(const char *p)
409 {
410 	size_t i;
411 	char *s;
412 
413 	if ((s = strchr(p, '=')) == NULL)
414 		goto badparm;
415 
416 	for (i = 0; i < __arraycount(pm); i++) {
417 		if (strncmp(p, pm[i].name, s - p) != 0)
418 			continue;
419 		pm[i].value = atoi(s + 1);
420 		return;
421 	}
422 badparm:
423 	(void)fprintf(stderr, "%s: Unknown param %s\n", progname, p);
424 	exit(1);
425 }
426 
427 private struct magic_set *
428 /*ARGSUSED*/
429 load(const char *magicfile, int flags)
430 {
431 	struct magic_set *magic = magic_open(flags);
432 	if (magic == NULL) {
433 		(void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
434 		return NULL;
435 	}
436 	if (magic_load(magic, magicfile) == -1) {
437 		(void)fprintf(stderr, "%s: %s\n",
438 		    progname, magic_error(magic));
439 		magic_close(magic);
440 		return NULL;
441 	}
442 	return magic;
443 }
444 
445 /*
446  * unwrap -- read a file of filenames, do each one.
447  */
448 private int
449 unwrap(struct magic_set *ms, const char *fn)
450 {
451 	FILE *f;
452 	ssize_t len;
453 	char *line = NULL;
454 	size_t llen = 0;
455 	int wid = 0, cwid;
456 	int e = 0;
457 
458 	if (strcmp("-", fn) == 0) {
459 		f = stdin;
460 		wid = 1;
461 	} else {
462 		if ((f = fopen(fn, "r")) == NULL) {
463 			(void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n",
464 			    progname, fn, strerror(errno));
465 			return 1;
466 		}
467 
468 		while ((len = getline(&line, &llen, f)) > 0) {
469 			if (line[len - 1] == '\n')
470 				line[len - 1] = '\0';
471 			cwid = file_mbswidth(line);
472 			if (cwid > wid)
473 				wid = cwid;
474 		}
475 
476 		rewind(f);
477 	}
478 
479 	while ((len = getline(&line, &llen, f)) > 0) {
480 		if (line[len - 1] == '\n')
481 			line[len - 1] = '\0';
482 		e |= process(ms, line, wid);
483 		if(nobuffer)
484 			(void)fflush(stdout);
485 	}
486 
487 	free(line);
488 	(void)fclose(f);
489 	return e;
490 }
491 
492 /*
493  * Called for each input file on the command line (or in a list of files)
494  */
495 private int
496 process(struct magic_set *ms, const char *inname, int wid)
497 {
498 	const char *type, c = nulsep > 1 ? '\0' : '\n';
499 	int std_in = strcmp(inname, "-") == 0;
500 
501 	if (wid > 0 && !bflag) {
502 		(void)printf("%s", std_in ? "/dev/stdin" : inname);
503 		if (nulsep)
504 			(void)putc('\0', stdout);
505 		if (nulsep < 2) {
506 			(void)printf("%s", separator);
507 			(void)printf("%*s ",
508 			    (int) (nopad ? 0 : (wid - file_mbswidth(inname))),
509 			    "");
510 		}
511 	}
512 
513 	type = magic_file(ms, std_in ? NULL : inname);
514 
515 	if (type == NULL) {
516 		(void)printf("ERROR: %s%c", magic_error(ms), c);
517 		return 1;
518 	} else {
519 		(void)printf("%s%c", type, c);
520 		return 0;
521 	}
522 }
523 
524 protected size_t
525 file_mbswidth(const char *s)
526 {
527 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
528 	size_t bytesconsumed, old_n, n, width = 0;
529 	mbstate_t state;
530 	wchar_t nextchar;
531 	(void)memset(&state, 0, sizeof(mbstate_t));
532 	old_n = n = strlen(s);
533 
534 	while (n > 0) {
535 		bytesconsumed = mbrtowc(&nextchar, s, n, &state);
536 		if (bytesconsumed == (size_t)(-1) ||
537 		    bytesconsumed == (size_t)(-2)) {
538 			/* Something went wrong, return something reasonable */
539 			return old_n;
540 		}
541 		if (s[0] == '\n') {
542 			/*
543 			 * do what strlen() would do, so that caller
544 			 * is always right
545 			 */
546 			width++;
547 		} else {
548 			int w = wcwidth(nextchar);
549 			if (w > 0)
550 				width += w;
551 		}
552 
553 		s += bytesconsumed, n -= bytesconsumed;
554 	}
555 	return width;
556 #else
557 	return strlen(s);
558 #endif
559 }
560 
561 private void
562 usage(void)
563 {
564 	(void)fprintf(stderr, USAGE, progname, progname, progname);
565 	exit(1);
566 }
567 
568 private void
569 defprint(int def)
570 {
571 	if (!def)
572 		return;
573 	if (((def & 1) && posixly) || ((def & 2) && !posixly))
574 		fprintf(stdout, " (default)");
575 	fputc('\n', stdout);
576 }
577 
578 private void
579 docprint(const char *opts, int def)
580 {
581 	size_t i;
582 	int comma;
583 	char *sp, *p;
584 
585 	p = strstr(opts, "%o");
586 	if (p == NULL) {
587 		fprintf(stdout, "%s", opts);
588 		defprint(def);
589 		return;
590 	}
591 
592 	for (sp = p - 1; sp > opts && *sp == ' '; sp--)
593 		continue;
594 
595 	fprintf(stdout, "%.*s", (int)(p - opts), opts);
596 
597 	comma = 0;
598 	for (i = 0; i < __arraycount(nv); i++) {
599 		fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name);
600 		if (i && i % 5 == 0) {
601 			fprintf(stdout, ",\n%*s", (int)(p - sp - 1), "");
602 			comma = 0;
603 		}
604 	}
605 
606 	fprintf(stdout, "%s", opts + (p - opts) + 2);
607 }
608 
609 private void
610 help(void)
611 {
612 	(void)fputs(
613 "Usage: file [OPTION...] [FILE...]\n"
614 "Determine type of FILEs.\n"
615 "\n", stdout);
616 #define OPT(shortname, longname, opt, def, doc)      \
617 	fprintf(stdout, "  -%c, --" longname, shortname), \
618 	docprint(doc, def);
619 #define OPT_LONGONLY(longname, opt, def, doc, id)        \
620 	fprintf(stdout, "      --" longname),	\
621 	docprint(doc, def);
622 #include "file_opts.h"
623 #undef OPT
624 #undef OPT_LONGONLY
625 	fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n");
626 	exit(0);
627 }
628