-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.c
More file actions
61 lines (52 loc) · 1.16 KB
/
Copy pathoptions.c
File metadata and controls
61 lines (52 loc) · 1.16 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "options.h"
void options_usage()
{
extern char *__progname;
(void) fprintf(stderr, "usage: %s [-n nrequests] [-c nclients] http://<ip>[:<port>]/<path>\n", __progname);
exit(EXIT_FAILURE);
}
int options_parse(options_t *options, int argc, char **argv)
{
char *uri, hostname[256];
int port, opt, n, e;
port = 80;
options->nrequests = 10000;
options->nclients = 100;
while (1)
{
opt = getopt(argc, argv , "n:c:h");
if (opt == -1)
break;
switch(opt)
{
case 'n':
options->nrequests = strtol(optarg, NULL, 0);
break;
case 'c':
options->nclients = strtol(optarg, NULL, 0);
break;
case 'h':
default:
return -1;
}
}
argc -= optind;
argv += optind;
if (argc != 1)
return -1;
uri = argv[0];
n = sscanf(uri, "http://%255[^:]:%d%4095s", hostname, &port, options->path) - 3;
if (n)
n = sscanf(uri, "http://%255[^/]%4095s", hostname, options->path) - 2;
if (n)
return -1;
options->port = htons(port);
e = inet_pton(AF_INET, hostname, (void *) &options->ip);
if (e != 1)
return -1;
return 0;
}