Ported to MinGW
[libisds.git] / test / test.c
blobdcf0daccaaf2f59ed5d595e13c318deae179e888
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdarg.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
10 #ifndef _WIN32
11 #include <sys/mman.h>
12 #endif
14 #include <errno.h>
15 #include <string.h>
16 #include <unistd.h>
19 /* Print formated string into automtically reallocated @uffer.
20 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
21 * memory.
22 * @format format string as for printf(3)
23 * @ap list of variadic arguments, after call will be in udefined state
24 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer */
25 int test_vasprintf(char **buffer, const char *format, va_list ap) {
26 va_list aq;
27 int length, new_length;
28 char *new_buffer;
30 if (!buffer || !format) {
31 if (buffer) {
32 free(*buffer);
33 *buffer = NULL;
35 return -1;
38 va_copy(aq, ap);
39 length = vsnprintf(NULL, 0, format, aq) + 1;
40 va_end(aq);
41 if (length <= 0) {
42 free(*buffer);
43 *buffer = NULL;
44 return -1;
47 new_buffer = realloc(*buffer, length);
48 if (!new_buffer) {
49 free(*buffer);
50 *buffer = NULL;
51 return -1;
53 *buffer = new_buffer;
55 new_length = vsnprintf(*buffer, length, format, ap);
56 if (new_length >= length) {
57 free(*buffer);
58 *buffer = NULL;
59 return -1;
62 return new_length;
66 /* Print formated string into automtically reallocated @uffer.
67 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
68 * memory.
69 * @format format string as for printf(3)
70 * @... variadic arguments
71 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer */
72 int test_asprintf(char **buffer, const char *format, ...) {
73 int ret;
74 va_list ap;
75 va_start(ap, format);
76 ret = test_vasprintf(buffer, format, ap);
77 va_end(ap);
78 return ret;
82 #ifdef _WIN32
83 int test_mmap_file(const char *file, int *fd, void **buffer, size_t *length) {
84 struct stat file_info;
86 if (!file || !fd || !buffer || !length) return -1;
88 *fd = open(file, O_RDONLY);
89 if (*fd == -1) {
90 fprintf(stderr, "%s: Could not open file: %s\n", file, strerror(errno));
91 return -1;
94 if (-1 == fstat(*fd, &file_info)) {
95 fprintf(stderr, "%s: Could not get file size: %s\n", file,
96 strerror(errno));
97 close(*fd);
98 return -1;
100 if (file_info.st_size < 0) {
101 fprintf(stderr, "File `%s' has negative size: %jd\n", file,
102 (intmax_t) file_info.st_size);
103 close(*fd);
104 return -1;
106 *length = file_info.st_size;
107 *buffer = malloc(*length);
109 if (!*buffer) {
110 fprintf(stderr, "%s: Could not allocate memory for file mapping: %s\n",
111 file, strerror(errno));
112 close(*fd);
113 return -1;
116 read(*fd, *buffer, *length);
118 return 0;
121 int test_munmap_file(int fd, void *buffer, size_t length) {
122 int err = 0;
123 free(buffer);
125 err = close(fd);
126 if (err) {
127 fprintf(stderr, "Could close file descriptor %d: %s\n", fd,
128 strerror(errno));
131 return err;
133 #else
134 int test_mmap_file(const char *file, int *fd, void **buffer, size_t *length) {
135 struct stat file_info;
137 if (!file || !fd || !buffer || !length) return -1;
140 *fd = open(file, O_RDONLY);
141 if (*fd == -1) {
142 fprintf(stderr, "%s: Could not open file: %s\n", file, strerror(errno));
143 return -1;
146 if (-1 == fstat(*fd, &file_info)) {
147 fprintf(stderr, "%s: Could not get file size: %s\n", file,
148 strerror(errno));
149 close(*fd);
150 return -1;
152 if (file_info.st_size < 0) {
153 fprintf(stderr, "File `%s' has negative size: %jd\n", file,
154 (intmax_t) file_info.st_size);
155 close(*fd);
156 return -1;
158 *length = file_info.st_size;
160 *buffer = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, *fd, 0);
161 if (*buffer == MAP_FAILED) {
162 fprintf(stderr, "%s: Could not map file to memory: %s\n", file,
163 strerror(errno));
164 close(*fd);
165 return -1;
168 return 0;
172 int test_munmap_file(int fd, void *buffer, size_t length) {
173 int err = 0;
174 long int page_size = sysconf(_SC_PAGE_SIZE);
175 size_t pages = (length % page_size) ?
176 ((length / page_size) + 1) * page_size:
177 length;
179 err = munmap(buffer, pages);
180 if (err) {
181 fprintf(stderr, "Could not unmap memory at %p and length %zu: %s\n",
182 buffer, pages, strerror(errno));
185 err = close(fd);
186 if (err) {
187 fprintf(stderr, "Could close file descriptor %d: %s\n", fd,
188 strerror(errno));
191 return err;
193 #endif