Split conky.h into several smaller header files
[monky] / src / smapi.c
1 /* smapi.c:  conky support for IBM Thinkpad smapi
2  *
3  * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
4  * 
5  * This library is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
18  * USA.
19  *
20  * $Id$
21  *
22  */
23
24 #include "smapi.h"
25
26 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
27
28 int smapi_bat_installed(int idx)
29 {
30         char path[128];
31         struct stat sb;
32         int ret = 0;
33
34         snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
35         if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
36                 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
37                 ret = (smapi_read_int(path) == 1) ? 1 : 0;
38         }
39         return ret;
40
41 }
42
43 char *smapi_read_str(const char *path)
44 {
45         FILE *fp;
46         char str[256] = "failed";
47         if ((fp = fopen(path, "r")) != NULL) {
48                 fscanf(fp, "%255s\n", str);
49                 fclose(fp);
50         }
51         return strndup(str, text_buffer_size);
52 }
53
54 int smapi_read_int(const char *path)
55 {
56         FILE *fp;
57         int i = 0;
58         if ((fp = fopen(path, "r")) != NULL) {
59                 fscanf(fp, "%i\n", &i);
60                 fclose(fp);
61         }
62         return i;
63 }
64
65 char *smapi_get_str(const char *fname)
66 {
67         char path[128];
68         if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
69                 return NULL;
70
71         return smapi_read_str(path);
72 }
73
74 char *smapi_get_bat_str(int idx, const char *fname)
75 {
76         char path[128];
77         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
78                 return NULL;
79         return smapi_read_str(path);
80 }
81
82 int smapi_get_bat_int(int idx, const char *fname)
83 {
84         char path[128];
85         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
86                 return 0;
87         return smapi_read_int(path);
88 }
89
90 char *smapi_get_bat_val(const char *args)
91 {
92         char fname[128];
93         int idx, cnt;
94
95         if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
96            snprintf(fname, 127, "%s", (args + cnt)) < 0) {
97                 ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
98                 return NULL;
99         }
100
101         if(!smapi_bat_installed(idx))
102                 return NULL;
103
104         return smapi_get_bat_str(idx, fname);
105 }
106
107 char *smapi_get_val(const char *args)
108 {
109         char str[128];
110
111         if(!args || sscanf(args, "%127s", str) <= 0)
112                 return NULL;
113
114         if(!strcmp(str, "bat"))
115                 return smapi_get_bat_val(args + 4);
116
117         return smapi_get_str(str);
118 }