Skip to content

Instantly share code, notes, and snippets.

@NewbiZ
Last active January 20, 2016 16:15
Show Gist options
  • Save NewbiZ/1e18e01e7304d3122667 to your computer and use it in GitHub Desktop.
Save NewbiZ/1e18e01e7304d3122667 to your computer and use it in GitHub Desktop.
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
void
version(void)
{
printf("HTTPMon 1.0.0\n");
}
void
usage(void)
{
printf("usage:\n");
printf("-v/--version\tdisplay program version\n");
printf("-h/--help\tdisplay this help\n");
printf("-q/--request-if\tinterface of incoming HTTP requests\n");
printf("-s/--response-if\tinterface of outgoing HTTP responses\n");
}
int
main(int argc, char** argv)
{
int ch;
const char* request_if = NULL;
const char* response_if = NULL;
char* tmp;
unsigned long worker_count = 1;
struct option longopts[] = {
{"version", 0, NULL, 'v'},
{"help", 0, NULL, 'h'},
{"request-if", 1, NULL, 'q'},
{"response-if", 1, NULL, 's'},
{"worker-count", 1, NULL, 'w'},
{NULL, 0, NULL, 0},
};
while ((ch = getopt_long(argc, argv, "hvq:s:w:", longopts, NULL)) != -1) {
switch (ch) {
case 'v':
version();
exit(0);
case 'h':
usage();
exit(0);
case 'q':
request_if = optarg;
break;
case 's':
response_if = optarg;
break;
case 'w':
worker_count = strtoul(optarg, &tmp, 10);
if ((worker_count==ULONG_MAX && errno==ERANGE) /* overflow */
|| *tmp!=0) { /* or trash characters */
fprintf(stderr, "%s: error parsing worker count: %s\n",
argv[0], optarg);
usage();
exit(1);
}
break;
case '?':
default:
usage();
exit(1);
}
}
if (request_if==NULL) {
fprintf(stderr, "%s: missing required argument -q/--request-if\n",
argv[0]);
usage();
exit(1);
}
if (response_if==NULL) {
fprintf(stderr, "%s: missing required argument -s/--response-if\n",
argv[0]);
usage();
exit(1);
}
version();
printf("request interface: %s\n", request_if);
printf("response interface: %s\n", response_if);
printf("worker count: %lu\n", worker_count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment