91905a60613de282d9cc1cbaee96d3528fe8b745
[monky] / src / conf_cookie.c
1 #define _GNU_SOURCE
2 #include "config.h"
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include "defconfig.h"
6
7 #if defined(HAVE_FOPENCOOKIE)
8 #define COOKIE_LEN_T    size_t
9 #define COOKIE_RET_T    ssize_t
10 #else
11 #define COOKIE_LEN_T    int
12 #define COOKIE_RET_T    int
13 #endif
14
15 static COOKIE_RET_T
16 conf_read(void *cookie, char *buf, COOKIE_LEN_T size)
17 {
18         static int col = 0, row = 0;
19         COOKIE_LEN_T i = 0;
20         const char *conf[] = defconfig;
21
22         (void)cookie;
23
24         while (i < size) {
25                 if (!(conf[row]))               /* end of rows */
26                         break;
27                 if (!(conf[row][col])) {        /* end of line */
28                         row++;
29                         col = 0;
30                         continue;
31                 }
32                 buf[i++] = conf[row][col++];
33         }
34         return i;
35 }
36
37 #if defined(HAVE_FOPENCOOKIE)
38 static cookie_io_functions_t conf_cookie = {
39         .read = &conf_read,
40         .write = NULL,
41         .seek = NULL,
42         .close = NULL,
43 };
44 FILE *conf_cookie_open(void)
45 {
46         return fopencookie(NULL, "r", conf_cookie);
47 }
48 #elif defined(HAVE_FUNOPEN)
49 FILE *conf_cookie_open(void)
50 {
51         return funopen(NULL, &conf_read, NULL, NULL, NULL);
52 }
53 #else
54 FILE *conf_cookie_open(void) { return NULL; }
55 #endif