exit(EXIT_FAILURE);
}
+static void get_database_dir_or_die(void)
+{
+ char *tmp;
+
+ if (conf.database_dir_given)
+ tmp = adu_strdup(conf.database_dir_arg);
+ else
+ tmp = make_message("%s%s",
+ conf.database_root_arg, conf.base_dir_arg);
+ /*
+ * As we change the cwd during database creation, database_dir
+ * must be an absolute path.
+ */
+ database_dir = absolute_path(tmp);
+ free(tmp);
+ if (database_dir)
+ return;
+ EMERG_LOG("failed to get absolute path of database dir\n");
+ exit(EXIT_FAILURE);
+}
+
/**
* The main function of adu.
*
ret = init_signals();
if (ret < 0)
goto out;
- ret = -E_SYNTAX;
- if (conf.database_dir_given)
- database_dir = adu_strdup(conf.database_dir_arg);
- else
- database_dir = make_message("%s%s",
- conf.database_root_arg, conf.base_dir_arg);
+ get_database_dir_or_die();
if (conf.select_given)
ret = com_select();
else if (conf.create_given)
free(argv);
return ret;
}
+
+char *absolute_path(const char *path)
+{
+ char *cwd, *ap;
+ long int path_max;
+
+ if (!path || !path[0])
+ return NULL;
+ if (path[0] == '/')
+ return adu_strdup(path);
+
+#ifdef PATH_MAX
+ path_max = PATH_MAX;
+#else
+ /*
+ * The result of pathconf(3) may be huge and unsuitable for mallocing
+ * memory. OTOH pathconf(3) may return -1 to signify that PATH_MAX is
+ * not bounded.
+ */
+ path_max = pathconf(name, _PC_PATH_MAX);
+ if (path_max <= 0 || path_max >= 4096)
+ path_max = 4096;
+#endif
+ cwd = adu_malloc(path_max);
+ if (!getcwd(cwd, path_max)) {
+ free(cwd);
+ return NULL;
+ }
+ ap = make_message("%s/%s", cwd, path);
+ free(cwd);
+ return ap;
+}
__must_check __malloc char *adu_strdup(const char *s);
__must_check __malloc char *adu_strcat(char *a, const char *b);
__must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...);
+__must_check __malloc char *absolute_path(const char *path);
__must_check int atoi64(const char *str, int64_t *result);
__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim);
int create_argv(const char *line, char ***result);