2 * Tools : memory management, file loading and saving
13 #include <sys/types.h>
17 #include <sys/types.h>
18 #include "configfile.h"
23 void alrm_add(Alarm
**list
, const char *value
) {
26 char *time
= NULL
, *date
= NULL
, *ison
= NULL
, *mesg
= NULL
, *at
;
32 lst
= xmalloc(sizeof(Alarm
));
35 if (strcmp(value
, lst
->entry
) == 0) ok
= FALSE
;
36 while ( (lst
->next
) && ok
) {
38 if (strcmp(value
, lst
->entry
) == 0) ok
= FALSE
;
41 lst
->next
= xmalloc(sizeof(Alarm
));
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
;
60 void free_alrm(Alarm
**list
) {
61 Alarm
*lst
= *list
, *next
;
75 void *xmalloc(size_t size
) {
76 void *ret
= malloc(size
);
85 char *xstrdup(const char *string
) {
86 char *ret
= strdup(string
);
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
);
109 void load_cfgfile() {
112 char line
[MAXSTRLEN
+ 1];
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
);
120 while (! feof(file
)) {
121 memset(line
, 0, MAXSTRLEN
+ 1);
122 fgets(line
, MAXSTRLEN
, file
);
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
);
157 printf("Error in %s at line %d :\n[%s].\n", config_file
, i
, line
);
162 char *robust_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
;
176 void save_cfgfile() {
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
);
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",
194 use_locale
? "Yes" : "No",
195 style_dir
? style_dir
: "",
196 style_name
? style_name
: "",
197 showcal
? "Yes" : "No",
198 calalrms
? "On" : "Off");
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
: "");