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