Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / file / dist / tests / test.c
blob19370d91e113a16bcc076441bf334094db2cedc8
1 /* $NetBSD$ */
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.
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.
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "magic.h"
35 static void *
36 xrealloc(void *p, size_t n)
38 p = realloc(p, n);
39 if (p == NULL) {
40 (void)fprintf(stderr, "ERROR slurping file: out of memory\n");
41 exit(10);
43 return p;
46 static char *
47 slurp(FILE *fp, size_t *final_len)
49 size_t len = 256;
50 int c;
51 char *l = (char *)xrealloc(NULL, len), *s = l;
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;
58 *s++ = c;
60 if (s == l + len)
61 l = (char *)xrealloc(l, len + 1);
62 *s++ = '\0';
64 *final_len = s - l;
65 l = (char *)xrealloc(l, s - l);
66 return l;
69 int
70 main(int argc, char **argv)
72 struct magic_set *ms;
73 const char *result;
74 char *desired;
75 size_t desired_len;
76 int i;
77 FILE *fp;
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;
84 if (magic_load(ms, NULL) == -1) {
85 (void)fprintf(stderr, "ERROR loading with NULL file: %s\n", magic_error(ms));
86 return 11;
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;
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;
114 magic_close(ms);
115 return 0;