-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
172 lines (148 loc) · 3.64 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*-
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return.
*/
#define _POSIX_C_SOURCE 200809
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "options.h"
#define GOPHERPORT "70"
void usage(void);
struct opt_options {
char *host;
char *port;
char *root;
};
struct opt_options *opt_parse(int argc, char **argv)
{
assert(argc > 0);
assert(argv != NULL && *argv != NULL);
struct opt_options *options = malloc(sizeof(struct opt_options));
if (options == NULL) {
syslog(LOG_ERR, "malloc error: %m");
fprintf(stderr, "malloc options: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
options->root = NULL;
options->host = NULL;
options->port = NULL;
int opt;
while ((opt = getopt(argc, argv, "r:H:p:h")) != -1) {
switch (opt){
case 'r':
free(options->root);
options->root = realpath(optarg, NULL);
if (options->root == NULL) {
syslog(LOG_ERR, "realpath error: %m");
fprintf(stderr, "realpath options->root: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
break;
case 'H':
free(options->host);
options->host = strdup(optarg);
if (options->host == NULL) {
syslog(LOG_ERR, "strdup error: %m");
fprintf(stderr, "strdup options->host: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
break;
case 'p':
free(options->port);
options->port = strdup(optarg);
if (options->port == NULL) {
syslog(LOG_ERR, "strdup error: %m");
fprintf(stderr, "strdup options->port: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
default:
syslog(LOG_NOTICE, "unknown option: %s",
argv[optind-1]);
usage();
exit(EXIT_FAILURE);
}
}
if (options->root == NULL) {
options->root = realpath(".", NULL);
if (options->root == NULL) {
syslog(LOG_ERR, "realpath error: %m");
fprintf(stderr, "realpath options->root: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
if (options->host == NULL) {
long hnlen = sysconf(_SC_HOST_NAME_MAX);
options->host = malloc(hnlen+1);
if (options->host == NULL) {
syslog(LOG_ERR, "malloc error: %m");
fprintf(stderr, "malloc options->host: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
gethostname(options->host, hnlen);
}
if (options->port == NULL) {
options->port = strdup(GOPHERPORT);
if (options->port == NULL) {
syslog(LOG_ERR, "strdup error: %m");
fprintf(stderr, "strdup options->port: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
syslog(LOG_DEBUG, "options->root: \"%s\"", options->root);
syslog(LOG_DEBUG, "options->host: \"%s\"", options->host);
syslog(LOG_DEBUG, "options->port: \"%s\"", options->port);
return (options);
}
void
opt_free(struct opt_options *options)
{
assert(options != NULL);
free(options->root);
free(options->host);
free(options->port);
free(options);
}
char *
opt_get_host(struct opt_options *options)
{
assert(options != NULL);
assert(options->host != NULL);
return (options->host);
}
char *
opt_get_root(struct opt_options *options)
{
assert(options != NULL);
assert(options->root != NULL);
return (options->root);
}
char *
opt_get_port(struct opt_options *options)
{
assert(options != NULL);
assert(options->port != NULL);
return (options->port);
}
void
usage(void)
{
fputs("Usage: mgopherd -r root -H host -p port\n", stderr);
fputs(" mgopherd -h\n", stderr);
}