1 /* 2 * Copyright (c) Christos Zoulas 2003. 3 * All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <unistd.h> 31 #include <string.h> 32 #include <errno.h> 33 #include <time.h> 34 35 #include "magic.h" 36 37 static const char *prog; 38 39 static void * 40 xrealloc(void *p, size_t n) 41 { 42 p = realloc(p, n); 43 if (p == NULL) { 44 (void)fprintf(stderr, "%s ERROR slurping file: %s\n", 45 prog, strerror(errno)); 46 exit(10); 47 } 48 return p; 49 } 50 51 static char * 52 slurp(FILE *fp, size_t *final_len) 53 { 54 size_t len = 256; 55 int c; 56 char *l = xrealloc(NULL, len), *s = l; 57 58 for (c = getc(fp); c != EOF; c = getc(fp)) { 59 if (s == l + len) { 60 s = l + len; 61 len *= 2; 62 l = xrealloc(l, len); 63 } 64 *s++ = c; 65 } 66 if (s != l && s[-1] == '\n') 67 s--; 68 if (s == l + len) { 69 l = xrealloc(l, len + 1); 70 s = l + len; 71 } 72 *s++ = '\0'; 73 74 *final_len = s - l; 75 return xrealloc(l, s - l); 76 } 77 78 int 79 main(int argc, char **argv) 80 { 81 struct magic_set *ms = NULL; 82 const char *result; 83 size_t result_len, desired_len; 84 char *desired = NULL; 85 int e = EXIT_FAILURE, flags, c; 86 FILE *fp; 87 88 setenv("TZ", "UTC", 1); 89 tzset(); 90 91 92 prog = strrchr(argv[0], '/'); 93 if (prog) 94 prog++; 95 else 96 prog = argv[0]; 97 98 if (argc == 1) 99 return 0; 100 101 flags = 0; 102 while ((c = getopt(argc, argv, "ek")) != -1) 103 switch (c) { 104 case 'e': 105 flags |= MAGIC_ERROR; 106 break; 107 case 'k': 108 flags |= MAGIC_CONTINUE; 109 break; 110 default: 111 goto usage; 112 } 113 114 argc -= optind; 115 argv += optind; 116 if (argc != 2) { 117 usage: 118 (void)fprintf(stderr, 119 "Usage: %s [-ek] TEST-FILE RESULT\n", prog); 120 goto bad; 121 } 122 123 ms = magic_open(flags); 124 if (ms == NULL) { 125 (void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n", 126 prog, strerror(errno)); 127 return e; 128 } 129 if (magic_load(ms, NULL) == -1) { 130 (void)fprintf(stderr, "%s: ERROR loading with NULL file: %s\n", 131 prog, magic_error(ms)); 132 goto bad; 133 } 134 135 if ((result = magic_file(ms, argv[0])) == NULL) { 136 (void)fprintf(stderr, "%s: ERROR loading file %s: %s\n", 137 prog, argv[1], magic_error(ms)); 138 goto bad; 139 } 140 fp = fopen(argv[1], "r"); 141 if (fp == NULL) { 142 (void)fprintf(stderr, "%s: ERROR opening `%s': %s", 143 prog, argv[1], strerror(errno)); 144 goto bad; 145 } 146 desired = slurp(fp, &desired_len); 147 fclose(fp); 148 (void)printf("%s: %s\n", argv[0], result); 149 if (strcmp(result, desired) != 0) { 150 result_len = strlen(result); 151 (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n" 152 "expected (len %zu)\n%s\n", prog, result_len, result, 153 desired_len, desired); 154 goto bad; 155 } 156 e = 0; 157 bad: 158 free(desired); 159 if (ms) 160 magic_close(ms); 161 return e; 162 } 163