now properly prints out help

This commit is contained in:
Fufu Fang 2018-07-29 17:05:49 +01:00
parent f2d58eb839
commit 62aee1c657
1 changed files with 34 additions and 26 deletions

60
main.c
View File

@ -4,26 +4,33 @@
#include <getopt.h> #include <getopt.h>
#include <string.h> #include <string.h>
void add_arg(char ***new_argv_ptr, int *new_argc, char *opt_string); void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string);
static void print_help(); static void print_help(char *program_name);
/** /**
* \brief add an argument to an argv array * \brief add an argument to an argv array
* \details This is basically how you add a string to an array of string * \details This is basically how you add a string to an array of string
*/ */
void add_arg(char ***new_argv_ptr, int *new_argc, char *opt_string) void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string)
{ {
(*new_argc)++; (*fuse_argc)++;
*new_argv_ptr = realloc(*new_argv_ptr, *new_argc * sizeof(char *)); *fuse_argv_ptr = realloc(*fuse_argv_ptr, *fuse_argc * sizeof(char *));
char **new_argv = *new_argv_ptr; char **fuse_argv = *fuse_argv_ptr;
new_argv[*new_argc - 1] = opt_string; fuse_argv[*fuse_argc - 1] = opt_string;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char **fuse_argv = NULL;
int fuse_argc = 0;
char **new_argv = NULL; /* Add the program's name to the fuse argument */
int new_argc = 0; add_arg(&fuse_argv, &fuse_argc, argv[0]);
/* Automatically print help if not enough arguments are supplied */
if (argc < 2) {
add_arg(&fuse_argv, &fuse_argc, "--help");
goto fuse_start;
}
char c; char c;
int opts_index = 0; int opts_index = 0;
@ -39,52 +46,53 @@ int main(int argc, char **argv)
&opts_index)) != -1) { &opts_index)) != -1) {
switch (c) { switch (c) {
case 'o': case 'o':
add_arg(&new_argv, &new_argc, " -o "); add_arg(&fuse_argv, &fuse_argc, "-o");
add_arg(&new_argv, &new_argc, optarg); add_arg(&fuse_argv, &fuse_argc, optarg);
break; break;
case 'h': case 'h':
print_help(); print_help(argv[0]);
add_arg(&new_argv, &new_argc, " -h "); add_arg(&fuse_argv, &fuse_argc, "-h");
goto fuse_start; goto fuse_start;
break; break;
case 'V': case 'V':
add_arg(&new_argv, &new_argc, " -V "); add_arg(&fuse_argv, &fuse_argc, "-V");
break; break;
case 'd': case 'd':
add_arg(&new_argv, &new_argc, " -d "); add_arg(&fuse_argv, &fuse_argc, "-d");
break; break;
case 'f': case 'f':
add_arg(&new_argv, &new_argc, " -f "); add_arg(&fuse_argv, &fuse_argc, "-f");
break; break;
case 's': case 's':
add_arg(&new_argv, &new_argc, " -s"); add_arg(&fuse_argv, &fuse_argc, "-s");
break; break;
case '?': case '?':
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
}; };
/* Add the last remaining argument, which is the mount point */
add_arg(&fuse_argv, &fuse_argc, argv[argc-1]);
/* The second last remaining argument is the URL */ /* The second last remaining argument is the URL */
char *base_url = argv[argc-2]; char *base_url = argv[argc-2];
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) { if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
fprintf(stderr, "Please supply a valid URL.\n"); fprintf(stderr, "Error: Please supply a valid URL.\n");
print_help(); print_help(argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} } else {
network_init(base_url); network_init(base_url);
}
/* The last remaining argument is the mount point */
add_arg(&new_argv, &new_argc, argv[argc-1]);
fuse_start: fuse_start:
fuse_local_init(new_argc, new_argv); fuse_local_init(fuse_argc, fuse_argv);
return 0; return 0;
} }
static void print_help() static void print_help(char *program_name)
{ {
fprintf(stderr, fprintf(stderr,
"usage: mount-http-dir [options] URL mount_point\n"); "Usage: %s [options] URL mount_point\n", program_name);
} }