1 #define _XOPEN_SOURCE 500
19 /* Print formated string into automtically reallocated @uffer.
20 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
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
) {
27 int length
, new_length
;
30 if (!buffer
|| !format
) {
39 length
= vsnprintf(NULL
, 0, format
, aq
) + 1;
47 new_buffer
= realloc(*buffer
, length
);
55 new_length
= vsnprintf(*buffer
, length
, format
, ap
);
56 if (new_length
>= length
) {
66 /* Print formated string into automtically reallocated @uffer.
67 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
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
, ...) {
76 ret
= test_vasprintf(buffer
, format
, ap
);
83 int test_mmap_file(const char *file
, int *fd
, void **buffer
, size_t *length
) {
84 struct stat file_info
;
87 if (!file
|| !fd
|| !buffer
|| !length
) return -1;
89 *fd
= open(file
, O_RDONLY
);
91 fprintf(stderr
, "%s: Could not open file: %s\n", file
, strerror(errno
));
95 if (-1 == fstat(*fd
, &file_info
)) {
96 fprintf(stderr
, "%s: Could not get file size: %s\n", file
,
101 if (file_info
.st_size
< 0) {
102 fprintf(stderr
, "File `%s' has negative size: %jd\n", file
,
103 (intmax_t) file_info
.st_size
);
107 *length
= file_info
.st_size
;
108 *buffer
= malloc(*length
);
111 fprintf(stderr
, "%s: Could not allocate memory for file mapping: %s\n",
112 file
, strerror(errno
));
118 ret
= read(*fd
, *buffer
+ pos
, *length
- pos
);
123 } while ((ret
< 0 && errno
== EINTR
) || (ret
> 0 && pos
< *length
));
126 fprintf(stderr
, "%s: Could not map file to memory: %s\n", file
,
137 int test_munmap_file(int fd
, void *buffer
, size_t length
) {
143 fprintf(stderr
, "Could close file descriptor %d: %s\n", fd
,
150 int test_mmap_file(const char *file
, int *fd
, void **buffer
, size_t *length
) {
151 struct stat file_info
;
153 if (!file
|| !fd
|| !buffer
|| !length
) return -1;
156 *fd
= open(file
, O_RDONLY
);
158 fprintf(stderr
, "%s: Could not open file: %s\n", file
, strerror(errno
));
162 if (-1 == fstat(*fd
, &file_info
)) {
163 fprintf(stderr
, "%s: Could not get file size: %s\n", file
,
168 if (file_info
.st_size
< 0) {
169 fprintf(stderr
, "File `%s' has negative size: %jd\n", file
,
170 (intmax_t) file_info
.st_size
);
174 *length
= file_info
.st_size
;
176 *buffer
= mmap(NULL
, *length
, PROT_READ
, MAP_PRIVATE
, *fd
, 0);
177 if (*buffer
== MAP_FAILED
) {
178 fprintf(stderr
, "%s: Could not map file to memory: %s\n", file
,
188 int test_munmap_file(int fd
, void *buffer
, size_t length
) {
190 long int page_size
= sysconf(_SC_PAGE_SIZE
);
191 size_t pages
= (length
% page_size
) ?
192 ((length
/ page_size
) + 1) * page_size
:
195 err
= munmap(buffer
, pages
);
197 fprintf(stderr
, "Could not unmap memory at %p and length %zu: %s\n",
198 buffer
, pages
, strerror(errno
));
203 fprintf(stderr
, "Could close file descriptor %d: %s\n", fd
,