QDNix
Quick’n’dirty *NIX
main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <signal.h>
5 
6 #define HTTPD_VERSION "1.0"
7 
8 #define IS_OPTARG(a, s, l) a[1] == s || \
9  strcmp(a + 1, "-" l) == 0
10 
11 static const char *prg_name;
12 static const char *srv_dir = "/var/html";
13 
14 static void
15 version(void)
16 {
17  printf("%s (QDNix) v%s\n", prg_name, HTTPD_VERSION);
18  printf("Copyright (C) 2023 d0p1\n");
19  printf("License BSD-3: <https://directory.fsf.org/wiki/License:BSD-3-Clause>\n");
20  printf("This is free software: you are free to change and redistribute it.\n");
21  printf("There is NO WARRANTY, to the extent permitted by law.\n");
22  exit(EXIT_SUCCESS);
23 }
24 
25 static void
26 usage(int retval)
27 {
28  printf("Usage: %s [OPTION].. [DIR]\n\n", prg_name);
29  printf("Arguments:\n");
30  printf("\tDIR\t\tHTML page directory (default: %s)\n", srv_dir);
31  printf("\tOptions:\n");
32  printf("\t-h,--help\tdisplay this menu and exit\n");
33  printf("\t-V,--version\toutput version information and exit\n");
34  exit(retval);
35 }
36 
37 int
38 main(int argc, char *const argv[])
39 {
40  size_t idx;
41 
42  prg_name = *argv++;
43  argc--;
44 
45  signal(SIGPIPE, SIG_IGN);
46 
47  for (idx = 0; idx < argc; idx++)
48  {
49  if (IS_OPTARG(argv[idx], 'V', "version"))
50  {
51  version();
52  }
53  if (IS_OPTARG(argv[idx], 'h', "help"))
54  {
55  usage(EXIT_SUCCESS);
56  }
57  }
58  return (EXIT_SUCCESS);
59 }