file.c (267897) | file.c (275698) |
---|---|
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: --- 18 unchanged lines hidden (view full) --- 27 */ 28/* 29 * file - find type of a file or files - main program. 30 */ 31 32#include "file.h" 33 34#ifndef lint | 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: --- 18 unchanged lines hidden (view full) --- 27 */ 28/* 29 * file - find type of a file or files - main program. 30 */ 31 32#include "file.h" 33 34#ifndef lint |
35FILE_RCSID("@(#)$File: file.c,v 1.153 2014/02/11 15:41:04 christos Exp $") | 35FILE_RCSID("@(#)$File: file.c,v 1.159 2014/11/28 02:46:39 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 --- 5 unchanged lines hidden (view full) --- 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 | 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 --- 5 unchanged lines hidden (view full) --- 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_LOCALE_H 58#include <locale.h> 59#endif | |
60#ifdef HAVE_WCHAR_H 61#include <wchar.h> 62#endif 63 64#if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION) 65#include <getopt.h> 66#ifndef HAVE_GETOPT_LONG 67int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); --- 28 unchanged lines hidden (view full) --- 96 {longname, opt, NULL, shortname}, 97#define OPT_LONGONLY(longname, opt, doc) \ 98 {longname, opt, NULL, 0}, 99#include "file_opts.h" 100#undef OPT 101#undef OPT_LONGONLY 102 {0, 0, NULL, 0} 103}; | 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 64int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); --- 28 unchanged lines hidden (view full) --- 93 {longname, opt, NULL, shortname}, 94#define OPT_LONGONLY(longname, opt, doc) \ 95 {longname, opt, NULL, 0}, 96#include "file_opts.h" 97#undef OPT 98#undef OPT_LONGONLY 99 {0, 0, NULL, 0} 100}; |
104#define OPTSTRING "bcCde:Ef:F:hiklLm:nNprsvz0" | 101#define OPTSTRING "bcCde:Ef:F:hiklLm:nNpP:rsvz0" |
105 106private const struct { 107 const char *name; 108 int value; 109} nv[] = { 110 { "apptype", MAGIC_NO_CHECK_APPTYPE }, 111 { "ascii", MAGIC_NO_CHECK_ASCII }, 112 { "cdf", MAGIC_NO_CHECK_CDF }, 113 { "compress", MAGIC_NO_CHECK_COMPRESS }, 114 { "elf", MAGIC_NO_CHECK_ELF }, 115 { "encoding", MAGIC_NO_CHECK_ENCODING }, 116 { "soft", MAGIC_NO_CHECK_SOFT }, 117 { "tar", MAGIC_NO_CHECK_TAR }, 118 { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */ 119 { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */ 120}; 121 | 102 103private const struct { 104 const char *name; 105 int value; 106} nv[] = { 107 { "apptype", MAGIC_NO_CHECK_APPTYPE }, 108 { "ascii", MAGIC_NO_CHECK_ASCII }, 109 { "cdf", MAGIC_NO_CHECK_CDF }, 110 { "compress", MAGIC_NO_CHECK_COMPRESS }, 111 { "elf", MAGIC_NO_CHECK_ELF }, 112 { "encoding", MAGIC_NO_CHECK_ENCODING }, 113 { "soft", MAGIC_NO_CHECK_SOFT }, 114 { "tar", MAGIC_NO_CHECK_TAR }, 115 { "text", MAGIC_NO_CHECK_TEXT }, /* synonym for ascii */ 116 { "tokens", MAGIC_NO_CHECK_TOKENS }, /* OBSOLETE: ignored for backwards compatibility */ 117}; 118 |
119private struct { 120 const char *name; 121 int tag; 122 size_t value; 123} pm[] = { 124 { "indir", MAGIC_PARAM_INDIR_MAX, 0 }, 125 { "name", MAGIC_PARAM_NAME_MAX, 0 }, 126 { "elf_phnum", MAGIC_PARAM_ELF_PHNUM_MAX, 0 }, 127 { "elf_shnum", MAGIC_PARAM_ELF_SHNUM_MAX, 0 }, 128}; 129 |
|
122private char *progname; /* used throughout */ 123 124private void usage(void); 125private void docprint(const char *); 126private void help(void); 127 128private int unwrap(struct magic_set *, const char *); 129private int process(struct magic_set *ms, const char *, int); 130private struct magic_set *load(const char *, int); | 130private char *progname; /* used throughout */ 131 132private void usage(void); 133private void docprint(const char *); 134private void help(void); 135 136private int unwrap(struct magic_set *, const char *); 137private int process(struct magic_set *ms, const char *, int); 138private struct magic_set *load(const char *, int); |
139private void setparam(const char *); 140private void applyparam(magic_t); |
|
131 132 133/* 134 * main - parse arguments and handle options 135 */ 136int 137main(int argc, char *argv[]) 138{ 139 int c; 140 size_t i; 141 int action = 0, didsomefiles = 0, errflg = 0; 142 int flags = 0, e = 0; 143 struct magic_set *magic = NULL; 144 int longindex; 145 const char *magicfile = NULL; /* where the magic is */ 146 147 /* makes islower etc work for other langs */ | 141 142 143/* 144 * main - parse arguments and handle options 145 */ 146int 147main(int argc, char *argv[]) 148{ 149 int c; 150 size_t i; 151 int action = 0, didsomefiles = 0, errflg = 0; 152 int flags = 0, e = 0; 153 struct magic_set *magic = NULL; 154 int longindex; 155 const char *magicfile = NULL; /* where the magic is */ 156 157 /* makes islower etc work for other langs */ |
158#ifdef HAVE_SETLOCALE |
|
148 (void)setlocale(LC_CTYPE, ""); | 159 (void)setlocale(LC_CTYPE, ""); |
160#endif |
|
149 150#ifdef __EMX__ 151 /* sh-like wildcard expansion! Shouldn't hurt at least ... */ 152 _wildcard(&argc, &argv); 153#endif 154 155 if ((progname = strrchr(argv[0], '/')) != NULL) 156 progname++; --- 81 unchanged lines hidden (view full) --- 238 case 'N': 239 ++nopad; 240 break; 241#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) 242 case 'p': 243 flags |= MAGIC_PRESERVE_ATIME; 244 break; 245#endif | 161 162#ifdef __EMX__ 163 /* sh-like wildcard expansion! Shouldn't hurt at least ... */ 164 _wildcard(&argc, &argv); 165#endif 166 167 if ((progname = strrchr(argv[0], '/')) != NULL) 168 progname++; --- 81 unchanged lines hidden (view full) --- 250 case 'N': 251 ++nopad; 252 break; 253#if defined(HAVE_UTIME) || defined(HAVE_UTIMES) 254 case 'p': 255 flags |= MAGIC_PRESERVE_ATIME; 256 break; 257#endif |
258 case 'P': 259 setparam(optarg); 260 break; |
|
246 case 'r': 247 flags |= MAGIC_RAW; 248 break; | 261 case 'r': 262 flags |= MAGIC_RAW; 263 break; |
264 break; |
|
249 case 's': 250 flags |= MAGIC_DEVICES; 251 break; 252 case 'v': 253 if (magicfile == NULL) 254 magicfile = magic_getpath(magicfile, action); 255 (void)fprintf(stdout, "%s-%s\n", progname, VERSION); 256 (void)fprintf(stdout, "magic file from %s\n", --- 36 unchanged lines hidden (view full) --- 293 * ask for it. 294 */ 295 magic = magic_open(flags|MAGIC_CHECK); 296 if (magic == NULL) { 297 (void)fprintf(stderr, "%s: %s\n", progname, 298 strerror(errno)); 299 return 1; 300 } | 265 case 's': 266 flags |= MAGIC_DEVICES; 267 break; 268 case 'v': 269 if (magicfile == NULL) 270 magicfile = magic_getpath(magicfile, action); 271 (void)fprintf(stdout, "%s-%s\n", progname, VERSION); 272 (void)fprintf(stdout, "magic file from %s\n", --- 36 unchanged lines hidden (view full) --- 309 * ask for it. 310 */ 311 magic = magic_open(flags|MAGIC_CHECK); 312 if (magic == NULL) { 313 (void)fprintf(stderr, "%s: %s\n", progname, 314 strerror(errno)); 315 return 1; 316 } |
317 318 |
|
301 switch(action) { 302 case FILE_CHECK: 303 c = magic_check(magic, magicfile); 304 break; 305 case FILE_COMPILE: 306 c = magic_compile(magic, magicfile); 307 break; 308 case FILE_LIST: --- 7 unchanged lines hidden (view full) --- 316 magic_error(magic)); 317 return 1; 318 } 319 return 0; 320 default: 321 if (magic == NULL) 322 if ((magic = load(magicfile, flags)) == NULL) 323 return 1; | 319 switch(action) { 320 case FILE_CHECK: 321 c = magic_check(magic, magicfile); 322 break; 323 case FILE_COMPILE: 324 c = magic_compile(magic, magicfile); 325 break; 326 case FILE_LIST: --- 7 unchanged lines hidden (view full) --- 334 magic_error(magic)); 335 return 1; 336 } 337 return 0; 338 default: 339 if (magic == NULL) 340 if ((magic = load(magicfile, flags)) == NULL) 341 return 1; |
324 break; | 342 applyparam(magic); |
325 } 326 327 if (optind == argc) { 328 if (!didsomefiles) 329 usage(); 330 } 331 else { 332 size_t j, wid, nw; --- 13 unchanged lines hidden (view full) --- 346 e |= process(magic, argv[optind], wid); 347 } 348 349 if (magic) 350 magic_close(magic); 351 return e; 352} 353 | 343 } 344 345 if (optind == argc) { 346 if (!didsomefiles) 347 usage(); 348 } 349 else { 350 size_t j, wid, nw; --- 13 unchanged lines hidden (view full) --- 364 e |= process(magic, argv[optind], wid); 365 } 366 367 if (magic) 368 magic_close(magic); 369 return e; 370} 371 |
372private void 373applyparam(magic_t magic) 374{ 375 size_t i; |
|
354 | 376 |
377 for (i = 0; i < __arraycount(pm); i++) { 378 if (pm[i].value == 0) 379 continue; 380 if (magic_setparam(magic, pm[i].tag, &pm[i].value) == -1) { 381 (void)fprintf(stderr, "%s: Can't set %s %s\n", progname, 382 pm[i].name, strerror(errno)); 383 exit(1); 384 } 385 } 386} 387 388private void 389setparam(const char *p) 390{ 391 size_t i; 392 char *s; 393 394 if ((s = strchr(p, '=')) == NULL) 395 goto badparm; 396 397 for (i = 0; i < __arraycount(pm); i++) { 398 if (strncmp(p, pm[i].name, s - p) != 0) 399 continue; 400 pm[i].value = atoi(s + 1); 401 return; 402 } 403badparm: 404 (void)fprintf(stderr, "%s: Unknown param %s\n", progname, p); 405 exit(1); 406} 407 |
|
355private struct magic_set * 356/*ARGSUSED*/ 357load(const char *magicfile, int flags) 358{ 359 struct magic_set *magic = magic_open(flags); 360 if (magic == NULL) { 361 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno)); 362 return NULL; --- 178 unchanged lines hidden --- | 408private struct magic_set * 409/*ARGSUSED*/ 410load(const char *magicfile, int flags) 411{ 412 struct magic_set *magic = magic_open(flags); 413 if (magic == NULL) { 414 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno)); 415 return NULL; --- 178 unchanged lines hidden --- |