changeset 472:0f128cbc2758

Generated web servers use getopt()
author Adam Chlipala <adamc@hcoop.net>
date Sat, 08 Nov 2008 09:55:36 -0500
parents 20fab0e96217
children 04b91c33ef54
files src/c/driver.c
diffstat 1 files changed, 28 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/driver.c	Thu Nov 06 19:43:48 2008 -0500
+++ b/src/c/driver.c	Sat Nov 08 09:55:36 2008 -0500
@@ -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 @@
   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;
+
+    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;
+    }
   }
 
-  nthreads = atoi(argv[1]);
-  if (nthreads <= 0) {
-    fprintf(stderr, "Invalid thread count\n");
-    return 1;
-  }
   names = calloc(nthreads, sizeof(int));
 
   sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!