added configuration file support
This commit is contained in:
parent
6684842b35
commit
2cc931afa3
79
main.c
79
main.c
|
@ -1,6 +1,7 @@
|
|||
#include "network.h"
|
||||
#include "fuse_local.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
#include <string.h>
|
||||
|
@ -12,6 +13,7 @@ static void print_help(char *program_name, int long_help);
|
|||
static void print_http_options();
|
||||
static int
|
||||
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc);
|
||||
void parse_config_file(char ***fuse_argv, int *fuse_argc);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
@ -37,6 +39,9 @@ int main(int argc, char **argv)
|
|||
goto fuse_start;
|
||||
}
|
||||
|
||||
/* parse the configuration file, if it exists */
|
||||
parse_config_file(&fuse_argv, &fuse_argc);
|
||||
|
||||
/* The second last remaining argument is the URL */
|
||||
char *base_url = argv[argc-2];
|
||||
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
|
||||
|
@ -56,6 +61,55 @@ int main(int argc, char **argv)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void parse_config_file(char ***fuse_argv, int *fuse_argc)
|
||||
{
|
||||
char *home = getenv("HOME");
|
||||
char *config_dir = "/.httpdirfs";
|
||||
char *main_config_name = "/config";
|
||||
int full_path_len = strnlen(home, 255) + strlen(config_dir) +
|
||||
strlen(main_config_name) + 1;
|
||||
char *full_path = calloc(full_path_len, sizeof(char *));
|
||||
strncat(full_path, home, strnlen(home, 255));
|
||||
strncat(full_path, config_dir, strlen(config_dir));
|
||||
strncat(full_path, main_config_name, strlen(main_config_name));
|
||||
|
||||
int argc = 1;
|
||||
char **argv = NULL;
|
||||
/* The buffer has to be able to fit a URL */
|
||||
int buf_len = URL_LEN_MAX;
|
||||
char buf[buf_len];
|
||||
FILE *config = fopen(full_path, "r");
|
||||
if (config) {
|
||||
argv = malloc(1 * sizeof(char *));
|
||||
/*
|
||||
* getopt_long() expects the first parameter to be the name of the
|
||||
* program that was called.
|
||||
*/
|
||||
argv[0] = "./httpdirfs";
|
||||
while (fgets(buf, buf_len, config)) {
|
||||
if (!strncmp(buf, "--", 2)) {
|
||||
argc++;
|
||||
buf[strnlen(buf, buf_len) - 1] = '\0';
|
||||
char *space;
|
||||
space = strchr(buf, ' ');
|
||||
if (!space) {
|
||||
argv = realloc(argv, argc * sizeof(char *));
|
||||
argv[argc - 1] = strndup(buf, buf_len);
|
||||
} else {
|
||||
argc++;
|
||||
argv = realloc(argv, argc * sizeof(char *));
|
||||
/* Only copy up to the space character*/
|
||||
argv[argc - 2] = strndup(buf, space - buf);
|
||||
/* Starts copying after the space */
|
||||
argv[argc - 1] = strndup(space + 1, buf_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
parse_arg_list(argc, argv, fuse_argv, fuse_argc);
|
||||
free(argv);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
|
||||
{
|
||||
|
@ -63,19 +117,21 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
|
|||
int long_index = 0;
|
||||
const char *short_opts = "o:hVdfsp:u:P:";
|
||||
const struct option long_opts[] = {
|
||||
{"help", no_argument, NULL, 'h'}, /* 0 */
|
||||
{"version", no_argument, NULL, 'V'}, /* 1 */
|
||||
{"debug", no_argument, NULL, 'd'}, /* 2 */
|
||||
{"user", required_argument, NULL, 'u'}, /* 3 */
|
||||
{"password", required_argument, NULL, 'p'}, /* 4 */
|
||||
{"proxy", required_argument, NULL, 'P'}, /* 5 */
|
||||
{"proxy-user", required_argument, NULL, 'L'}, /* 6 */
|
||||
{"proxy-pass", required_argument, NULL, 'L'}, /* 7 */
|
||||
/* Note that 'L' is returned for long options */
|
||||
{"help", no_argument, NULL, 'h'}, /* 0 */
|
||||
{"version", no_argument, NULL, 'V'}, /* 1 */
|
||||
{"debug", no_argument, NULL, 'd'}, /* 2 */
|
||||
{"username", required_argument, NULL, 'u'}, /* 3 */
|
||||
{"password", required_argument, NULL, 'p'}, /* 4 */
|
||||
{"proxy", required_argument, NULL, 'P'}, /* 5 */
|
||||
{"proxy-username", required_argument, NULL, 'L'}, /* 6 */
|
||||
{"proxy-password", required_argument, NULL, 'L'}, /* 7 */
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
while ((c =
|
||||
getopt_long(argc, argv, short_opts, long_opts,
|
||||
&long_index)) != -1) {
|
||||
printf("c: %c\n", c);
|
||||
switch (c) {
|
||||
case 'o':
|
||||
add_arg(fuse_argv, fuse_argc, "-o");
|
||||
|
@ -107,6 +163,7 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
|
|||
NETWORK_CONFIG.proxy = strndup(optarg, URL_LEN_MAX);
|
||||
break;
|
||||
case 'L':
|
||||
/* Long options */
|
||||
switch (long_index) {
|
||||
case 6:
|
||||
NETWORK_CONFIG.proxy_user = strndup(optarg,
|
||||
|
@ -158,12 +215,12 @@ static void print_http_options()
|
|||
{
|
||||
fprintf(stderr,
|
||||
"HTTP options:\n\
|
||||
-u --user HTTP authentication username\n\
|
||||
-u --username HTTP authentication username\n\
|
||||
-p --password HTTP authentication password\n\
|
||||
-P --proxy Proxy for libcurl, for more details refer to\n\
|
||||
https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html\n\
|
||||
--proxy-user Username for the proxy\n\
|
||||
--proxy-pass Password for the proxy\n\
|
||||
--proxy-username Username for the proxy\n\
|
||||
--proxy-password Password for the proxy\n\
|
||||
\n\
|
||||
libfuse options:\n");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue