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 <string.h> 31 #include <errno.h> 32 33 #include "magic.h" 34 35 static const char *prog; 36 37 static void * 38 xrealloc(void *p, size_t n) 39 { 40 p = realloc(p, n); 41 if (p == NULL) { 42 (void)fprintf(stderr, "%s ERROR slurping file: %s\n", 43 prog, strerror(errno)); 44 exit(10); 45 } 46 return p; 47 } 48 49 static char * 50 slurp(FILE *fp, size_t *final_len) 51 { 52 size_t len = 256; 53 int c; 54 char *l = (char *)xrealloc(NULL, len), *s = l; 55 56 for (c = getc(fp); c != EOF; c = getc(fp)) { 57 if (s == l + len) { 58 l = xrealloc(l, len * 2); 59 len *= 2; 60 } 61 *s++ = c; 62 } 63 if (s == l + len) 64 l = (char *)xrealloc(l, len + 1); 65 *s++ = '\0'; 66 67 *final_len = s - l; 68 l = (char *)xrealloc(l, s - l); 69 return l; 70 } 71 72 int 73 main(int argc, char **argv) 74 { 75 struct magic_set *ms; 76 const char *result; 77 size_t result_len, desired_len; 78 char *desired = NULL; 79 int e = EXIT_FAILURE; 80 FILE *fp; 81 82 83 prog = strrchr(argv[0], '/'); 84 if (prog) 85 prog++; 86 else 87 prog = argv[0]; 88 89 ms = magic_open(MAGIC_ERROR); 90 if (ms == NULL) { 91 (void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n", 92 prog, strerror(errno)); 93 return e; 94 } 95 if (magic_load(ms, NULL) == -1) { 96 (void)fprintf(stderr, "%s: ERROR loading with NULL file: %s\n", 97 prog, magic_error(ms)); 98 goto bad; 99 } 100 101 if (argc == 1) { 102 e = 0; 103 goto bad; 104 } 105 106 if (argc != 3) { 107 (void)fprintf(stderr, "Usage: %s TEST-FILE RESULT\n", prog); 108 goto bad; 109 } 110 if ((result = magic_file(ms, argv[1])) == NULL) { 111 (void)fprintf(stderr, "%s: ERROR loading file %s: %s\n", 112 prog, argv[1], magic_error(ms)); 113 goto bad; 114 } 115 fp = fopen(argv[2], "r"); 116 if (fp == NULL) { 117 (void)fprintf(stderr, "%s: ERROR opening `%s': %s", 118 prog, argv[2], strerror(errno)); 119 goto bad; 120 } 121 desired = slurp(fp, &desired_len); 122 fclose(fp); 123 (void)printf("%s: %s\n", argv[1], result); 124 if (strcmp(result, desired) != 0) { 125 result_len = strlen(result); 126 (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n" 127 "expected (len %zu)\n%s\n", prog, result_len, result, 128 desired_len, desired); 129 goto bad; 130 } 131 e = 0; 132 bad: 133 free(desired); 134 magic_close(ms); 135 return e; 136 } 137