Removing old svn keywords.
[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  */
21
22 #include "smapi.h"
23
24 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
25
26 int smapi_bat_installed(int idx)
27 {
28         char path[128];
29         struct stat sb;
30         int ret = 0;
31
32         snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
33         if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
34                 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
35                 ret = (smapi_read_int(path) == 1) ? 1 : 0;
36         }
37         return ret;
38
39 }
40
41 char *smapi_read_str(const char *path)
42 {
43         FILE *fp;
44         char str[256] = "failed";
45         if ((fp = fopen(path, "r")) != NULL) {
46                 fscanf(fp, "%255s\n", str);
47                 fclose(fp);
48         }
49         return strndup(str, text_buffer_size);
50 }
51
52 int smapi_read_int(const char *path)
53 {
54         FILE *fp;
55         int i = 0;
56         if ((fp = fopen(path, "r")) != NULL) {
57                 fscanf(fp, "%i\n", &i);
58                 fclose(fp);
59         }
60         return i;
61 }
62
63 char *smapi_get_str(const char *fname)
64 {
65         char path[128];
66         if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
67                 return NULL;
68
69         return smapi_read_str(path);
70 }
71
72 char *smapi_get_bat_str(int idx, const char *fname)
73 {
74         char path[128];
75         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
76                 return NULL;
77         return smapi_read_str(path);
78 }
79
80 int smapi_get_bat_int(int idx, const char *fname)
81 {
82         char path[128];
83         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
84                 return 0;
85         return smapi_read_int(path);
86 }
87
88 char *smapi_get_bat_val(const char *args)
89 {
90         char fname[128];
91         int idx, cnt;
92
93         if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
94            snprintf(fname, 127, "%s", (args + cnt)) < 0) {
95                 ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
96                 return NULL;
97         }
98
99         if(!smapi_bat_installed(idx))
100                 return NULL;
101
102         return smapi_get_bat_str(idx, fname);
103 }
104
105 char *smapi_get_val(const char *args)
106 {
107         char str[128];
108
109         if(!args || sscanf(args, "%127s", str) <= 0)
110                 return NULL;
111
112         if(!strcmp(str, "bat"))
113                 return smapi_get_bat_val(args + 4);
114
115         return smapi_get_str(str);
116 }