comparison src/c/driver.c @ 472:0f128cbc2758

Generated web servers use getopt()
author Adam Chlipala <adamc@hcoop.net>
date Sat, 08 Nov 2008 09:55:36 -0500
parents 1626dcba13ee
children 9babc5d2ec5a
comparison
equal deleted inserted replaced
471:20fab0e96217 472:0f128cbc2758
2 2
3 #include <string.h> 3 #include <string.h>
4 #include <sys/types.h> 4 #include <sys/types.h>
5 #include <sys/socket.h> 5 #include <sys/socket.h>
6 #include <netinet/in.h> 6 #include <netinet/in.h>
7 #include <unistd.h>
7 8
8 #include <pthread.h> 9 #include <pthread.h>
9 10
10 #include "urweb.h" 11 #include "urweb.h"
11 12
12 int uw_port = 8080;
13 int uw_backlog = 10; 13 int uw_backlog = 10;
14 int uw_bufsize = 1024; 14 int uw_bufsize = 1024;
15 15
16 typedef struct node { 16 typedef struct node {
17 int fd; 17 int fd;
296 // The skeleton for this function comes from Beej's sockets tutorial. 296 // The skeleton for this function comes from Beej's sockets tutorial.
297 int sockfd; // listen on sock_fd 297 int sockfd; // listen on sock_fd
298 struct sockaddr_in my_addr; 298 struct sockaddr_in my_addr;
299 struct sockaddr_in their_addr; // connector's address information 299 struct sockaddr_in their_addr; // connector's address information
300 int sin_size, yes = 1; 300 int sin_size, yes = 1;
301 int nthreads, i, *names; 301 int uw_port = 8080, nthreads = 1, i, *names, opt;
302 302
303 if (argc < 2) { 303 while ((opt = getopt(argc, argv, "p:t:")) != -1) {
304 fprintf(stderr, "No thread count specified\n"); 304 switch (opt) {
305 return 1; 305 case '?':
306 } 306 fprintf(stderr, "Unknown command-line option");
307 307 return 1;
308 nthreads = atoi(argv[1]); 308
309 if (nthreads <= 0) { 309 case 'p':
310 fprintf(stderr, "Invalid thread count\n"); 310 uw_port = atoi(optarg);
311 return 1; 311 if (uw_port <= 0) {
312 } 312 fprintf(stderr, "Invalid port number\n");
313 return 1;
314 }
315 break;
316
317 case 't':
318 nthreads = atoi(optarg);
319 if (nthreads <= 0) {
320 fprintf(stderr, "Invalid thread count\n");
321 return 1;
322 }
323 break;
324
325 default:
326 fprintf(stderr, "Unexpected getopt() behavior\n");
327 return 1;
328 }
329 }
330
313 names = calloc(nthreads, sizeof(int)); 331 names = calloc(nthreads, sizeof(int));
314 332
315 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking! 333 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
316 334
317 if (sockfd < 0) { 335 if (sockfd < 0) {