io.c: Error out on non-existent calendar file

Show an error message and die if the user specified a non-existent
custom calendar file. This fixes some random hangs when calcurse is used
in non-interactive mode within scripts.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2014-08-06 15:59:15 +02:00
parent f9208c0b3d
commit 8ead883c32

View File

@ -226,10 +226,7 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...)
*/ */
void io_init(const char *cfile, const char *datadir) void io_init(const char *cfile, const char *datadir)
{ {
FILE *data_file;
const char *home; const char *home;
char apts_file[BUFSIZ] = "";
int ch;
if (datadir != NULL) { if (datadir != NULL) {
home = datadir; home = datadir;
@ -265,41 +262,9 @@ void io_init(const char *cfile, const char *datadir)
snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH, home); snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH, home);
} }
} else { } else {
snprintf(apts_file, BUFSIZ, "%s", cfile); snprintf(path_apts, BUFSIZ, "%s", cfile);
strncpy(path_apts, apts_file, BUFSIZ); EXIT_IF(!io_file_exists(path_apts), _("%s does not exist"),
/* check if the file exists, otherwise create it */ path_apts);
data_file = fopen(path_apts, "r");
if (data_file == NULL) {
printf(_("%s does not exist, create it now [y/n]? "),
path_apts);
ch = getchar();
switch (ch) {
case 'N':
case 'n':
puts(_("aborting...\n"));
exit_calcurse(EXIT_FAILURE);
break;
case 'Y':
case 'y':
data_file = fopen(path_apts, "w");
if (data_file == NULL) {
perror(path_apts);
exit_calcurse(EXIT_FAILURE);
} else {
printf(_("%s successfully created\n"),
path_apts);
puts(_("starting interactive mode...\n"));
}
break;
default:
puts(_("aborting...\n"));
exit_calcurse(EXIT_FAILURE);
break;
}
}
file_close(data_file, __FILE_POS__);
} }
} }