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