mirror of
https://github.com/fangfufu/httpdirfs.git
synced 2024-09-29 21:52:12 +02:00
added getopt_long
This commit is contained in:
parent
6f31ea774f
commit
f2d58eb839
86
main.c
86
main.c
@ -1,34 +1,90 @@
|
|||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "fuse_local.h"
|
#include "fuse_local.h"
|
||||||
|
|
||||||
static void help();
|
#include <getopt.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void add_arg(char ***new_argv_ptr, int *new_argc, char *opt_string);
|
||||||
|
static void print_help();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief add an argument to an argv array
|
||||||
|
* \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)
|
||||||
|
{
|
||||||
|
(*new_argc)++;
|
||||||
|
*new_argv_ptr = realloc(*new_argv_ptr, *new_argc * sizeof(char *));
|
||||||
|
char **new_argv = *new_argv_ptr;
|
||||||
|
new_argv[*new_argc - 1] = opt_string;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
* Copied from:
|
char **new_argv = NULL;
|
||||||
* https://www.cs.nmsu.edu/~pfeiffer/fuse-tutorial/src/bbfs.c
|
int new_argc = 0;
|
||||||
*/
|
|
||||||
if ((argc < 3) || (argv[argc-2][0] == '-') || (argv[argc-1][0] == '-')) {
|
char c;
|
||||||
help();
|
int opts_index = 0;
|
||||||
|
const char *short_opts = "o:hVdfs";
|
||||||
|
const struct option long_opts[] = {
|
||||||
|
{"help", no_argument, NULL, 'h'},
|
||||||
|
{"version", no_argument, NULL, 'V'},
|
||||||
|
{"debug", no_argument, NULL, 'd'},
|
||||||
|
{0, 0, 0, 0}
|
||||||
|
};
|
||||||
|
while ((c =
|
||||||
|
getopt_long(argc, argv, short_opts, long_opts,
|
||||||
|
&opts_index)) != -1) {
|
||||||
|
switch (c) {
|
||||||
|
case 'o':
|
||||||
|
add_arg(&new_argv, &new_argc, " -o ");
|
||||||
|
add_arg(&new_argv, &new_argc, optarg);
|
||||||
|
break;
|
||||||
|
case 'h':
|
||||||
|
print_help();
|
||||||
|
add_arg(&new_argv, &new_argc, " -h ");
|
||||||
|
goto fuse_start;
|
||||||
|
break;
|
||||||
|
case 'V':
|
||||||
|
add_arg(&new_argv, &new_argc, " -V ");
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
add_arg(&new_argv, &new_argc, " -d ");
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
add_arg(&new_argv, &new_argc, " -f ");
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
add_arg(&new_argv, &new_argc, " -s");
|
||||||
|
break;
|
||||||
|
case '?':
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
argv[argc-2] = argv[argc-1];
|
/* The second last remaining argument is the URL */
|
||||||
argv[argc-1] = NULL;
|
char *base_url = argv[argc-2];
|
||||||
argc--;
|
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
|
||||||
|
fprintf(stderr, "Please supply a valid URL.\n");
|
||||||
|
print_help();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
network_init(base_url);
|
||||||
|
|
||||||
char *BASE_URL = argv[argc-2];
|
/* The last remaining argument is the mount point */
|
||||||
network_init(BASE_URL);
|
add_arg(&new_argv, &new_argc, argv[argc-1]);
|
||||||
|
|
||||||
fuse_local_init(argc, argv);
|
fuse_start:
|
||||||
|
fuse_local_init(new_argc, new_argv);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void help()
|
static void print_help()
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"usage: mount-http-dir [options] URL mount_point\n");
|
"usage: mount-http-dir [options] URL mount_point\n");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user