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