4 * Copyright (c) Christos Zoulas 2003.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 xrealloc(void *p
, size_t n
)
40 (void)fprintf(stderr
, "ERROR slurping file: out of memory\n");
47 slurp(FILE *fp
, size_t *final_len
)
51 char *l
= (char *)xrealloc(NULL
, len
), *s
= l
;
53 for (c
= getc(fp
); c
!= EOF
; c
= getc(fp
)) {
55 l
= (char *)xrealloc(l
, len
* 2);
61 l
= (char *)xrealloc(l
, len
+ 1);
65 l
= (char *)xrealloc(l
, s
- l
);
70 main(int argc
, char **argv
)
79 ms
= magic_open(MAGIC_NONE
);
81 (void)fprintf(stderr
, "ERROR opening MAGIC_NONE: out of memory\n");
84 if (magic_load(ms
, NULL
) == -1) {
85 (void)fprintf(stderr
, "ERROR loading with NULL file: %s\n", magic_error(ms
));
91 (void)fprintf(stderr
, "Usage: test TEST-FILE RESULT\n");
93 if ((result
= magic_file(ms
, argv
[1])) == NULL
) {
94 (void)fprintf(stderr
, "ERROR loading file %s: %s\n", argv
[1], magic_error(ms
));
97 fp
= fopen(argv
[2], "r");
99 (void)fprintf(stderr
, "ERROR opening `%s': ", argv
[2]);
103 desired
= slurp(fp
, &desired_len
);
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
);