xref: /minix3/external/bsd/file/dist/tests/test.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: test.c,v 1.1.1.3 2015/01/02 20:34:28 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Christos Zoulas 2003.
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "magic.h"
34 
35 static void *
xrealloc(void * p,size_t n)36 xrealloc(void *p, size_t n)
37 {
38 	p = realloc(p, n);
39 	if (p == NULL) {
40 		(void)fprintf(stderr, "ERROR slurping file: out of memory\n");
41 		exit(10);
42 	}
43 	return p;
44 }
45 
46 static char *
slurp(FILE * fp,size_t * final_len)47 slurp(FILE *fp, size_t *final_len)
48 {
49 	size_t len = 256;
50 	int c;
51 	char *l = (char *)xrealloc(NULL, len), *s = l;
52 
53 	for (c = getc(fp); c != EOF; c = getc(fp)) {
54 		if (s == l + len) {
55 			l = (char *)xrealloc(l, len * 2);
56 			len *= 2;
57 		}
58 		*s++ = c;
59 	}
60 	if (s == l + len)
61 		l = (char *)xrealloc(l, len + 1);
62 	*s++ = '\0';
63 
64 	*final_len = s - l;
65 	l = (char *)xrealloc(l, s - l);
66 	return l;
67 }
68 
69 int
main(int argc,char ** argv)70 main(int argc, char **argv)
71 {
72 	struct magic_set *ms;
73 	const char *result;
74 	char *desired;
75 	size_t desired_len;
76 	int i;
77 	FILE *fp;
78 
79 	ms = magic_open(MAGIC_NONE);
80 	if (ms == NULL) {
81 		(void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory\n");
82 		return 10;
83 	}
84 	if (magic_load(ms, NULL) == -1) {
85 		(void)fprintf(stderr, "ERROR loading with NULL file: %s\n", magic_error(ms));
86 		return 11;
87 	}
88 
89 	if (argc > 1) {
90 		if (argc != 3) {
91 			(void)fprintf(stderr, "Usage: test TEST-FILE RESULT\n");
92 		} else {
93 			if ((result = magic_file(ms, argv[1])) == NULL) {
94 				(void)fprintf(stderr, "ERROR loading file %s: %s\n", argv[1], magic_error(ms));
95 				return 12;
96 			} else {
97 				fp = fopen(argv[2], "r");
98 				if (fp == NULL) {
99 					(void)fprintf(stderr, "ERROR opening `%s': ", argv[2]);
100 					perror(NULL);
101 					return 13;
102 				}
103 				desired = slurp(fp, &desired_len);
104 				fclose(fp);
105 				(void)printf("%s: %s\n", argv[1], result);
106                                 if (strcmp(result, desired) != 0) {
107 					(void)fprintf(stderr, "Error: result was\n%s\nexpected:\n%s\n", result, desired);
108 					return 1;
109                                 }
110 			}
111 		}
112 	}
113 
114 	magic_close(ms);
115 	return 0;
116 }
117