Fix segfault in mail folder (SF: 3007493)
[monky] / src / smapi.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * smapi.c:  conky support for IBM Thinkpad smapi
5  *
6  * Copyright (C) 2007 Phil Sutter <Phil@nwl.cc>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21  * USA.
22  *
23  */
24 #define _GNU_SOURCE
25 #include "conky.h"      /* text_buffer_size, PACKAGE_NAME, maybe more */
26 #include "smapi.h"
27 #include "temphelper.h"
28 #include "logging.h"
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #define SYS_SMAPI_PATH "/sys/devices/platform/smapi"
36
37 int smapi_bat_installed(int idx)
38 {
39         char path[128];
40         struct stat sb;
41         int ret = 0;
42
43         snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i", idx);
44         if (!stat(path, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR) {
45                 snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/installed", idx);
46                 ret = (smapi_read_int(path) == 1) ? 1 : 0;
47         }
48         return ret;
49
50 }
51
52 char *smapi_read_str(const char *path)
53 {
54         FILE *fp;
55         char str[256] = "failed";
56         if ((fp = fopen(path, "r")) != NULL) {
57                 fscanf(fp, "%255s\n", str);
58                 fclose(fp);
59         }
60         return strndup(str, text_buffer_size);
61 }
62
63 int smapi_read_int(const char *path)
64 {
65         FILE *fp;
66         int i = 0;
67         if ((fp = fopen(path, "r")) != NULL) {
68                 fscanf(fp, "%i\n", &i);
69                 fclose(fp);
70         }
71         return i;
72 }
73
74 char *smapi_get_str(const char *fname)
75 {
76         char path[128];
77         if(snprintf(path, 127, SYS_SMAPI_PATH "/%s", fname) < 0)
78                 return NULL;
79
80         return smapi_read_str(path);
81 }
82
83 char *smapi_get_bat_str(int idx, const char *fname)
84 {
85         char path[128];
86         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
87                 return NULL;
88         return smapi_read_str(path);
89 }
90
91 int smapi_get_bat_int(int idx, const char *fname)
92 {
93         char path[128];
94         if(snprintf(path, 127, SYS_SMAPI_PATH "/BAT%i/%s", idx, fname) < 0)
95                 return 0;
96         return smapi_read_int(path);
97 }
98
99 char *smapi_get_bat_val(const char *args)
100 {
101         char fname[128];
102         int idx, cnt;
103
104         if(sscanf(args, "%i %n", &idx, &cnt) <= 0 ||
105            snprintf(fname, 127, "%s", (args + cnt)) < 0) {
106                 NORM_ERR("smapi: wrong arguments, should be 'bat,<int>,<str>'");
107                 return NULL;
108         }
109
110         if(!smapi_bat_installed(idx))
111                 return NULL;
112
113         return smapi_get_bat_str(idx, fname);
114 }
115
116 char *smapi_get_val(const char *args)
117 {
118         char str[128];
119
120         if(!args || sscanf(args, "%127s", str) <= 0)
121                 return NULL;
122
123         if(!strcmp(str, "bat"))
124                 return smapi_get_bat_val(args + 4);
125
126         return smapi_get_str(str);
127 }
128
129 void print_smapi(struct text_object *obj, char *p, int p_max_size)
130 {
131         char *s;
132
133         if (!obj->data.s)
134                 return;
135
136         s = smapi_get_val(obj->data.s);
137         snprintf(p, p_max_size, "%s", s);
138         free(s);
139 }
140
141 void print_smapi_bat_perc(struct text_object *obj, char *p, int p_max_size)
142 {
143         int idx, val;
144         if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
145                 val = smapi_bat_installed(idx) ?
146                         smapi_get_bat_int(idx, "remaining_percent") : 0;
147                 percent_print(p, p_max_size, val);
148         } else
149                 NORM_ERR("argument to smapi_bat_perc must be an integer");
150 }
151
152 void print_smapi_bat_temp(struct text_object *obj, char *p, int p_max_size)
153 {
154         int idx, val;
155         if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
156                 val = smapi_bat_installed(idx) ?
157                         smapi_get_bat_int(idx, "temperature") : 0;
158                 /* temperature is in milli degree celsius */
159                 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
160         } else
161                 NORM_ERR("argument to smapi_bat_temp must be an integer");
162 }
163
164 void print_smapi_bat_power(struct text_object *obj, char *p, int p_max_size)
165 {
166         int idx, val;
167         if (obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
168                 val = smapi_bat_installed(idx) ?
169                         smapi_get_bat_int(idx, "power_now") : 0;
170                 /* power_now is in mW, set to W with one digit precision */
171                 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
172         } else
173                 NORM_ERR("argument to smapi_bat_power must be an integer");
174 }
175
176 void print_smapi_bat_bar(struct text_object *obj, char *p, int p_max_size)
177 {
178         if (!p_max_size)
179                 return;
180
181         if (obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
182                 new_bar(obj, p, p_max_size, (int)
183                                 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
184         else
185                 new_bar(obj, p, p_max_size, 0);
186 }