tinywebd

annotate src/main.c @ 17:2874f61a43b1

implementing the directory index generation
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Apr 2015 04:33:02 +0300
parents 0244b08cc9d3
children
rev   line source
nuclear@12 1 /* tinyweb - tiny web server library and daemon
nuclear@12 2 * Author: John Tsiombikas <nuclear@member.fsf.org>
nuclear@12 3 *
nuclear@12 4 * This program is placed in the public domain. Feel free to use it any
nuclear@12 5 * way you like. Mentions and retaining this attribution header will be
nuclear@12 6 * appreciated, but not required.
nuclear@12 7 */
nuclear@0 8 #include <stdio.h>
nuclear@0 9 #include <stdlib.h>
nuclear@0 10 #include <string.h>
nuclear@7 11 #include <errno.h>
nuclear@3 12 #include <signal.h>
nuclear@7 13 #include "tinyweb.h"
nuclear@1 14
nuclear@7 15 int parse_args(int argc, char **argv);
nuclear@3 16 void sighandler(int s);
nuclear@5 17
nuclear@5 18
nuclear@0 19 int main(int argc, char **argv)
nuclear@0 20 {
nuclear@7 21 int *sockets, num_sockets, sockets_arr_size = 0;
nuclear@7 22
nuclear@1 23 if(parse_args(argc, argv) == -1) {
nuclear@1 24 return 1;
nuclear@1 25 }
nuclear@1 26
nuclear@3 27 signal(SIGINT, sighandler);
nuclear@3 28 signal(SIGTERM, sighandler);
nuclear@3 29 signal(SIGQUIT, sighandler);
nuclear@3 30
nuclear@9 31 if(tw_start() == -1) {
nuclear@9 32 return 1;
nuclear@9 33 }
nuclear@0 34
nuclear@0 35 for(;;) {
nuclear@7 36 int i;
nuclear@0 37 fd_set rdset;
nuclear@0 38
nuclear@7 39 num_sockets = tw_get_sockets(0);
nuclear@7 40 if(num_sockets > sockets_arr_size) {
nuclear@7 41 int newsz = sockets_arr_size ? sockets_arr_size * 2 : 16;
nuclear@7 42 int *newarr = realloc(sockets, newsz * sizeof *sockets);
nuclear@7 43 if(!newarr) {
nuclear@7 44 fprintf(stderr, "failed to allocate sockets array\n");
nuclear@7 45 tw_stop();
nuclear@7 46 return 1;
nuclear@7 47 }
nuclear@7 48 sockets = newarr;
nuclear@7 49 sockets_arr_size = newsz;
nuclear@7 50 }
nuclear@7 51 tw_get_sockets(sockets);
nuclear@7 52
nuclear@0 53 FD_ZERO(&rdset);
nuclear@7 54 for(i=0; i<num_sockets; i++) {
nuclear@7 55 FD_SET(sockets[i], &rdset);
nuclear@0 56 }
nuclear@0 57
nuclear@7 58 while(select(tw_get_maxfd() + 1, &rdset, 0, 0, 0) == -1 && errno == EINTR);
nuclear@0 59
nuclear@7 60 for(i=0; i<num_sockets; i++) {
nuclear@7 61 if(FD_ISSET(sockets[i], &rdset)) {
nuclear@7 62 tw_handle_socket(sockets[i]);
nuclear@0 63 }
nuclear@0 64 }
nuclear@0 65 }
nuclear@3 66
nuclear@3 67 return 0; /* unreachable */
nuclear@0 68 }
nuclear@0 69
nuclear@3 70 void sighandler(int s)
nuclear@3 71 {
nuclear@3 72 if(s == SIGINT || s == SIGTERM || s == SIGQUIT) {
nuclear@7 73 tw_stop();
nuclear@3 74 printf("bye!\n");
nuclear@3 75 exit(0);
nuclear@3 76 }
nuclear@3 77 }
nuclear@3 78
nuclear@1 79
nuclear@1 80 static void print_help(const char *argv0)
nuclear@1 81 {
nuclear@1 82 printf("Usage: %s [options]\n", argv0);
nuclear@1 83 printf("Options:\n");
nuclear@1 84 printf(" -p <port> set the TCP/IP port number to use\n");
nuclear@1 85 printf(" -h print usage help and exit\n");
nuclear@1 86 }
nuclear@1 87
nuclear@1 88 int parse_args(int argc, char **argv)
nuclear@1 89 {
nuclear@1 90 int i;
nuclear@1 91
nuclear@1 92 for(i=1; i<argc; i++) {
nuclear@1 93 if(argv[i][0] == '-' && argv[i][2] == 0) {
nuclear@1 94 switch(argv[i][1]) {
nuclear@1 95 case 'p':
nuclear@7 96 {
nuclear@7 97 int port = atoi(argv[++i]);
nuclear@7 98 if(!port) {
nuclear@7 99 fprintf(stderr, "-p must be followed by a valid port number\n");
nuclear@7 100 return -1;
nuclear@7 101 }
nuclear@7 102 tw_set_port(port);
nuclear@1 103 }
nuclear@1 104 break;
nuclear@1 105
nuclear@8 106 case 'c':
nuclear@8 107 if(tw_set_root(argv[++i]) == -1) {
nuclear@8 108 return -1;
nuclear@8 109 }
nuclear@8 110 break;
nuclear@8 111
nuclear@1 112 case 'h':
nuclear@1 113 print_help(argv[0]);
nuclear@1 114 exit(0);
nuclear@1 115
nuclear@1 116 default:
nuclear@1 117 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
nuclear@1 118 return -1;
nuclear@1 119 }
nuclear@1 120 } else {
nuclear@1 121 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
nuclear@1 122 return -1;
nuclear@1 123 }
nuclear@0 124 }
nuclear@0 125 return 0;
nuclear@0 126 }