]> git.deb.at Git - pkg/abook.git/blobdiff - abook.c
some int -> bool changes
[pkg/abook.git] / abook.c
diff --git a/abook.c b/abook.c
index 7b119b9b6c8b9174fbd0316e1660ede2c0881450..da165382683649f3fb596b1e13e285c8e4ce5a1d 100644 (file)
--- a/abook.c
+++ b/abook.c
@@ -12,6 +12,7 @@
 #include <sys/stat.h>
 #include <signal.h>
 #include <fcntl.h>
+#include <errno.h>
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
@@ -33,7 +34,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,40 +45,89 @@ static void         add_email(int);
 char *datafile = NULL;
 char *rcfile = NULL;
 
+bool alternative_datafile = FALSE;
+bool 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
+check_abook_directory()
+{
+       struct stat s;
+       char *dir;
+
+       assert(!is_ui_initialized());
+
+       if(alternative_datafile)
+               return;
+
+       dir = strconcat(getenv("HOME"), "/" DIR_IN_HOME, NULL);
+       assert(dir != NULL);
+
+       if(stat(dir, &s) == -1) {
+               if(errno != ENOENT) {
+                       perror(dir);
+                        free(dir);
+                        exit(1);
+               }
+               if(mkdir(dir, 0700) == -1) {
+                       printf("Cannot create directory %s\n", dir);
+                       perror(dir);
+                       free(dir);
+                       exit(1);
+               }
+       } else if(!S_ISDIR(s.st_mode)) {
+               printf("%s is not a directory\n", dir);
+               free(dir);
+               exit(1);
+       }
+
+       free(dir);
+}
+
 static void
 init_abook()
 {
        set_filenames();
+       check_abook_directory();
        init_options();
 
        signal(SIGKILL, quit_abook_sig);
        signal(SIGTERM, quit_abook_sig);
-       
+
        if( init_ui() )
                exit(1);
-       
+
        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();
 }
@@ -95,7 +144,7 @@ quit_abook()
        close_database();
 
        close_ui();
-       
+
        exit(0);
 }
 
@@ -111,18 +160,26 @@ main(int argc, char **argv)
 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
        setlocale(LC_ALL, "" );
 #endif
-               
+
        parse_command_line(argc, argv);
-       
+
        init_abook();
 
        get_commands(); 
-       
+
        quit_abook();
 
        return 0;
 }
 
+static void
+free_filenames()
+{
+       my_free(rcfile);
+       my_free(datafile);
+}
+
+
 static void
 set_filenames()
 {
@@ -134,21 +191,16 @@ set_filenames()
        }
 
        if(!datafile)
-               datafile = strconcat(getenv("HOME"), "/" DATAFILE, NULL);
+               datafile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
+                               DATAFILE, NULL);
 
        if(!rcfile)
-               rcfile = strconcat(getenv("HOME"), "/" RCFILE, NULL);
+               rcfile = strconcat(getenv("HOME"), "/" DIR_IN_HOME "/"
+                               RCFILE, NULL);
 
        atexit(free_filenames);
 }
 
-static void
-free_filenames()
-{
-       my_free(rcfile);
-       my_free(datafile);
-}
-
 /*
  * CLI
  */
@@ -178,11 +230,11 @@ void
 set_filename(char **var, char *path)
 {
        char *cwd;
-       
+
        assert(var != NULL);
        assert(*var == NULL); /* or else we probably leak memory */
        assert(path != NULL);
-       
+
        if(*path == '/') {
                *var = strdup(path);
                return;
@@ -261,6 +313,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 +321,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);
@@ -362,7 +416,7 @@ muttq_print_item(FILE *file, int item)
        int i;
 
        split_emailstr(item, emails);
-       
+
        for(i = 0; i < (options_get_int("mutt_return_all_emails") ?
                        MAX_EMAILS : 1) ; i++)
                if( *emails[i] )
@@ -405,7 +459,7 @@ init_mutt_query()
 {
        set_filenames();
        init_options();
-       
+
        if( load_database(datafile) ) {
                printf("Cannot open database\n");
                quit_mutt_query(1);
@@ -428,7 +482,7 @@ make_mailstr(int item)
                strdup(name);
 
        free(name);
-       
+
        return ret;
 }
 
@@ -479,7 +533,7 @@ launch_mutt(int item)
 #endif
        system(cmd);    
        free(cmd);
-       
+
        /*
         * we need to make sure that curses settings are correct
         */
@@ -549,18 +603,17 @@ FILE *
 abook_fopen (const char *path, const char *mode)
 {      
        struct stat s;
-       
+
        if( ! strchr(mode, 'r') )
                return fopen(path, mode);
-       
+
        if ( (stat(path, &s)) == -1 )
                return NULL;
-       
+
        return S_ISREG(s.st_mode) ? fopen(path, mode) : NULL;
 }
 
 
-
 static void
 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
 {
@@ -649,7 +702,7 @@ init_add_email()
        atexit(free_filenames);
        init_options();
        atexit(close_config);
-       
+
        /*
         * we don't actually care if loading fails or not
         */
@@ -701,7 +754,7 @@ add_email(int quiet)
 {
        char *line;
        char *name = NULL, *email = NULL;
-       
+
        init_add_email();
 
        do {