diff options
author | 2008-11-08 09:55:36 -0500 | |
---|---|---|
committer | 2008-11-08 09:55:36 -0500 | |
commit | 001d4ac1ca69ff58706f007d079e28ff24ab252f (patch) | |
tree | af15ca989d28ff4dd9d4e7c5edc7d4e90b3b2501 /src/c/driver.c | |
parent | 50e456effc2cad22d6a61e08887816096c427658 (diff) |
Generated web servers use getopt()
Diffstat (limited to 'src/c/driver.c')
-rw-r--r-- | src/c/driver.c | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/c/driver.c b/src/c/driver.c index 1eef9742..be5d7b6c 100644 --- a/src/c/driver.c +++ b/src/c/driver.c @@ -4,12 +4,12 @@ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> +#include <unistd.h> #include <pthread.h> #include "urweb.h" -int uw_port = 8080; int uw_backlog = 10; int uw_bufsize = 1024; @@ -298,18 +298,36 @@ int main(int argc, char *argv[]) { struct sockaddr_in my_addr; struct sockaddr_in their_addr; // connector's address information int sin_size, yes = 1; - int nthreads, i, *names; + int uw_port = 8080, nthreads = 1, i, *names, opt; + + while ((opt = getopt(argc, argv, "p:t:")) != -1) { + switch (opt) { + case '?': + fprintf(stderr, "Unknown command-line option"); + return 1; - if (argc < 2) { - fprintf(stderr, "No thread count specified\n"); - return 1; - } + case 'p': + uw_port = atoi(optarg); + if (uw_port <= 0) { + fprintf(stderr, "Invalid port number\n"); + return 1; + } + break; - nthreads = atoi(argv[1]); - if (nthreads <= 0) { - fprintf(stderr, "Invalid thread count\n"); - return 1; + case 't': + nthreads = atoi(optarg); + if (nthreads <= 0) { + fprintf(stderr, "Invalid thread count\n"); + return 1; + } + break; + + default: + fprintf(stderr, "Unexpected getopt() behavior\n"); + return 1; + } } + names = calloc(nthreads, sizeof(int)); sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking! |