1 #define _XOPEN_SOURCE 500
19 /* Global variables for each test unit */
21 void (*test_destructor_function
)(void *) = NULL
;
23 /* Print formated string into automtically reallocated @uffer.
24 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
26 * @format format string as for printf(3)
27 * @ap list of variadic arguments, after call will be in udefined state
28 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer */
29 int test_vasprintf(char **buffer
, const char *format
, va_list ap
) {
31 int length
, new_length
;
34 if (!buffer
|| !format
) {
43 length
= vsnprintf(NULL
, 0, format
, aq
) + 1;
51 new_buffer
= realloc(*buffer
, length
);
59 new_length
= vsnprintf(*buffer
, length
, format
, ap
);
60 if (new_length
>= length
) {
70 /* Print formated string into automtically reallocated @uffer.
71 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
73 * @format format string as for printf(3)
74 * @... variadic arguments
75 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer */
76 int test_asprintf(char **buffer
, const char *format
, ...) {
80 ret
= test_vasprintf(buffer
, format
, ap
);
87 int test_mmap_file(const char *file
, int *fd
, void **buffer
, size_t *length
) {
88 struct stat file_info
;
91 if (!file
|| !fd
|| !buffer
|| !length
) return -1;
93 *fd
= open(file
, O_RDONLY
);
95 fprintf(stderr
, "%s: Could not open file: %s\n", file
, strerror(errno
));
99 if (-1 == fstat(*fd
, &file_info
)) {
100 fprintf(stderr
, "%s: Could not get file size: %s\n", file
,
105 if (file_info
.st_size
< 0) {
106 fprintf(stderr
, "File `%s' has negative size: %" PRIdMAX
"\n", file
,
107 (intmax_t) file_info
.st_size
);
111 *length
= file_info
.st_size
;
112 *buffer
= malloc(*length
);
115 fprintf(stderr
, "%s: Could not allocate memory for file mapping: %s\n",
116 file
, strerror(errno
));
122 ret
= read(*fd
, *buffer
+ pos
, *length
- pos
);
127 } while ((ret
< 0 && errno
== EINTR
) || (ret
> 0 && pos
< *length
));
130 fprintf(stderr
, "%s: Could not map file to memory: %s\n", file
,
141 int test_munmap_file(int fd
, void *buffer
, size_t length
) {
147 fprintf(stderr
, "Could close file descriptor %d: %s\n", fd
,
154 int test_mmap_file(const char *file
, int *fd
, void **buffer
, size_t *length
) {
155 struct stat file_info
;
157 if (!file
|| !fd
|| !buffer
|| !length
) return -1;
160 *fd
= open(file
, O_RDONLY
);
162 fprintf(stderr
, "%s: Could not open file: %s\n", file
, strerror(errno
));
166 if (-1 == fstat(*fd
, &file_info
)) {
167 fprintf(stderr
, "%s: Could not get file size: %s\n", file
,
172 if (file_info
.st_size
< 0) {
173 fprintf(stderr
, "File `%s' has negative size: %" PRIdMAX
"\n", file
,
174 (intmax_t) file_info
.st_size
);
178 *length
= file_info
.st_size
;
180 *buffer
= mmap(NULL
, *length
, PROT_READ
, MAP_PRIVATE
, *fd
, 0);
181 if (*buffer
== MAP_FAILED
) {
182 fprintf(stderr
, "%s: Could not map file to memory: %s\n", file
,
192 int test_munmap_file(int fd
, void *buffer
, size_t length
) {
194 long int page_size
= sysconf(_SC_PAGE_SIZE
);
195 size_t pages
= (length
% page_size
) ?
196 ((length
/ page_size
) + 1) * page_size
:
199 err
= munmap(buffer
, pages
);
201 fprintf(stderr
, "Could not unmap memory at %p and length %zu: %s\n",
202 buffer
, pages
, strerror(errno
));
207 fprintf(stderr
, "Could close file descriptor %d: %s\n", fd
,