-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
60 lines (53 loc) · 1.37 KB
/
options.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
/* options.c */
/* Part of hhttppss, a simple HTTP server skeleton
* Author: Berke Durak
* Released in the public domain
*/
#include "options.h"
#include <getopt.h>
#include <stdlib.h>
char *options_usage = "[-d][-b <backlog>][-p <port>][-D <rootdir>]";
static char *option_string = "b:dp:D:";
/*(*** options_parse */
void options_parse(options *opt, int argc, char **argv, void (*bad_option)(char *msg, ...))
{
int c;
int x;
for(;;) {
c = getopt(argc, argv, option_string);
if(c < 0) break;
switch(c) {
case 'b': /* backlog */
x = atoi(optarg);
if(x < 0) bad_option("Backlog value %d negative", x);
opt->opt_backlog = x;
break;
case 'd': /* don't daemonize */
opt->opt_daemonize = 0;
break;
case 'p': /* port */
x = atoi(optarg);
if(x < 1 || x > 65535) bad_option("Port value %d out of range", x);
opt->opt_port = x;
break;
case 'D': /* root directory */
opt->opt_root = optarg;
break;
case '?':
default:
bad_option(NULL);
break;
}
}
}
/***)*/
/*(*** options_set_defaults */
void options_set_defaults(options *opt)
{
opt->opt_log_file = OPT_DEFAULT_LOG_FILE;
opt->opt_port = OPT_DEFAULT_PORT;
opt->opt_backlog = OPT_DEFAULT_BACKLOG;
opt->opt_daemonize = OPT_DEFAULT_DAEMONIZE;
opt->opt_root = OPT_DEFAULT_ROOT;
}
/***)*/