22 #include "fileutils.h"
48 #define MAIN_ERR_PREFIX "CONF: "
51 #define CONF_PERM (posix_mode_t) (POSIX_S_IRUSR | POSIX_S_IWUSR)
72 static int conf_initialized = 0;
73 static const char confname[] =
"configfile";
74 static const char tmpname[] =
"configfile.new";
111 static void conf_init_configuration(
struct conf* cfg)
113 const char server_default[] =
"localhost";
114 const char service_default[] =
"nntp";
115 const char test_ere_default[] =
"\\.test$|^test$";
116 const char test_kwords_default[] =
"ignore,no-reply";
117 const char crl_upd_ts_default[] =
"1970-01-01T00:00:00Z";
118 const char sigfile_default[] =
".signature";
120 const char intro_default[] =
"%s wrote:";
126 conf_add_entry_int(cfg,
CONF_POS_X,
"pos_x:", 10);
127 conf_add_entry_int(cfg,
CONF_POS_Y,
"pos_y:", 40);
128 conf_add_entry_int(cfg,
CONF_SIZE_X,
"size_x:", 750);
129 conf_add_entry_int(cfg,
CONF_SIZE_Y,
"size_y:", 350);
130 conf_add_entry_int(cfg,
CONF_TILE_X,
"tile_x:", 200);
131 conf_add_entry_int(cfg,
CONF_TILE_Y,
"tile_y:", 120);
134 string = (
char*) posix_malloc(strlen(server_default) + (size_t) 1);
135 if(NULL ==
string) { error = 1; }
138 strcpy(
string, server_default);
139 conf_add_entry_string(cfg,
CONF_SERVER,
"server:",
string);
143 string = (
char*) posix_malloc(strlen(service_default) + (size_t) 1);
144 if(NULL ==
string) { error = 1; }
147 strcpy(
string, service_default);
148 conf_add_entry_string(cfg,
CONF_SERVICE,
"service:",
string);
152 conf_add_entry_int(cfg,
CONF_ENC,
"enc:", 0);
155 conf_add_entry_int(cfg,
CONF_AUTH,
"auth:", 0);
157 empty = (
char*) posix_malloc((
size_t) 1);
158 if(NULL == empty) { error = 1; }
162 conf_add_entry_string(cfg,
CONF_USER,
"user:", empty);
164 empty = (
char*) posix_malloc((
size_t) 1);
165 if(NULL == empty) { error = 1; }
169 conf_add_entry_string(cfg,
CONF_PASS,
"pass:", empty);
173 conf_add_entry_int(cfg,
CONF_CAC,
"cac:", 250);
176 empty = (
char*) posix_malloc((
size_t) 1);
177 if(NULL == empty) { error = 1; }
181 conf_add_entry_string(cfg,
CONF_FROM,
"from:", empty);
185 empty = (
char*) posix_malloc((
size_t) 1);
186 if(NULL == empty) { error = 1; }
190 conf_add_entry_string(cfg,
CONF_REPLYTO,
"replyto:", empty);
194 empty = (
char*) posix_malloc((
size_t) 1);
195 if(NULL == empty) { error = 1; }
199 conf_add_entry_string(cfg,
CONF_FQDN,
"fqdn:", empty);
203 empty = (
char*) posix_malloc((
size_t) 1);
204 if(NULL == empty) { error = 1; }
208 conf_add_entry_string(cfg,
CONF_NEWSRC,
"newsrc:", empty);
212 empty = (
char*) posix_malloc((
size_t) 1);
213 if(NULL == empty) { error = 1; }
217 conf_add_entry_string(cfg,
CONF_SCORERC,
"scorerc:", empty);
221 string = (
char*) posix_malloc(strlen(intro_default) + (size_t) 1);
222 if(NULL ==
string) { error = 1; }
225 strcpy(
string, intro_default);
226 conf_add_entry_string(cfg,
CONF_INTRO,
"intro:",
string);
230 empty = (
char*) posix_malloc((
size_t) 1);
231 if(NULL == empty) { error = 1; }
239 conf_add_entry_int(cfg,
CONF_TVIEW,
"tview:", 1);
242 conf_add_entry_int(cfg,
CONF_ONLYUR,
"onlyur:", 0);
245 empty = (
char*) posix_malloc((
size_t) 1);
246 if(NULL == empty) { error = 1; }
254 empty = (
char*) posix_malloc((
size_t) 1);
255 if(NULL == empty) { error = 1; }
259 conf_add_entry_string(cfg,
CONF_EDITOR,
"editor:", empty);
263 empty = (
char*) posix_malloc((
size_t) 1);
264 if(NULL == empty) { error = 1; }
268 conf_add_entry_string(cfg,
CONF_PPROC,
"post_proc:", empty);
272 empty = (
char*) posix_malloc((
size_t) 1);
273 if(NULL == empty) { error = 1; }
277 conf_add_entry_string(cfg,
CONF_INEWS,
"inews:", empty);
287 string = (
char*) posix_malloc(strlen(test_ere_default) + (size_t) 1);
288 if(NULL ==
string) { error = 1; }
291 strcpy(
string, test_ere_default);
296 string = (
char*) posix_malloc(strlen(test_kwords_default) + (size_t) 1);
297 if(NULL ==
string) { error = 1; }
300 strcpy(
string, test_kwords_default);
312 string = (
char*) posix_malloc(strlen(crl_upd_ts_default) + (size_t) 1);
313 if(NULL ==
string) { error = 1; }
316 strcpy(
string, crl_upd_ts_default);
333 conf_add_entry_int(cfg,
CONF_NO_OVER,
"nntp_no_over:", 0);
342 string = (
char*) posix_malloc(strlen(sigfile_default) + (size_t) 1);
343 if(NULL ==
string) { error = 1; }
346 strcpy(
string, sigfile_default);
347 conf_add_entry_string(cfg,
CONF_SIGFILE,
"signature_file:",
string);
351 conf_add_entry_int(cfg,
CONF_TS_LTIME,
"timestamp_ltime:", 1);
369 empty = (
char*) posix_malloc((
size_t) 1);
370 if(NULL == empty) { error = 1; }
398 if(error) {
PRINT_ERROR(
"Initializing configuration failed"); }
399 else { conf_initialized = 1; }
409 static int conf_get_confpathname(
const char** confpathname,
410 const char* conffile)
416 if(NULL != *confpathname)
434 posix_free((
void*) *confpathname);
435 *confpathname = NULL;
445 static int conf_extract_entry(
char* line,
struct conf* target)
448 size_t labelsize = strlen(target->
label);
455 if(strlen(line) > labelsize)
458 if(!strncmp(target->
label, line, labelsize))
465 string = &line[labelsize];
466 if(1 == sscanf(
string,
"%d", &
val)) target->
val.
i =
val;
474 if(
' ' != line[i]) {
break; }
491 sbuf = (
char*) posix_malloc(strlen(
string) + (size_t) 1);
494 PRINT_ERROR(
"Cannot allocate memory for config file entry");
495 target->
val.
s = NULL;
500 strcpy(sbuf,
string);
502 posix_free((
void*) target->
val.
s);
503 target->
val.
s = sbuf;
516 static int conf_update_entry(
char** line,
size_t len,
struct conf* target)
519 size_t labelsize = strlen(target->
label);
524 if(strlen(*line) >= labelsize)
527 if(!strncmp(target->
label, *line, labelsize))
538 reqd = posix_snprintf(*line, len,
"%s %d\n",
544 reqd = posix_snprintf(*line, len,
"%s %s\n",
547 if(0 > reqd) {
break; }
548 else if(len <= (
size_t) reqd)
551 if(POSIX_SIZE_MAX <= (
size_t) reqd) {
break; }
552 string = (
char*) posix_realloc(*line, (
size_t) (reqd + 1));
553 if(NULL ==
string) {
break; }
554 else { *line = string; }
557 while(len <= (
size_t) reqd);
568 static int conf_import_configuration(
struct conf* cfg, FILE* fs)
573 posix_ssize_t readlen;
579 readlen = posix_getline(&line, &len, fs);
582 if(POSIX_ENOMEM == posix_errno)
584 PRINT_ERROR(
"Cannot assign memory for config file parser");
602 if(0 >= readlen) {
break; }
606 if(!strncmp(
"#", line, 1)) {
continue; }
609 if(!conf_extract_entry(line, &cfg[i])) {
break; }
616 posix_free((
void*) line);
625 static int conf_export_configuration(
struct conf* cfg,
626 FILE* fs, FILE* fs_tmp)
631 posix_ssize_t readlen;
640 readlen = posix_getline(&line, &len, fs);
643 if(POSIX_ENOMEM == posix_errno)
645 PRINT_ERROR(
"Cannot assign memory for config file parser");
663 if(0 >= readlen) {
break; }
669 if(!conf_update_entry(&line, len, &cfg[i])) {
break; }
673 rv = fprintf(fs_tmp,
"%s", line);
674 if(0 > rv) {
break; }
686 reqd = strlen(cfg[i].
label);
687 if(POSIX_SIZE_MAX <= reqd) {
break; }
688 if(reqd + (
size_t) 1 > len)
691 p = (
char*) posix_realloc(line, reqd + (
size_t) 1);
692 if (NULL != p) line = p;
694 strncpy(line, cfg[i].
label, len);
695 rv = conf_update_entry(&line, len, &cfg[i]);
699 rv = fprintf(fs_tmp,
"%s", line);
711 posix_free((
void*) line);
737 posix_free((
void*) cfg[i].
val.
s);
744 conf_initialized = 0;
767 const char* confpathname = NULL;
771 conf_init_configuration(cfg);
775 res = conf_get_confpathname(&confpathname, confname);
785 res =
fu_open_file(confpathname, &fd, POSIX_O_RDWR | POSIX_O_CREAT,
791 if(!res) { res = conf_import_configuration(cfg, fs); }
798 posix_free((
void*) confpathname);
825 const char* confpathname = NULL;
826 const char* tmppathname = NULL;
829 res = conf_get_confpathname(&confpathname, confname);
830 if(!res) { res = conf_get_confpathname(&tmppathname, tmpname); }
835 res =
fu_open_file(confpathname, &fd, POSIX_O_RDWR | POSIX_O_CREAT,
845 POSIX_O_WRONLY | POSIX_O_CREAT | POSIX_O_TRUNC,
863 res = conf_export_configuration(cfg, fs, fs_tmp);
867 if(!res) { res =
fu_sync(fd_tmp, fs_tmp); }
870 if(!res) { res = posix_rename(tmppathname, confpathname); }
875 PRINT_ERROR(
"Failed to store configuration in config file");
887 printf(
"%s: %sStore to: %s\n", CFG_NAME,
MAIN_ERR_PREFIX, confpathname);
891 posix_free((
void*) tmppathname);
892 posix_free((
void*) confpathname);
918 if(NULL != cfg && NULL != cfg->
val.
s && NULL != s)
923 p = posix_malloc(++len);
928 posix_free((
void*) cfg->
val.
s);