file.c revision 302221
17011SN/A/* 212325Spsandoz * Copyright (c) Ian F. Darwin 1986-1995. 37011SN/A * Software written by Ian F. Darwin and others; 47011SN/A * maintained 1995-present by Christos Zoulas and others. 57011SN/A * 67011SN/A * Redistribution and use in source and binary forms, with or without 77011SN/A * modification, are permitted provided that the following conditions 87011SN/A * are met: 97011SN/A * 1. Redistributions of source code must retain the above copyright 107011SN/A * notice immediately at the beginning of the file, without modification, 117011SN/A * this list of conditions, and the following disclaimer. 127011SN/A * 2. Redistributions in binary form must reproduce the above copyright 137011SN/A * notice, this list of conditions and the following disclaimer in the 147011SN/A * documentation and/or other materials provided with the distribution. 157011SN/A * 167011SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 177011SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 187011SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 197011SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 207011SN/A * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 217011SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 227011SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 237011SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 247011SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 257011SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 267011SN/A * SUCH DAMAGE. 277011SN/A */ 287011SN/A/* 297011SN/A * file - find type of a file or files - main program. 307011SN/A */ 318051SN/A 327011SN/A#include "file.h" 337011SN/A 347011SN/A#ifndef lint 357011SN/AFILE_RCSID("@(#)$File: file.c,v 1.171 2016/05/17 15:52:45 christos Exp $") 367011SN/A#endif /* lint */ 377011SN/A 387011SN/A#include "magic.h" 397011SN/A 407011SN/A#include <stdlib.h> 417011SN/A#include <unistd.h> 427011SN/A#include <string.h> 437011SN/A#ifdef RESTORE_TIME 447011SN/A# if (__COHERENT__ >= 0x420) 457011SN/A# include <sys/utime.h> 467011SN/A# else 477570SN/A# ifdef USE_UTIMES 487011SN/A# include <sys/time.h> 497011SN/A# else 507011SN/A# include <utime.h> 517011SN/A# endif 527011SN/A# endif 537011SN/A#endif 547011SN/A#ifdef HAVE_UNISTD_H 557011SN/A#include <unistd.h> /* for read() */ 567011SN/A#endif 577011SN/A#ifdef HAVE_WCHAR_H 587570SN/A#include <wchar.h> 597570SN/A#endif 607570SN/A 617570SN/A#if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION) 627570SN/A#include <getopt.h> 637570SN/A#ifndef HAVE_GETOPT_LONG 647570SN/Aint getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); 657011SN/A#endif 667011SN/A#else 677011SN/A#include "mygetopt.h" 687011SN/A#endif 697011SN/A 707011SN/A#ifdef S_IFLNK 717011SN/A#define FILE_FLAGS "-bcEhikLlNnprsvzZ0" 727011SN/A#else 737011SN/A#define FILE_FLAGS "-bcEiklNnprsvzZ0" 747011SN/A#endif 757011SN/A 767011SN/A# define USAGE \ 7710298SN/A "Usage: %s [" FILE_FLAGS \ 787011SN/A "] [--apple] [--extension] [--mime-encoding] [--mime-type]\n" \ 797011SN/A " [-e testname] [-F separator] [-f namefile] [-m magicfiles] " \ 807011SN/A "file ...\n" \ 817011SN/A " %s -C [-m magicfiles]\n" \ 827011SN/A " %s [--help]\n" 837011SN/A 847011SN/Aprivate int /* Global command-line options */ 857011SN/A bflag = 0, /* brief output format */ 867011SN/A nopad = 0, /* Don't pad output */ 877011SN/A nobuffer = 0, /* Do not buffer stdout */ 887011SN/A nulsep = 0; /* Append '\0' to the separator */ 897011SN/A 907011SN/Aprivate const char *separator = ":"; /* Default field separator */ 917011SN/Aprivate const struct option long_options[] = { 927011SN/A#define OPT_HELP 1 937011SN/A#define OPT_APPLE 2 947011SN/A#define OPT_EXTENSIONS 3 957011SN/A#define OPT_MIME_TYPE 4 967011SN/A#define OPT_MIME_ENCODING 5 977011SN/A#define OPT(shortname, longname, opt, def, doc) \ 987011SN/A {longname, opt, NULL, shortname}, 997011SN/A#define OPT_LONGONLY(longname, opt, def, doc, id) \ 1007011SN/A {longname, opt, NULL, id}, 10113916Samlu#include "file_opts.h" 1027011SN/A#undef OPT 1037011SN/A#undef OPT_LONGONLY 1047011SN/A {0, 0, NULL, 0} 1057011SN/A}; 1067011SN/A#define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsvzZ0" 1077011SN/A 1087011SN/Aprivate const struct { 1097011SN/A const char *name; 1107011SN/A int value; 1117011SN/A} nv[] = { 1127011SN/A { "apptype", MAGIC_NO_CHECK_APPTYPE }, 1137011SN/A { "ascii", MAGIC_NO_CHECK_ASCII }, 1147011SN/A { "cdf", MAGIC_NO_CHECK_CDF }, 1157011SN/A { "compress", MAGIC_NO_CHECK_COMPRESS }, 1167011SN/A { "elf", MAGIC_NO_CHECK_ELF }, 1177011SN/A { "encoding", MAGIC_NO_CHECK_ENCODING }, 1187011SN/A { "soft", MAGIC_NO_CHECK_SOFT }, 1197011SN/A { "tar", MAGIC_NO_CHECK_TAR }, 1207011SN/A { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */ 1217011SN/A { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */ 1227011SN/A}; 1237011SN/A 1247011SN/Aprivate struct { 1257011SN/A const char *name; 1267011SN/A int tag; 1277011SN/A size_t value; 1287011SN/A} pm[] = { 1297011SN/A { "indir", MAGIC_PARAM_INDIR_MAX, 0 }, 1307011SN/A { "name", MAGIC_PARAM_NAME_MAX, 0 }, 1317011SN/A { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 }, 1327011SN/A { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 }, 1337011SN/A { "elf_notes", MAGIC_PARAM_ELF_NOTES_MAX, 0 }, 1347011SN/A { "regex", MAGIC_PARAM_REGEX_MAX, 0 }, 1357011SN/A { "bytes", MAGIC_PARAM_BYTES_MAX, 0 }, 1367011SN/A}; 1377011SN/A 1387011SN/Aprivate char *progname; /* used throughout */ 13912325Spsandozprivate int posixly; 1407011SN/A 14112325Spsandoz#ifdef __dead 1427011SN/A__dead 1437011SN/A#endif 1447011SN/Aprivate void usage(void); 1457011SN/Aprivate void docprint(const char *, int); 1467011SN/A#ifdef __dead 1477011SN/A__dead 1487011SN/A#endif 1497011SN/Aprivate void help(void); 1507011SN/A 1517011SN/Aprivate int unwrap(struct magic_set *, const char *); 1527011SN/Aprivate int process(struct magic_set *ms, const char *, int); 1537011SN/Aprivate struct magic_set *load(const char *, int); 1547011SN/Aprivate void setparam(const char *); 1557011SN/Aprivate void applyparam(magic_t); 1567011SN/A 1577570SN/A 1587570SN/A/* 1597570SN/A * main - parse arguments and handle options 1607570SN/A */ 1617570SN/Aint 1627570SN/Amain(int argc, char *argv[]) 1637570SN/A{ 1647570SN/A int c; 1657011SN/A size_t i; 1667011SN/A int action = 0, didsomefiles = 0, errflg = 0; 1677011SN/A int flags = 0, e = 0; 1687011SN/A struct magic_set *magic = NULL; 1697011SN/A int longindex; 1707011SN/A const char *magicfile = NULL; /* where the magic is */ 1717011SN/A 1727011SN/A /* makes islower etc work for other langs */ 1737011SN/A#ifdef HAVE_SETLOCALE 1747011SN/A (void)setlocale(LC_CTYPE, ""); 1757011SN/A#endif 1767011SN/A 1777011SN/A#ifdef __EMX__ 1787011SN/A /* sh-like wildcard expansion! Shouldn't hurt at least ... */ 1797011SN/A _wildcard(&argc, &argv); 1807011SN/A#endif 1817011SN/A 1827011SN/A if ((progname = strrchr(argv[0], '/')) != NULL) 1837011SN/A progname++; 1847011SN/A else 1857011SN/A progname = argv[0]; 1867011SN/A 1877011SN/A#ifdef S_IFLNK 1887011SN/A posixly = getenv("POSIXLY_CORRECT") != NULL; 1897011SN/A flags |= posixly ? MAGIC_SYMLINK : 0; 1907011SN/A#endif 1917011SN/A while ((c = getopt_long(argc, argv, OPTSTRING, long_options, 1927011SN/A &longindex)) != -1) 1937011SN/A switch (c) { 1947011SN/A case OPT_HELP: 1957011SN/A help(); 1967011SN/A break; 1977011SN/A case OPT_APPLE: 1987011SN/A flags |= MAGIC_APPLE; 1997011SN/A break; 2007011SN/A case OPT_EXTENSIONS: 2017011SN/A flags |= MAGIC_EXTENSION; 2027011SN/A break; 2037011SN/A case OPT_MIME_TYPE: 2047011SN/A 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 387out: 388 if (magic) 389 magic_close(magic); 390 return e; 391} 392 393private void 394applyparam(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 409private void 410setparam(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 } 424badparm: 425 (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p); 426 exit(1); 427} 428 429private struct magic_set * 430/*ARGSUSED*/ 431load(const char *magicfile, int flags) 432{ 433 struct magic_set *magic = magic_open(flags); 434 if (magic == NULL) { 435 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno)); 436 return NULL; 437 } 438 if (magic_load(magic, magicfile) == -1) { 439 (void)fprintf(stderr, "%s: %s\n", 440 progname, magic_error(magic)); 441 magic_close(magic); 442 return NULL; 443 } 444 return magic; 445} 446 447/* 448 * unwrap -- read a file of filenames, do each one. 449 */ 450private int 451unwrap(struct magic_set *ms, const char *fn) 452{ 453 FILE *f; 454 ssize_t len; 455 char *line = NULL; 456 size_t llen = 0; 457 int wid = 0, cwid; 458 int e = 0; 459 460 if (strcmp("-", fn) == 0) { 461 f = stdin; 462 wid = 1; 463 } else { 464 if ((f = fopen(fn, "r")) == NULL) { 465 (void)fprintf(stderr, "%s: Cannot open `%s' (%s).\n", 466 progname, fn, strerror(errno)); 467 return 1; 468 } 469 470 while ((len = getline(&line, &llen, f)) > 0) { 471 if (line[len - 1] == '\n') 472 line[len - 1] = '\0'; 473 cwid = file_mbswidth(line); 474 if (cwid > wid) 475 wid = cwid; 476 } 477 478 rewind(f); 479 } 480 481 while ((len = getline(&line, &llen, f)) > 0) { 482 if (line[len - 1] == '\n') 483 line[len - 1] = '\0'; 484 e |= process(ms, line, wid); 485 if(nobuffer) 486 (void)fflush(stdout); 487 } 488 489 free(line); 490 (void)fclose(f); 491 return e; 492} 493 494/* 495 * Called for each input file on the command line (or in a list of files) 496 */ 497private int 498process(struct magic_set *ms, const char *inname, int wid) 499{ 500 const char *type, c = nulsep > 1 ? '\0' : '\n'; 501 int std_in = strcmp(inname, "-") == 0; 502 503 if (wid > 0 && !bflag) { 504 (void)printf("%s", std_in ? "/dev/stdin" : inname); 505 if (nulsep) 506 (void)putc('\0', stdout); 507 if (nulsep < 2) { 508 (void)printf("%s", separator); 509 (void)printf("%*s ", 510 (int) (nopad ? 0 : (wid - file_mbswidth(inname))), 511 ""); 512 } 513 } 514 515 type = magic_file(ms, std_in ? NULL : inname); 516 517 if (type == NULL) { 518 (void)printf("ERROR: %s%c", magic_error(ms), c); 519 return 1; 520 } else { 521 (void)printf("%s%c", type, c); 522 return 0; 523 } 524} 525 526protected size_t 527file_mbswidth(const char *s) 528{ 529#if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 530 size_t bytesconsumed, old_n, n, width = 0; 531 mbstate_t state; 532 wchar_t nextchar; 533 (void)memset(&state, 0, sizeof(mbstate_t)); 534 old_n = n = strlen(s); 535 536 while (n > 0) { 537 bytesconsumed = mbrtowc(&nextchar, s, n, &state); 538 if (bytesconsumed == (size_t)(-1) || 539 bytesconsumed == (size_t)(-2)) { 540 /* Something went wrong, return something reasonable */ 541 return old_n; 542 } 543 if (s[0] == '\n') { 544 /* 545 * do what strlen() would do, so that caller 546 * is always right 547 */ 548 width++; 549 } else { 550 int w = wcwidth(nextchar); 551 if (w > 0) 552 width += w; 553 } 554 555 s += bytesconsumed, n -= bytesconsumed; 556 } 557 return width; 558#else 559 return strlen(s); 560#endif 561} 562 563private void 564usage(void) 565{ 566 (void)fprintf(stderr, USAGE, progname, progname, progname); 567 exit(1); 568} 569 570private void 571defprint(int def) 572{ 573 if (!def) 574 return; 575 if (((def & 1) && posixly) || ((def & 2) && !posixly)) 576 fprintf(stdout, " (default)"); 577 fputc('\n', stdout); 578} 579 580private void 581docprint(const char *opts, int def) 582{ 583 size_t i; 584 int comma; 585 char *sp, *p; 586 587 p = strstr(opts, "%o"); 588 if (p == NULL) { 589 fprintf(stdout, "%s", opts); 590 defprint(def); 591 return; 592 } 593 594 for (sp = p - 1; sp > opts && *sp == ' '; sp--) 595 continue; 596 597 fprintf(stdout, "%.*s", (int)(p - opts), opts); 598 599 comma = 0; 600 for (i = 0; i < __arraycount(nv); i++) { 601 fprintf(stdout, "%s%s", comma++ ? ", " : "", nv[i].name); 602 if (i && i % 5 == 0) { 603 fprintf(stdout, ",\n%*s", (int)(p - sp - 1), ""); 604 comma = 0; 605 } 606 } 607 608 fprintf(stdout, "%s", opts + (p - opts) + 2); 609} 610 611private void 612help(void) 613{ 614 (void)fputs( 615"Usage: file [OPTION...] [FILE...]\n" 616"Determine type of FILEs.\n" 617"\n", stdout); 618#define OPT(shortname, longname, opt, def, doc) \ 619 fprintf(stdout, " -%c, --" longname, shortname), \ 620 docprint(doc, def); 621#define OPT_LONGONLY(longname, opt, def, doc, id) \ 622 fprintf(stdout, " --" longname), \ 623 docprint(doc, def); 624#include "file_opts.h" 625#undef OPT 626#undef OPT_LONGONLY 627 fprintf(stdout, "\nReport bugs to http://bugs.gw.com/\n"); 628 exit(0); 629} 630