wmclockmon: properly fix allocating and freeing of `command`, `light_color` and the...
[dockapps.git] / wmclockmon / wmclockmon-config / tools.c
blob42e5c502cabe5f15e0709fa2a1b3e3c13ad3a03c
1 /*
2 * Tools : memory management, file loading and saving
3 */
5 #include "../config.h"
6 #include "defines.h"
7 #include "variables.h"
8 #include "tools.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <sys/types.h>
14 #include <ctype.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include "configfile.h"
20 int time_mode;
21 char *msgcmd;
23 void alrm_add(Alarm **list, const char *value) {
24 Alarm *lst = *list;
25 int ok = TRUE;
26 char *time = NULL, *date = NULL, *ison = NULL, *mesg = NULL, *at;
27 char *tokstr;
28 char *toksav;
30 if (! value) return;
31 if (! lst) {
32 lst = xmalloc(sizeof(Alarm));
33 *list = lst;
34 } else {
35 if (strcmp(value, lst->entry) == 0) ok = FALSE;
36 while ( (lst->next) && ok) {
37 lst = lst->next;
38 if (strcmp(value, lst->entry) == 0) ok = FALSE;
40 if (! ok) return;
41 lst->next = xmalloc(sizeof(Alarm));
42 lst = lst->next;
44 toksav = tokstr = xstrdup(value);
45 at = strchr(value, '@');
46 if (at) ison = strtok(tokstr, "@");
47 time = strtok(at ? NULL : tokstr, "-.");
48 if (strchr(value, '-')) date = strtok(NULL, ".");
49 mesg = strtok(NULL, "\n\0");
50 lst->entry = xstrdup(value);
51 lst->time = time ? xstrdup(time) : NULL;
52 lst->date = date ? xstrdup(date) : NULL;
53 lst->on = ison ? getbool(ison) : TRUE;
54 lst->msg = mesg ? xstrdup(mesg) : NULL;
55 lst->next = NULL;
56 FREE(toksav);
60 void free_alrm(Alarm **list) {
61 Alarm *lst = *list, *next;
62 while (lst) {
63 next = lst->next;
64 FREE(lst->entry);
65 FREE(lst->time);
66 FREE(lst->date);
67 FREE(lst->msg);
68 free(lst);
69 lst = next;
71 *list = NULL;
75 void *xmalloc(size_t size) {
76 void *ret = malloc(size);
77 if (ret == NULL) {
78 perror("malloc() ");
79 exit(-1);
80 } else
81 return ret;
85 char *xstrdup(const char *string) {
86 char *ret = strdup(string);
87 if (ret == NULL) {
88 perror("strdup() ");
89 exit(-1);
90 } else
91 return ret;
95 int getbool(const char *value) {
96 if (strcmp(value, "0") == 0) return FALSE;
97 if (strcmp(value, "1") == 0) return TRUE;
98 if (strcasecmp(value, "true") == 0) return TRUE;
99 if (strcasecmp(value, "false") == 0) return FALSE;
100 if (strcasecmp(value, "yes") == 0) return TRUE;
101 if (strcasecmp(value, "no") == 0) return FALSE;
102 if (strcasecmp(value, "on") == 0) return TRUE;
103 if (strcasecmp(value, "off") == 0) return FALSE;
104 printf("Error in converting \"%s\" to boolean value.\n", value);
105 return FALSE;
109 void load_cfgfile() {
110 FILE *file;
111 int i = 0;
112 char line[MAXSTRLEN + 1];
113 char *value;
115 if ((file = fopen(config_file, "r")) == NULL) {
116 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
117 printf("Unable to open configuration file \"%s\".\n", config_file);
118 return;
120 while (! feof(file)) {
121 memset(line, 0, MAXSTRLEN + 1);
122 fgets(line, MAXSTRLEN, file);
123 i++;
124 if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = 0;
125 if ((line[0] == '#') || (line[0] == 0)) continue;
126 value = strchr(line, '=') + 1;
127 while ((value[0] == ' ') && (value[0] != 0)) value++;
128 if (value[0] == 0) continue;
130 if (strncmp(line, "Backlight", 9) == 0)
131 backlight = getbool(value);
132 else if (strncmp(line, "Color", 5) == 0)
133 light_color = xstrdup(value);
134 else if (strncmp(line, "Alarm", 5) == 0)
135 alrm_add(&alarms, value);
136 else if (strncmp(line, "Command", 7) == 0)
137 command = xstrdup(value);
138 else if (strncmp(line, "MessageCmd", 10) == 0)
139 msgcmd = xstrdup(value);
140 else if (strncmp(line, "Blink", 5) == 0)
141 switch_authorized = getbool(value);
142 else if (strncmp(line, "H12", 3) == 0)
143 h12 = getbool(value);
144 else if (strncmp(line, "Locale", 6) == 0)
145 use_locale = getbool(value);
146 else if (strncmp(line, "StyleDir", 8) == 0)
147 style_dir = xstrdup(value);
148 else if (strncmp(line, "Style", 5) == 0)
149 style_name = xstrdup(value);
150 else if (strncmp(line, "TimeMode", 5) == 0)
151 time_mode = atoi(value);
152 else if (strncmp(line, "ShowCal", 7) == 0)
153 showcal = getbool(value);
154 else if (strncmp(line, "CalAlrms", 8) == 0)
155 calalrms = getbool(value);
156 else
157 printf("Error in %s at line %d :\n[%s].\n", config_file, i, line);
162 char *robust_home() {
163 if (getenv("HOME"))
164 return getenv("HOME");
165 else if (getenv("USER") && getpwnam(getenv("USER")))
166 return getpwnam (getenv ("USER") )->pw_dir;
167 else if (getenv ("LOGNAME") && getpwnam(getenv("LOGNAME")))
168 return getpwnam(getenv("LOGNAME"))->pw_dir;
169 else if ((getuid() != -1) && (getpwuid(getuid())))
170 return getpwuid(getuid())->pw_dir;
171 else
172 return "/";
176 void save_cfgfile() {
177 FILE *file;
178 Alarm *alrm = alarms;
180 if ((file = fopen(config_file, "w")) == NULL) {
181 if (strstr(config_file, "/"DEFAULT_CFGFILE) == NULL)
182 printf("Unable to open configuration file \"%s\".\n", config_file);
183 return;
186 fprintf(file, configfile,
187 backlight ? "On" : "Off",
188 light_color ? light_color : "", /*"#6ec63b",*/
189 command ? command : "",
190 msgcmd ? msgcmd : "",
191 switch_authorized ? "Yes" : "No",
192 h12 ? "True" : "False",
193 time_mode,
194 use_locale ? "Yes" : "No",
195 style_dir ? style_dir : "",
196 style_name ? style_name : "",
197 showcal ? "Yes" : "No",
198 calalrms ? "On" : "Off");
199 while (alrm) {
200 fprintf(file, alarmline,
201 alrm->on ? "On" : "Off",
202 alrm->time ? alrm->time : "12:00",
203 alrm->date ? "-" : "",
204 alrm->date ? alrm->date : "",
205 alrm->msg ? "." : "",
206 alrm->msg ? alrm->msg : "");
207 alrm = alrm->next;
209 fclose(file);