Commit e6c3ba1dd302e3eef3f9c27a1e9eed960a2433f3

Thomas de Grivel 2024-09-14T16:00:59

kc3_httpd -C /var/www/my.site -d

diff --git a/httpd/httpd.c b/httpd/httpd.c
index 4fb5d69..ff87be2 100644
--- a/httpd/httpd.c
+++ b/httpd/httpd.c
@@ -10,6 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <errno.h>
 #include <signal.h>
 #include <string.h>
 #include <sys/types.h>
@@ -23,24 +24,46 @@ int main (int argc, char **argv)
   bool    daemonize = true;
   s_ident daemonize_ident;
   s_tag   daemonize_value;
+  s32 e;
   char  log_buf[64] = {0};
   s32   log_fd = -1;
   s_str log_str = {0};
   s_call call = {0};
   const s_sym *module = NULL;
   char *p;
-  int r = 1;
+  s32 r = 1;
+  s32 skip = 0;
   time_t t;
   s_tag tmp = {0};
   const struct tm *utc = NULL;
   kc3_init(NULL, &argc, &argv);
-  if (argc > 0 && argv[0] && argv[0][0] == '-') {
+  while (argc > 0 && argv[0] && argv[0][0] == '-') {
+    skip = 1;
     p = argv[0] + 1;
     while (*p) {
       switch (*p) {
       case 'd':
         daemonize = false;
         break;
+      case 'C':
+        if (argc <= skip || ! argv[skip]) {
+          err_puts("kc3_httpd: -C without an argument");
+          assert(! "kc3_httpd: -C without an argument");
+          kc3_clean(NULL);
+          return 1;
+        }
+        if (chdir(argv[skip])) {
+          e = errno;
+          err_write_1("kc3_httpd: chdir: ");
+          err_write_1(argv[1]);
+          err_write_1(": ");
+          err_puts(strerror(e));
+          assert(! "kc3_httpd: chdir");
+          kc3_clean(NULL);
+          return 1;
+        }
+        skip++;
+        break;
       default:
         err_write_1("kc3_httpd: unknown flag: -");
         err_write_u8(*p);
@@ -51,8 +74,8 @@ int main (int argc, char **argv)
       }
       p++;
     }
-    argc--;
-    argv++;
+    argc -= skip;
+    argv += skip;
   }
   if (daemonize) {
     if ((t = time(NULL)) == -1) {
@@ -107,8 +130,10 @@ int main (int argc, char **argv)
     call.arguments = list_new_str_1(NULL, argv[0], NULL);
   else
     call.arguments = NULL;
-  if (! eval_call(&call, &tmp))
+  if (! eval_call(&call, &tmp)) {
+    err_puts("kc3_httpd: eval_call");
     goto clean;
+  }
   switch (tmp.type) {
   case TAG_U8:
     r = tmp.data.u8;
diff --git a/test/Makefile b/test/Makefile
index 574e036..e5b13c9 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -167,16 +167,16 @@ test_http_debug:
 	KC3S=${SRC_TOP}/kc3s/kc3s_debug time ./http_test
 
 test_httpd:
-	cd httpd && ../../httpd/.libs/kc3_httpd -d
+	${SRC_TOP}/httpd/.libs/kc3_httpd -C ${PWD}/httpd -d
 
 test_httpd_asan:
-	cd httpd && ../../httpd/.libs/kc3_httpd_asan -d
+	${SRC_TOP}/httpd/.libs/kc3_httpd_asan -C ${PWD}/httpd -d
 
 test_httpd_cov:
-	cd httpd && ../../httpd/.libs/kc3_httpd_cov -d
+	${SRC_TOP}/httpd/.libs/kc3_httpd_cov -C ${PWD}/httpd -d
 
 test_httpd_debug:
-	cd httpd && ../../httpd/.libs/kc3_httpd_debug -d
+	${SRC_TOP}/httpd/.libs/kc3_httpd_debug -C ${PWD}/httpd -d
 
 test_ikc3:
 	IKC3=${SRC_TOP}/ikc3/ikc3 time ./ikc3_test