You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

#define _GNU_SOURCE
#include <stdio.h>
#include <getopt.h>
#include "srv.c"
#define DEFAULT_ADDR "127.0.0.1"
#define DEFAULT_PORT 7677
void help(char* exe) {
char* help = "%s - a simple webserver.\n"
" --help: this help message\n"
" --addr [addr], -a [addr]: set the ip address to bind to (default: 127.0.0.1)\n"
" --port [port], -p [port]: set the port to bind to (default: 7677)\n"
" --dir [directory], -d [directory]: set the directory to serve out of (default: the CWD)\n";
printf(help, exe);
}
int main(int argc, char** argv) {
char* ADDR = DEFAULT_ADDR;
int PORT = DEFAULT_PORT;
int c;
int opt_idx;
char* longopt;
struct option long_options[] = {
{"addr", required_argument, 0, 'a'},
{"port", required_argument, 0, 'p'},
{"dir", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0 }
};
while (1) {
c = getopt_long(argc, argv, "a:p:d:h", long_options, &opt_idx);
if (c==-1) break;
switch (c) {
case 0:
longopt = (char*)long_options[opt_idx].name;
printf("%s\n", longopt);
if (!strcmp(longopt, "help")) {
help(argv[0]);
exit(0);
} else if (!strcmp(longopt, "addr")) {
ADDR = optarg;
} else if (!strcmp(longopt, "port")) {
PORT = atoi(optarg);
} else if (!strcmp(longopt, "dir")) {
chdir(optarg);
}
break;
case 'a':
ADDR = optarg;
break;
case 'p':
PORT = atoi(optarg);
break;
case 'd':
chdir(optarg);
break;
case 'h':
help(argv[0]);
exit(0);
}
}
srv(ADDR, PORT);
}