2 * Tools : memory management, file loading and saving
13 #include <sys/types.h>
17 #include <sys/types.h>
18 #include "configfile.h"
21 int fexist(const char *filename
) {
24 if ( (file
= fopen(filename
, "r") ) == NULL
) return FALSE
;
31 void alrm_add(Alarm
**list
, const char *value
) {
34 char *time
= NULL
, *date
= NULL
, *ison
= NULL
, *mesg
= NULL
, *at
;
35 char *tokstr
= xstrdup(value
);
36 char *toksav
= tokstr
;
40 lst
= xmalloc(sizeof(Alarm
));
43 if (strcmp(value
, lst
->entry
) == 0) ok
= FALSE
;
44 while ( (lst
->next
) && ok
) {
46 if (strcmp(value
, lst
->entry
) == 0) ok
= FALSE
;
49 lst
->next
= xmalloc(sizeof(Alarm
));
52 at
= strchr(value
, '@');
53 if (at
) ison
= strtok(tokstr
, "@");
54 time
= strtok(at
? NULL
: tokstr
, "-.");
55 if (strchr(value
, '-')) date
= strtok(NULL
, ".");
56 mesg
= strtok(NULL
, "\n\0");
57 lst
->entry
= xstrdup(value
);
58 lst
->time
= time
? xstrdup(time
) : NULL
;
59 lst
->date
= date
? xstrdup(date
) : NULL
;
60 lst
->on
= ison
? getbool(ison
) : TRUE
;
61 lst
->msg
= mesg
? xstrdup(mesg
) : NULL
;
67 void free_alrm(Alarm
**list
) {
68 Alarm
*lst
= *list
, *next
;
82 int nb_alrm(Alarm
*list
) {
95 void *xmalloc(size_t size
) {
96 void *ret
= malloc(size
);
105 char *xstrdup(const char *string
) {
106 char *ret
= strdup(string
);
115 int getbool(char *value
) {
117 for (i
= 0 ; value
[i
] ; i
++) value
[i
] = tolower(value
[i
]);
118 if (strcmp(value
, "0") == 0) return FALSE
;
119 if (strcmp(value
, "1") == 0) return TRUE
;
120 if (strcmp(value
, "true") == 0) return TRUE
;
121 if (strcmp(value
, "false") == 0) return FALSE
;
122 if (strcmp(value
, "yes") == 0) return TRUE
;
123 if (strcmp(value
, "no") == 0) return FALSE
;
124 if (strcmp(value
, "on") == 0) return TRUE
;
125 if (strcmp(value
, "off") == 0) return FALSE
;
126 printf("Error in converting \"%s\" to boolean value.\n", value
);
131 void load_cfgfile() {
134 char line
[MAXSTRLEN
+ 1];
137 if ((file
= fopen(config_file
, "r")) == NULL
) {
138 if (strstr(config_file
, "/"DEFAULT_CFGFILE
) == NULL
)
139 printf("Unable to open configuration file \"%s\".\n", config_file
);
142 while (! feof(file
)) {
143 memset(line
, 0, MAXSTRLEN
+ 1);
144 fgets(line
, MAXSTRLEN
, file
);
146 if (line
[strlen(line
) - 1] == '\n') line
[strlen(line
) - 1] = 0;
147 if ((line
[0] == '#') || (line
[0] == 0)) continue;
148 value
= strchr(line
, '=') + 1;
149 while ((value
[0] == ' ') && (value
[0] != 0)) value
++;
150 if (value
[0] == 0) continue;
152 if (strncmp(line
, "Backlight", 9) == 0)
153 backlight
= getbool(value
);
154 else if (strncmp(line
, "Color", 5) == 0)
155 light_color
= xstrdup(value
);
156 else if (strncmp(line
, "Alarm", 5) == 0)
157 alrm_add(&alarms
, value
);
158 else if (strncmp(line
, "Command", 7) == 0)
159 command
= xstrdup(value
);
160 else if (strncmp(line
, "MessageCmd", 10) == 0)
161 msgcmd
= xstrdup(value
);
162 else if (strncmp(line
, "Blink", 5) == 0)
163 switch_authorized
= getbool(value
);
164 else if (strncmp(line
, "H12", 3) == 0)
165 h12
= getbool(value
);
166 else if (strncmp(line
, "Locale", 6) == 0)
167 use_locale
= getbool(value
);
168 else if (strncmp(line
, "StyleDir", 8) == 0)
169 style_dir
= xstrdup(value
);
170 else if (strncmp(line
, "Style", 5) == 0)
171 style_name
= xstrdup(value
);
172 else if (strncmp(line
, "TimeMode", 5) == 0)
173 time_mode
= atoi(value
);
174 else if (strncmp(line
, "ShowCal", 7) == 0)
175 showcal
= getbool(value
);
176 else if (strncmp(line
, "CalAlrms", 8) == 0)
177 calalrms
= getbool(value
);
179 printf("Error in %s at line %d :\n[%s].\n", config_file
, i
, line
);
184 char *robust_home() {
186 return getenv("HOME");
187 else if (getenv("USER") && getpwnam(getenv("USER")))
188 return getpwnam (getenv ("USER") )->pw_dir
;
189 else if (getenv ("LOGNAME") && getpwnam(getenv("LOGNAME")))
190 return getpwnam(getenv("LOGNAME"))->pw_dir
;
191 else if ((getuid() != -1) && (getpwuid(getuid())))
192 return getpwuid(getuid())->pw_dir
;
198 void save_cfgfile() {
200 Alarm
*alrm
= alarms
;
202 if ((file
= fopen(config_file
, "w")) == NULL
) {
203 if (strstr(config_file
, "/"DEFAULT_CFGFILE
) == NULL
)
204 printf("Unable to open configuration file \"%s\".\n", config_file
);
208 fprintf(file
, configfile
,
209 backlight
? "On" : "Off",
210 light_color
? light_color
: "", /*"#6ec63b",*/
211 command
? command
: "",
212 msgcmd
? msgcmd
: "",
213 switch_authorized
? "Yes" : "No",
214 h12
? "True" : "False",
216 use_locale
? "Yes" : "No",
217 style_dir
? style_dir
: "",
218 style_name
? style_name
: "",
219 showcal
? "Yes" : "No",
220 calalrms
? "On" : "Off");
222 fprintf(file
, alarmline
,
223 alrm
->on
? "On" : "Off",
224 alrm
->time
? alrm
->time
: "12:00",
225 alrm
->date
? "-" : "",
226 alrm
->date
? alrm
->date
: "",
227 alrm
->msg
? "." : "",
228 alrm
->msg
? alrm
->msg
: "");