From: Jaakko Heinonen Date: Thu, 7 Feb 2002 12:33:13 +0000 (+0000) Subject: Attempt to improve --datafile behavior X-Git-Tag: upstream/0.6.1~2^2~303 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=481433c4d30a6bd8d8f9cfb88e1d83af69a640a2;p=pkg%2Fabook.git Attempt to improve --datafile behavior --- diff --git a/ChangeLog b/ChangeLog index ea55e01..2b65755 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 2002-xx-xx - abook can be compiled with g++ + - attempt to improve --datafile behavior 2002-02-04 - use getopt to parse command line (also new options added) diff --git a/abook.c b/abook.c index 7b119b9..45eb83d 100644 --- a/abook.c +++ b/abook.c @@ -33,7 +33,6 @@ static void init_abook(); static void quit_abook_sig(int i); static void set_filenames(); -static void free_filenames(); static void parse_command_line(int argc, char **argv); static void show_usage(); static void mutt_query(char *str); @@ -45,6 +44,24 @@ static void add_email(int); char *datafile = NULL; char *rcfile = NULL; +int alternative_datafile = FALSE; +int alternative_rcfile = FALSE; + +static int +datafile_writeable() +{ + FILE *f; + + assert(datafile != NULL); + + if( (f = fopen(datafile, "a")) == NULL) + return FALSE; + + fclose(f); + + return TRUE; +} + static void init_abook() { @@ -59,26 +76,21 @@ init_abook() umask(DEFAULT_UMASK); - /* - * this is very ugly for now - */ - /*if( options_get_int("datafile", "autosave") )*/ - - if( load_database(datafile) == 2 ) { - char *tmp = strconcat(getenv("HOME"), - "/" DATAFILE, NULL); - - if( safe_strcmp(tmp, datafile) ) { - refresh_screen(); - statusline_msg("Sorry, the specified file does " - "not appear to be a valid abook addressbook"); - statusline_msg("Will open default addressbook..."); - free(datafile); - datafile = tmp; - load_database(datafile); - } else - free(tmp); - } + if(!datafile_writeable()) { + char *s = mkstr("File %s is not writeable", datafile); + refresh_screen(); + statusline_msg(s); + free(s); + if(load_database(datafile) || !statusline_ask_boolean( + "If you continue all changes will " + "be lost. Do you want to continue?", FALSE)) { + close_config(); + close_database(); + close_ui(); + exit(1); + } + } else + load_database(datafile); refresh_screen(); } @@ -123,6 +135,14 @@ main(int argc, char **argv) return 0; } +static void +free_filenames() +{ + my_free(rcfile); + my_free(datafile); +} + + static void set_filenames() { @@ -142,13 +162,6 @@ set_filenames() atexit(free_filenames); } -static void -free_filenames() -{ - my_free(rcfile); - my_free(datafile); -} - /* * CLI */ @@ -261,6 +274,7 @@ parse_command_line(int argc, char **argv) break; case 'f': set_filename(&datafile, optarg); + alternative_datafile = TRUE; break; case OPT_MUTT_QUERY: query_string = optarg; @@ -268,6 +282,7 @@ parse_command_line(int argc, char **argv) break; case 'C': set_filename(&rcfile, optarg); + alternative_rcfile = TRUE; break; case OPT_CONVERT: change_mode(&mode, MODE_CONVERT); diff --git a/database.c b/database.c index 2a5ce8b..8b22602 100644 --- a/database.c +++ b/database.c @@ -136,10 +136,7 @@ load_database(char *filename) parse_database(in); - if ( items == 0 ) - return 2; - - return 0; + return (items == 0) ? 2 : 0; } int diff --git a/ui.c b/ui.c index 695577e..1630a82 100644 --- a/ui.c +++ b/ui.c @@ -44,6 +44,8 @@ extern int items, curitem; extern char *datafile; +extern int alternative_datafile; + /* * internal variables */ @@ -629,4 +631,6 @@ ui_open_datafile() refresh_screen(); free(filename); + + alternative_datafile = TRUE; }