create config directory in HOME and read config from there

This commit is contained in:
josch
2014-10-26 14:11:22 +01:00
parent b6026aa350
commit 21465f8943

View File

@@ -28,7 +28,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/stat.h>
#include <bits/fcntl-linux.h> #include <bits/fcntl-linux.h>
#include <fuse/fuse_common.h> #include <fuse/fuse_common.h>
#include <pwd.h> #include <pwd.h>
@@ -397,23 +397,22 @@ static void parse_config(int *argc, char ***argv, char *configfile)
// commandline and should be set by the configuration file // commandline and should be set by the configuration file
// try set config file first if set // try set config file first if set
if (configfile != NULL && (fp = fopen(configfile, "r")) != NULL) { if (configfile != NULL) {
if ((fp = fopen(configfile, "r")) != NULL) {
parse_config_file(fp, argc, argv); parse_config_file(fp, argc, argv);
fclose(fp); fclose(fp);
return; return;
} else {
fprintf(stderr, "Cannot open configuration file %s\n", configfile);
exit(1);
} }
// try "./.mediafire-tools.conf" second
if ((fp = fopen("./.mediafire-tools.conf", "r")) != NULL) {
parse_config_file(fp, argc, argv);
fclose(fp);
return;
} }
// try "~/.mediafire-tools.conf" third // then try "~/.mediafire-tools/config"
if ((homedir = getenv("HOME")) == NULL) { if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir; homedir = getpwuid(getuid())->pw_dir;
} }
homedir = strdup_printf("%s/.mediafire-tools.conf", homedir); homedir = strdup_printf("%s/.mediafire-tools/config", homedir);
if ((fp = fopen("./.mediafire-tools.conf", "r")) != NULL) { if ((fp = fopen(homedir, "r")) != NULL) {
parse_config_file(fp, argc, argv); parse_config_file(fp, argc, argv);
fclose(fp); fclose(fp);
} }
@@ -550,11 +549,30 @@ int main(int argc, char *argv[])
{ {
int ret, int ret,
i; i;
char *homedir;
char *cachedir;
struct mediafirefs_user_options options = { struct mediafirefs_user_options options = {
NULL, NULL, NULL, NULL, -1, NULL NULL, NULL, NULL, NULL, -1, NULL
}; };
if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
homedir = strdup_printf("%s/.mediafire-tools", homedir);
/* EEXIST is okay, so only fail if it is something else */
if (mkdir(homedir, 0755) != 0 && errno != EEXIST) {
perror("mkdir");
exit(1);
}
cachedir = strdup_printf("%s/cache", homedir);
if (mkdir(cachedir, 0755) != 0 && errno != EEXIST) {
perror("mkdir");
exit(1);
}
free(cachedir);
free(homedir);
parse_arguments(&argc, &argv, &options); parse_arguments(&argc, &argv, &options);
connect_mf(&options); connect_mf(&options);