7d96a471454c371dedd14231f8344bcfb7a5901a
[monky] / src / conf_cookie.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Please see COPYING for details
6  *
7  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
8  *      (see AUTHORS)
9  * All rights reserved.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  * vim: ts=4 sw=4 noet ai cindent syntax=c
24  *
25  */
26
27 #define _GNU_SOURCE
28 #include "config.h"
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include "defconfig.h"
32
33 #if defined(HAVE_FOPENCOOKIE)
34 #define COOKIE_LEN_T    size_t
35 #define COOKIE_RET_T    ssize_t
36 #else
37 #define COOKIE_LEN_T    int
38 #define COOKIE_RET_T    int
39 #endif
40
41 static COOKIE_RET_T
42 conf_read(void *cookie, char *buf, COOKIE_LEN_T size)
43 {
44         static int col = 0, row = 0;
45         COOKIE_LEN_T i = 0;
46         const char *conf[] = defconfig;
47
48         (void)cookie;
49
50         while (i < size) {
51                 if (!(conf[row]))               /* end of rows */
52                         break;
53                 if (!(conf[row][col])) {        /* end of line */
54                         row++;
55                         col = 0;
56                         continue;
57                 }
58                 buf[i++] = conf[row][col++];
59         }
60         return i;
61 }
62
63 #if defined(HAVE_FOPENCOOKIE)
64 static cookie_io_functions_t conf_cookie = {
65         .read = &conf_read,
66         .write = NULL,
67         .seek = NULL,
68         .close = NULL,
69 };
70 FILE *conf_cookie_open(void)
71 {
72         return fopencookie(NULL, "r", conf_cookie);
73 }
74 #elif defined(HAVE_FUNOPEN)
75 FILE *conf_cookie_open(void)
76 {
77         return funopen(NULL, &conf_read, NULL, NULL, NULL);
78 }
79 #else
80 FILE *conf_cookie_open(void) { return NULL; }
81 #endif