From c5e1cb8c69d62a6ff64cb06d7f263f9c274cb4de Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Sat, 8 Nov 2008 09:55:36 -0500 Subject: Generated web servers use getopt() --- src/c/driver.c | 38 ++++++++++++++++++++++++++++---------- 1 file 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 #include #include +#include #include #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! -- cgit v1.2.3