2 * Tools : memory management, file loading and saving
13 #include <sys/types.h>
17 #include <sys/types.h>
18 #include "configfile.h"
25 void alrm_add(Alarm
**list
, const char *value
) {
28 char *time
= NULL
, *date
= NULL
, *ison
= NULL
, *mesg
= NULL
, *at
;
34 lst
= xmalloc(sizeof(Alarm
));
37 if (strcmp(value
, lst
->entry
) == 0) ok
= FALSE
;
38 while ( (lst
->next
) && ok
) {
40 if (strcmp(value
, lst
->entry
) == 0) ok
= FALSE
;
43 lst
->next
= xmalloc(sizeof(Alarm
));
46 toksav
= tokstr
= xstrdup(value
);
47 at
= strchr(value
, '@');
48 if (at
) ison
= strtok(tokstr
, "@");
49 time
= strtok(at
? NULL
: tokstr
, "-.");
50 if (strchr(value
, '-')) date
= strtok(NULL
, ".");
51 mesg
= strtok(NULL
, "\n\0");
52 lst
->entry
= xstrdup(value
);
53 lst
->time
= time
? xstrdup(time
) : NULL
;
54 lst
->date
= date
? xstrdup(date
) : NULL
;
55 lst
->on
= ison
? getbool(ison
) : TRUE
;
56 lst
->msg
= mesg
? xstrdup(mesg
) : NULL
;
62 void free_alrm(Alarm
**list
) {
63 Alarm
*lst
= *list
, *next
;
77 void *xmalloc(size_t size
) {
78 void *ret
= malloc(size
);
87 char *xstrdup(const char *string
) {
88 char *ret
= strdup(string
);
97 int getbool(const char *value
) {
98 if (strcmp(value
, "0") == 0) return FALSE
;
99 if (strcmp(value
, "1") == 0) return TRUE
;
100 if (strcasecmp(value
, "true") == 0) return TRUE
;
101 if (strcasecmp(value
, "false") == 0) return FALSE
;
102 if (strcasecmp(value
, "yes") == 0) return TRUE
;
103 if (strcasecmp(value
, "no") == 0) return FALSE
;
104 if (strcasecmp(value
, "on") == 0) return TRUE
;
105 if (strcasecmp(value
, "off") == 0) return FALSE
;
106 printf("Error in converting \"%s\" to boolean value.\n", value
);
111 void load_cfgfile() {
114 char line
[MAXSTRLEN
+ 1];
117 if ((file
= fopen(config_file
, "r")) == NULL
) {
118 if (strstr(config_file
, "/"DEFAULT_CFGFILE
) == NULL
)
119 printf("Unable to open configuration file \"%s\".\n", config_file
);
122 free_light_color
= free_command
= 0;
123 while (! feof(file
)) {
124 memset(line
, 0, MAXSTRLEN
+ 1);
125 fgets(line
, MAXSTRLEN
, file
);
127 if (line
[strlen(line
) - 1] == '\n') line
[strlen(line
) - 1] = 0;
128 if ((line
[0] == '#') || (line
[0] == 0)) continue;
129 value
= strchr(line
, '=') + 1;
130 while ((value
[0] == ' ') && (value
[0] != 0)) value
++;
131 if (value
[0] == 0) continue;
133 if (strncmp(line
, "Backlight", 9) == 0)
134 backlight
= getbool(value
);
135 else if (strncmp(line
, "Color", 5) == 0) {
136 light_color
= xstrdup(value
);
137 free_light_color
= 1;
138 } else if (strncmp(line
, "Alarm", 5) == 0)
139 alrm_add(&alarms
, value
);
140 else if (strncmp(line
, "Command", 7) == 0) {
141 command
= xstrdup(value
);
143 } else if (strncmp(line
, "MessageCmd", 10) == 0)
144 msgcmd
= xstrdup(value
);
145 else if (strncmp(line
, "Blink", 5) == 0)
146 switch_authorized
= getbool(value
);
147 else if (strncmp(line
, "H12", 3) == 0)
148 h12
= getbool(value
);
149 else if (strncmp(line
, "Locale", 6) == 0)
150 use_locale
= getbool(value
);
151 else if (strncmp(line
, "StyleDir", 8) == 0)
152 style_dir
= xstrdup(value
);
153 else if (strncmp(line
, "Style", 5) == 0)
154 style_name
= xstrdup(value
);
155 else if (strncmp(line
, "TimeMode", 5) == 0)
156 time_mode
= atoi(value
);
157 else if (strncmp(line
, "ShowCal", 7) == 0)
158 showcal
= getbool(value
);
159 else if (strncmp(line
, "CalAlrms", 8) == 0)
160 calalrms
= getbool(value
);
162 printf("Error in %s at line %d :\n[%s].\n", config_file
, i
, line
);
167 char *robust_home() {
169 return getenv("HOME");
170 else if (getenv("USER") && getpwnam(getenv("USER")))
171 return getpwnam (getenv ("USER") )->pw_dir
;
172 else if (getenv ("LOGNAME") && getpwnam(getenv("LOGNAME")))
173 return getpwnam(getenv("LOGNAME"))->pw_dir
;
174 else if ((getuid() != -1) && (getpwuid(getuid())))
175 return getpwuid(getuid())->pw_dir
;
181 void save_cfgfile() {
183 Alarm
*alrm
= alarms
;
185 if ((file
= fopen(config_file
, "w")) == NULL
) {
186 if (strstr(config_file
, "/"DEFAULT_CFGFILE
) == NULL
)
187 printf("Unable to open configuration file \"%s\".\n", config_file
);
191 fprintf(file
, configfile
,
192 backlight
? "On" : "Off",
193 light_color
? light_color
: "", /*"#6ec63b",*/
194 command
? command
: "",
195 msgcmd
? msgcmd
: "",
196 switch_authorized
? "Yes" : "No",
197 h12
? "True" : "False",
199 use_locale
? "Yes" : "No",
200 style_dir
? style_dir
: "",
201 style_name
? style_name
: "",
202 showcal
? "Yes" : "No",
203 calalrms
? "On" : "Off");
205 fprintf(file
, alarmline
,
206 alrm
->on
? "On" : "Off",
207 alrm
->time
? alrm
->time
: "12:00",
208 alrm
->date
? "-" : "",
209 alrm
->date
? alrm
->date
: "",
210 alrm
->msg
? "." : "",
211 alrm
->msg
? alrm
->msg
: "");