add temporary directory
[webserver.git] / webserver.c
index 2f5422771c8379dc5e06cb09243541979a5722d5..bca0262579bdec1c6ffa686ed7b9d08331f38f28 100644 (file)
@@ -6,9 +6,11 @@
 #include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "debug.h"
+#include "file.h"
 #include "http.h"
 #include "server.h"
 
@@ -18,6 +20,7 @@
 
 #define BUFFER_SIZE 4096
 #define ROOT_DIR "webroot"
+#define TEMP_DIR "tmp"
 #define SERVER_NAME "localhost"
 #define CHARSET "iso-8859-1"
 
@@ -28,6 +31,7 @@
 char *progname = NULL;
 int port = 8080;
 char *root = ROOT_DIR;
+char *temp = TEMP_DIR;
 char *servername = SERVER_NAME;
 char *charset = CHARSET;
 
@@ -42,6 +46,7 @@ int usage (int ret)
     fprintf (fid, " -p : port number (%d)\n", port);
     fprintf (fid, " -r : web root directory (%s)\n", root);
     fprintf (fid, " -s : server name (%s)\n", servername);
+    fprintf (fid, " -t : temporay directory (%s)\n", temp);
     fprintf (fid, " -v : verbose level (%d)\n", verbose);
 
     return ret;
@@ -95,6 +100,14 @@ int main (int argc, char *argv[])
                 return 1;
             }
             break;
+        case 'r':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1));
+                return 1;
+            }
+            root = arg;
+            break;
         case 's':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -103,13 +116,13 @@ int main (int argc, char *argv[])
             }
             servername = arg;
             break;
-        case 'r':
+        case 't':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
                 VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1));
                 return 1;
             }
-            root = arg;
+            temp = arg;
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
@@ -125,17 +138,37 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* init seed */
+    srand (time (NULL));
+
     /* check root directory */
     VERBOSE (DEBUG, PRINT ("Check web root\n"));
-    DIR *pdir = opendir (root);
-    if (pdir == NULL) {
+    DIR *prootdir = opendir (root);
+    if (prootdir == NULL) {
         VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root));
         return 1;
     }
-    closedir (pdir);
+    closedir (prootdir);
+
+    /* check temp directory */
+    VERBOSE (DEBUG, PRINT ("Check temp dirweb root\n"));
+    DIR *ptempdir = opendir (temp);
+    if (ptempdir == NULL) {
+        VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", temp));
+        return 1;
+    }
+    char *ntemp = tempname (temp);
+    FILE *ftemp = fopen (ntemp, "w");
+    if (ftemp == NULL) {
+        VERBOSE (ERROR, PERROR ("Can't write temporary file (%s)\n", ntemp));
+        return 1;
+    }
+    fclose (ftemp);
+    unlink (ntemp);
+    free (ntemp);
 
     /* configuration */
-    conf_t conf = {root, servername, charset};
+    conf_t conf = {root, temp, servername, charset};
 
     /* init network stack */
     VERBOSE (DEBUG, PRINT ("Initializing socket\n"));