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