full adaptation to c99 master
authorLaurent Mazet <mazet@softndesign.org>
Wed, 16 Apr 2025 19:38:44 +0000 (21:38 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 16 Apr 2025 19:38:44 +0000 (21:38 +0200)
file.c
http.c
makefile

diff --git a/file.c b/file.c
index 786a895c1b8d9c6b896c3aa921b908079f8847d9..376478a5782d1b4422ebb18f9d74ccd9abdff84c 100644 (file)
--- a/file.c
+++ b/file.c
@@ -13,6 +13,7 @@
 
 int readfile (char **buffer, char *filename)
 {
 
 int readfile (char **buffer, char *filename)
 {
+    int len = 0;
 
     /* open file */
 
 
     /* open file */
 
@@ -26,15 +27,17 @@ int readfile (char **buffer, char *filename)
     VERBOSE (DEBUG, PRINT ("Seek file size\n"));
     fseek (fd, 0, SEEK_END);
     int size = ftell (fd);
     VERBOSE (DEBUG, PRINT ("Seek file size\n"));
     fseek (fd, 0, SEEK_END);
     int size = ftell (fd);
-    fseek (fd, 0, SEEK_SET);  /* same as rewind(f); */
-
-    /* read full file */
-
-    VERBOSE (DEBUG, PRINT ("Reading %d bytes\n", size));
-    *buffer = calloc (size + 1, 1);
-    int len = fread (*buffer, 1, size, fd);
-    if (len != size) {
-        VERBOSE (WARNING, PRINT ("Can't read full file (%s)\n", filename));
+    if (size == -1) {
+        VERBOSE (WARNING, PRINT ("Can't read file (%s)\n", filename));
+    } else {
+        fseek (fd, 0, SEEK_SET);  /* same as rewind(f); */
+
+        /* read full file */
+        VERBOSE (DEBUG, PRINT ("Reading %d bytes\n", size));
+        *buffer = calloc (size + 1, 1);
+        if ((len = fread (*buffer, 1, size, fd)) != size) {
+            VERBOSE (WARNING, PRINT ("Can't read full file (%s)\n", filename));
+        }
     }
     fclose (fd);
 
     }
     fclose (fd);
 
diff --git a/http.c b/http.c
index 22b5cbcbbf901c169ca439bbcd45b97f837d1c8c..e2939c175c7d7bdb0226fa39d72a7c84b4e1f3f7 100644 (file)
--- a/http.c
+++ b/http.c
@@ -100,8 +100,7 @@ mime_t *find_mime_type (char *filename)
     }
 
     /* find mime */
     }
 
     /* find mime */
-    int i;
-    for (i = 0; i < NB_MIMES; i++) {
+    for (int i = 0; i < NB_MIMES; i++) {
         if (strcmp (ext, (mimes + i)->ext) == 0) {
             return mimes + i;
         }
         if (strcmp (ext, (mimes + i)->ext) == 0) {
             return mimes + i;
         }
@@ -144,11 +143,9 @@ void print_header_values (header_t *header)
 
 char *find_sequence (char *data, int len, char *seq, char **pdata)
 {
 
 char *find_sequence (char *data, int len, char *seq, char **pdata)
 {
-
     int size = strlen (seq);
 
     int size = strlen (seq);
 
-    int i;
-    for (i = 0; i < len - size + 1; i++) {
+    for (int i = 0; i < len - size + 1; i++) {
         if (strncmp (data + i, seq, size) == 0) {
             data[i] = 0;
             if (pdata != NULL) {
         if (strncmp (data + i, seq, size) == 0) {
             data[i] = 0;
             if (pdata != NULL) {
@@ -357,14 +354,20 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
 
     /* analyse uri */
     char *filename = strtok (uri, "&");
 
     /* analyse uri */
     char *filename = strtok (uri, "&");
+    VERBOSE (DEBUG, PRINT ("Filename: '%s'\n", filename));
     char *variables = strtok (NULL, "\n");
     char *variables = strtok (NULL, "\n");
+    VERBOSE (DEBUG, PRINT ("Variables: '%s'\n", variables));
     char *path = (char *) calloc (strlen (conf->root) + strlen (filename) + 2, 1);
     //sprintf (filename, "%s%s%s", conf->root, ((conf->root[strlen (conf->root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
     sprintf (path, "%s/%s", conf->root, filename);
 
     /* check header */
     header_t header = {0};
     char *path = (char *) calloc (strlen (conf->root) + strlen (filename) + 2, 1);
     //sprintf (filename, "%s%s%s", conf->root, ((conf->root[strlen (conf->root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
     sprintf (path, "%s/%s", conf->root, filename);
 
     /* check header */
     header_t header = {0};
-    while (strcmp (line = find_sequence (data, len + data - saved_data, "\r\n", &data), "") != 0) {
+    while (1) {
+        line = find_sequence (data, len + data - saved_data, "\r\n", &data);
+        if ((!line) || strcmp (line , "") != 0) {
+            break;
+        }
         VERBOSE (DEBUG, PRINT ("Header line: '%s'\n", line));
         char *field = strtok (line, ":");
         char *value = trim (strtok (NULL, "\r"));
         VERBOSE (DEBUG, PRINT ("Header line: '%s'\n", line));
         char *field = strtok (line, ":");
         char *value = trim (strtok (NULL, "\r"));
@@ -409,7 +412,6 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
         VERBOSE (WARNING, PRINT ("Incoherent size (%d <> %s)\n", len, header.content_length));
     }
     if (len > 0) {
         VERBOSE (WARNING, PRINT ("Incoherent size (%d <> %s)\n", len, header.content_length));
     }
     if (len > 0) {
-        int i;
         char *fcomp= NULL;
         char *fdecomp = NULL;
         char *command = NULL;
         char *fcomp= NULL;
         char *fdecomp = NULL;
         char *command = NULL;
@@ -424,7 +426,7 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
             command = createcommand ("gunzip %s", fcomp);
             system (command);
             fdecomp = strdup (fcomp);
             command = createcommand ("gunzip %s", fcomp);
             system (command);
             fdecomp = strdup (fcomp);
-            for (i = strlen (fdecomp) - 1; i > 0; i--) {
+            for (int i = strlen (fdecomp) - 1; i > 0; i--) {
                 if (fdecomp[i] == '.') {
                     fdecomp[i] = 0;
                     break;
                 if (fdecomp[i] == '.') {
                     fdecomp[i] = 0;
                     break;
@@ -439,7 +441,7 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
             command = createcommand ("compress %s", fcomp);
             system (command);
             fdecomp = strdup (fcomp);
             command = createcommand ("compress %s", fcomp);
             system (command);
             fdecomp = strdup (fcomp);
-            for (i = strlen (fdecomp) - 1; i > 0; i--) {
+            for (int i = strlen (fdecomp) - 1; i > 0; i--) {
                 if (fdecomp[i] == '.') {
                     fdecomp[i] = 0;
                     break;
                 if (fdecomp[i] == '.') {
                     fdecomp[i] = 0;
                     break;
index 355e64fc8aada0713bf5a230b4591b04cfb075e0..09ecb8bc309abe5c9bba31812b7dfecc3743bcb9 100644 (file)
--- a/makefile
+++ b/makefile
@@ -2,8 +2,10 @@
 
 CC = gcc
 
 
 CC = gcc
 
-INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
+#INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
+INCLUDES =
 OFLAGS  = -O4 -Os
 OFLAGS  = -O4 -Os
+#OFLAGS  = -O0
 #OFLAGS  = -O4 -ffast-math -finline-functions
 #OFLAGS  = -O4 -finline-functions
 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
 #OFLAGS  = -O4 -ffast-math -finline-functions
 #OFLAGS  = -O4 -finline-functions
 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
@@ -12,17 +14,30 @@ OFLAGS  = -O4 -Os
 CFLAGS += -W -Wall -Wextra -g
 #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
 CFLAGS += -W -Wall -Wextra -g
 #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
-LDFLAGS += -g $(OPTIONS)
-
-ifeq ($(OS),Windows_NT)
-LDLIBS += -lws2_32
+LDFLAGS += -g $(LDOPTS) $(OPTIONS)
+
+LDOPT = linker
+MV = mv
+ifneq (, $(findstring linux, $(MAKE_HOST)))
+# Linux
+else ifneq (, $(findstring mingw, $(MAKE_HOST)))
+# Windows MinGw
+CFLAGS += -DWIN32
+#LDLIBS += -lws2_32
+LDOPT = winlnk
+else ifneq (, $(findstring cygwin, $(MAKE_HOST)))
+CFLAGS += -DWIN32
+# Windows CygWin
+LDOPT = winlnk
+else ifneq (, $(findstring msdos, $(MAKE_HOST)))
+# MSDOS
+LDOPT = doslnk
+MV = move
 endif
 
 # Targets
 
 endif
 
 # Targets
 
-ALLEXE  =
-ALLEXE += webserver
-#ALLEXE += skel
+ALLEXE  = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done)
 
 SHELL = bash
 
 
 SHELL = bash
 
@@ -43,18 +58,26 @@ INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1)
 
 VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; }
 
 
 VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; }
 
+GETCOMMENTS = awk '/\/\*\s*$(1):/,/\*\// { sub(/.*\/\*\s*$(1):/, ""); sub (/\s*\*\/.*/, ""); print } /\/\/\s*$(1):/ {sub (/.*\/\/\s*$(1):/, ""); print }' $(2)
+#GETCOMMENTS = perl -- getcomments.pl -p='$(1):\s' -f='%' $(2)
+
 ## Generic rules
 
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
 ## Generic rules
 
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
+analyze:
+       make purge
+       scan-build make
+       #scan-build -stats make
+
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
 clean:
        $(call TITLE, "Cleaning")
        touch clean
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
 clean:
        $(call TITLE, "Cleaning")
        touch clean
-       rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_* gmon.out)
+       rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_* gmon.out _)
        $(call PASS, SUCCESS)
 
 depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
        $(call PASS, SUCCESS)
 
 depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
@@ -71,7 +94,8 @@ purge: clean
        rm -f purge $(ALLEXE:%=%.exe)
        $(call PASS, SUCCESS)
 
        rm -f purge $(ALLEXE:%=%.exe)
        $(call PASS, SUCCESS)
 
-valgrinds: all
+valgrinds:
+       $(MAKE) all
        $(MAKE) $(addprefix valgrind_,$(ALLEXE))
 
 wipe: purge
        $(MAKE) $(addprefix valgrind_,$(ALLEXE))
 
 wipe: purge
@@ -80,7 +104,9 @@ wipe: purge
        rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog)
        $(call PASS, SUCCESS)
 
        rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog)
        $(call PASS, SUCCESS)
 
-tests: all
+tests:
+       -rm -f $(ALLEXE)
+       $(MAKE) all
        $(MAKE) $(addprefix test_,$(ALLEXE))
 
 ## Main rules
        $(MAKE) $(addprefix test_,$(ALLEXE))
 
 ## Main rules
@@ -90,13 +116,14 @@ include $(wildcard *.ld)
 
 gcov_%:
        $(MAKE) purge
 
 gcov_%:
        $(MAKE) purge
-       OPTIONS="-coverage -O0" $(MAKE)
+       $(MAKE) depends
+       OPTIONS="-coverage -O0" $(MAKE) ${@:gcov_%=%}.exe
        $(MAKE) test_$(@:gcov_%=%)
        gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
        touch gcov
        rm -f gcov $(wildcard *.gcda *.gcno)
        $(MAKE) purge
        $(MAKE) test_$(@:gcov_%=%)
        gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
        touch gcov
        rm -f gcov $(wildcard *.gcda *.gcno)
        $(MAKE) purge
-       grep '#####' *.c.gcov || true
+       grep '^ *#####' *.c.gcov || true
 
 gprof_%:
        $(MAKE) purge
 
 gprof_%:
        $(MAKE) purge
@@ -119,9 +146,9 @@ gprof_%:
 
 %.test: %.c
        $(call TITLE, "Building $@")
 
 %.test: %.c
        $(call TITLE, "Building $@")
-#      awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
-       perl -- getcomments.pl -p='test:\s' -f='%' $< > $@
+       $(call GETCOMMENTS,test, $<) > $@
        $(call PASS, SUCCESS)
        $(call PASS, SUCCESS)
+       -rm -f _
 
 test_%: %.test %.exe
        IFS=$$'\n'; RC=0; \
 
 test_%: %.test %.exe
        IFS=$$'\n'; RC=0; \
@@ -130,6 +157,7 @@ test_%: %.test %.exe
          eval $$test; \
          [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
                        || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
          eval $$test; \
          [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
                        || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
+         test "$$RC" = 1 -a "$(STOP)" = 1 && break; \
        done; \
        test "$$RC" -ne 1
 
        done; \
        test "$$RC" -ne 1
 
@@ -141,29 +169,29 @@ valgrind_%: %.exe
 %.d: %.c
        $(call TITLE, "Building $@")
        $(CC) $(INCLUDES) -MM $< -o $@~
 %.d: %.c
        $(call TITLE, "Building $@")
        $(CC) $(INCLUDES) -MM $< -o $@~
-       echo ${<:.c=.o}: $(shell perl -- getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
-       mv $@~ $@
+       echo ${<:.c=.o}: $(shell $(call GETCOMMENTS,depends, $<)) >> $@~
+       $(MV) $@~ $@
        $(call PASS, SUCCESS)
 
 %.ld: %.c
        $(call TITLE, "Building $@")
        $(call PASS, SUCCESS)
 
 %.ld: %.c
        $(call TITLE, "Building $@")
-       echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
+       echo ${<:.c=.exe}: $(shell $(call GETCOMMENTS,$(LDOPT), $<) | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
        $(call PASS, SUCCESS)
 
 %.o: %.c
        $(call TITLE, "Building $@")
        $(call PASS, SUCCESS)
 
 %.o: %.c
        $(call TITLE, "Building $@")
-       $(CC) $(CFLAGS) $(INCLUDES) $(shell perl -- getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
+       $(CC) $(CFLAGS) $(INCLUDES) $(shell $(call GETCOMMENTS,cflags, $<)) -c $< -o $@
        $(call PASS, SUCCESS)
 
 
 %.exe: %.o %.d
        $(call TITLE, "Building $@")
        $(call PASS, SUCCESS)
 
 
 %.exe: %.o %.d
        $(call TITLE, "Building $@")
-       $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@
+       $(CC) $(LDFLAGS) $< $(shell $(call GETCOMMENTS,$(LDOPT), ${<:.o=.c})) $(LDLIBS) -o $@
        $(call PASS, SUCCESS)
 
 ## Phony
 
        $(call PASS, SUCCESS)
 
 ## Phony
 
-.PHONY: all clean count depends gcovs purge tests
+.PHONY: all analyze clean count depends gcovs purge tests
 
 ## Precious
 
 
 ## Precious