2 * Copyright (c) Christos Zoulas 2021.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/types.h>
45 void *(*orig)(size_t) = dlsym(RTLD_NEXT, "malloc");
46 void *p = (*orig)(len);
47 int l = snprintf(buf, sizeof(buf), "malloc %zu %p\n", len, p);
56 void (*orig)(void *) = dlsym(RTLD_NEXT, "free");
58 int l = snprintf(buf, sizeof(buf), "free %p\n", p);
63 calloc(size_t len, size_t nitems)
66 void *(*orig)(size_t, size_t) = dlsym(RTLD_NEXT, "calloc");
67 void *p = (*orig)(len, nitems);
68 size_t tot = len * nitems;
69 int l = snprintf(buf, sizeof(buf), "calloc %zu %p\n", tot, p);
74 realloc(void *q, size_t len)
77 void *(*orig)(void *, size_t) = dlsym(RTLD_NEXT, "realloc");
78 void *p = (*orig)(q, len);
79 int l = snprintf(buf, sizeof(buf), "realloc %zu %p\n", len, p);
87 fprintf(stderr, "Usage: test [-b] <filename>\n");
92 main(int argc, char *argv[])
97 while ((c = getopt(argc, argv, "b")) != -1)
112 magic_t m = magic_open(0);
114 err(EXIT_FAILURE, "magic_open");
120 int fd = open(argv[0], O_RDONLY);
122 err(EXIT_FAILURE, "Cannot open `%s'", argv[0]);
125 if (fstat(fd, &st) == -1)
126 err(EXIT_FAILURE, "Cannot stat `%s'", argv[0]);
127 size_t l = (size_t)st.st_size;
128 void *p = mmap(NULL, l, PROT_READ, MAP_FILE | MAP_PRIVATE, fd,
131 err(EXIT_FAILURE, "Cannot map `%s'", argv[0]);
133 r = magic_buffer(m, p, l);
136 r = magic_file(m, argv[0]);
140 printf("%s\n", r ? r : "(null)");