1186690Sobrien/*
2186690Sobrien * Copyright (c) Christos Zoulas 2003.
3186690Sobrien * All Rights Reserved.
4186690Sobrien *
5186690Sobrien * Redistribution and use in source and binary forms, with or without
6186690Sobrien * modification, are permitted provided that the following conditions
7186690Sobrien * are met:
8186690Sobrien * 1. Redistributions of source code must retain the above copyright
9186690Sobrien *    notice immediately at the beginning of the file, without modification,
10186690Sobrien *    this list of conditions, and the following disclaimer.
11186690Sobrien * 2. Redistributions in binary form must reproduce the above copyright
12186690Sobrien *    notice, this list of conditions and the following disclaimer in the
13186690Sobrien *    documentation and/or other materials provided with the distribution.
14186690Sobrien *
15186690Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16186690Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17186690Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18186690Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19186690Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20186690Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21186690Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22186690Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23186690Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24186690Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25186690Sobrien * SUCH DAMAGE.
26186690Sobrien */
27186690Sobrien
28186690Sobrien#include <stdio.h>
29186690Sobrien#include <stdlib.h>
30186690Sobrien#include <string.h>
31186690Sobrien#include "magic.h"
32186690Sobrien
33186690Sobrienstatic void *
34186690Sobrienxrealloc(void *p, size_t n)
35186690Sobrien{
36186690Sobrien	p = realloc(p, n);
37186690Sobrien	if (p == NULL) {
38186690Sobrien		(void)fprintf(stderr, "ERROR slurping file: out of memory\n");
39186690Sobrien		exit(10);
40186690Sobrien	}
41186690Sobrien	return p;
42186690Sobrien}
43186690Sobrien
44186690Sobrienstatic char *
45186690Sobrienslurp(FILE *fp, size_t *final_len)
46186690Sobrien{
47186690Sobrien	size_t len = 256;
48186690Sobrien	int c;
49186690Sobrien	char *l = (char *)xrealloc(NULL, len), *s = l;
50186690Sobrien
51186690Sobrien	for (c = getc(fp); c != EOF; c = getc(fp)) {
52186690Sobrien		if (s == l + len) {
53186690Sobrien			l = (char *)xrealloc(l, len * 2);
54186690Sobrien			len *= 2;
55186690Sobrien		}
56186690Sobrien		*s++ = c;
57186690Sobrien	}
58186690Sobrien	if (s == l + len)
59186690Sobrien		l = (char *)xrealloc(l, len + 1);
60186690Sobrien	*s++ = '\0';
61186690Sobrien
62186690Sobrien	*final_len = s - l;
63186690Sobrien	l = (char *)xrealloc(l, s - l);
64186690Sobrien	return l;
65186690Sobrien}
66186690Sobrien
67186690Sobrienint
68186690Sobrienmain(int argc, char **argv)
69186690Sobrien{
70186690Sobrien	struct magic_set *ms;
71186690Sobrien	const char *result;
72186690Sobrien	char *desired;
73186690Sobrien	size_t desired_len;
74186690Sobrien	int i;
75186690Sobrien	FILE *fp;
76186690Sobrien
77186690Sobrien	ms = magic_open(MAGIC_NONE);
78186690Sobrien	if (ms == NULL) {
79186690Sobrien		(void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory\n");
80186690Sobrien		return 10;
81186690Sobrien	}
82186690Sobrien	if (magic_load(ms, NULL) == -1) {
83186690Sobrien		(void)fprintf(stderr, "ERROR loading with NULL file: %s\n", magic_error(ms));
84186690Sobrien		return 11;
85186690Sobrien	}
86186690Sobrien
87186690Sobrien	if (argc > 1) {
88186690Sobrien		if (argc != 3) {
89186690Sobrien			(void)fprintf(stderr, "Usage: test TEST-FILE RESULT\n");
90186690Sobrien		} else {
91186690Sobrien			if ((result = magic_file(ms, argv[1])) == NULL) {
92186690Sobrien				(void)fprintf(stderr, "ERROR loading file %s: %s\n", argv[1], magic_error(ms));
93186690Sobrien				return 12;
94186690Sobrien			} else {
95186690Sobrien				fp = fopen(argv[2], "r");
96186690Sobrien				if (fp == NULL) {
97186690Sobrien					(void)fprintf(stderr, "ERROR opening `%s': ", argv[2]);
98186690Sobrien					perror(NULL);
99186690Sobrien					return 13;
100186690Sobrien				}
101186690Sobrien				desired = slurp(fp, &desired_len);
102186690Sobrien				fclose(fp);
103186690Sobrien				(void)printf("%s: %s\n", argv[1], result);
104186690Sobrien                                if (strcmp(result, desired) != 0) {
105186690Sobrien					(void)fprintf(stderr, "Error: result was\n%s\nexpected:\n%s\n", result, desired);
106186690Sobrien					return 1;
107186690Sobrien                                }
108186690Sobrien			}
109186690Sobrien		}
110186690Sobrien	}
111186690Sobrien
112186690Sobrien	magic_close(ms);
113186690Sobrien	return 0;
114186690Sobrien}
115