#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
+#include <errno.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
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);
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
+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);
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();
}
return 0;
}
+static void
+free_filenames()
+{
+ my_free(rcfile);
+ my_free(datafile);
+}
+
+
static void
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
*/
break;
case 'f':
set_filename(&datafile, optarg);
+ alternative_datafile = TRUE;
break;
case OPT_MUTT_QUERY:
query_string = optarg;
break;
case 'C':
set_filename(&rcfile, optarg);
+ alternative_rcfile = TRUE;
break;
case OPT_CONVERT:
change_mode(&mode, MODE_CONVERT);