This repository has been archived by the owner on Jun 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
97 lines (79 loc) · 1.78 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @author Benjamin Oakes <hello@benjaminoakes.com>
*/
#include <signal.h>
#include <stdio.h>
#include "mhttpd.h"
#define DEFAULT_PORT 80
/** Name this program was called with */
const char* progname;
/** Print usage information */
static void show_usage() {
printf(
"Usage: %s [--help] [port]\n"
"\n"
"Serve a directory over HTTP\n"
"\n"
" --help Show this help text.\n"
" port Port number. Default: %d (requires privileges)\n"
"\n"
"Example:\n"
"\n"
" %s 3000\n"
, progname
, DEFAULT_PORT
, progname
);
exit(-1);
}
/**
* Process command line arguments
*
* @return port number
*/
static int process_args(int argc, char* argv[]) {
int i;
int port = DEFAULT_PORT;
for (i = 0; i < argc; i++) {
if (0 == strcmp(argv[i], "--help")) {
show_usage();
}
}
if (argc == 2) {
port = atoi(argv[1]);
} else if (argc > 2) {
show_usage();
}
return port;
}
/**
* Handle quit signal
* @see http://www.go4expert.com/forums/showthread.php?t=839
*/
static void signal_quit() {
debug("%s", __FUNCTION__);
// TODO clean up?
exit(0);
}
/** Handle interrupt signal */
static void signal_interrupt() {
debug("%s", __FUNCTION__);
signal_quit();
}
/** Start server */
static void start(int port) {
log_info("Starting...");
log_info("Listening on port %d", port);
mhttp.listen(port);
log_info("Finished");
}
/** Initialize, process arguments, and start. */
int main(int argc, char* argv[]) {
int port;
progname = argv[0];
signal(SIGINT, signal_interrupt);
signal(SIGQUIT, signal_quit);
port = process_args(argc, argv);
start(port);
return 0;
}