full adaptation to c99 master
authorLaurent Mazet <mazet@softndesign.org>
Sun, 13 Apr 2025 19:00:30 +0000 (21:00 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 13 Apr 2025 19:00:30 +0000 (21:00 +0200)
battleships.c
board.c
display.c
log.c
makefile

index 8304caf8ee90e11803e6ed7ae44d6588de161966..74539c1ed7ca9cea05e100ea2faaadd6c4421f3c 100644 (file)
@@ -168,16 +168,14 @@ int main (int argc, char *argv[])
         case KEY_UP:
         case 'i':
             if (length > 0) {
-                int t;
-                for (t = y - 1; t >= 0; t--) {
+                for (int t = y - 1; t >= 0; t--) {
                     if (testlocation (boardhuman, length, x, t, orient, " ")) {
                         y = t;
                         break;
                     }
                 }
             } else {
-                int t;
-                for (t = y - 1; t >= 0; t--) {
+                for (int t = y - 1; t >= 0; t--) {
                     if (testlocation (boardcomputer, 1, x, t, orient, " S")) {
                         y = t;
                         break;
@@ -188,16 +186,14 @@ int main (int argc, char *argv[])
         case KEY_LEFT:
         case 'j':
             if (length > 0) {
-                int t;
-                for (t = x - 1; t >= 0; t--) {
+                for (int t = x - 1; t >= 0; t--) {
                     if (testlocation (boardhuman, length, t, y, orient, " ")) {
                         x = t;
                         break;
                     }
                 }
             } else {
-                int t;
-                for (t = x - 1; t >= 0; t--) {
+                for (int t = x - 1; t >= 0; t--) {
                     if (testlocation (boardcomputer, 1, t, y, orient, " S")) {
                         x = t;
                         break;
@@ -208,16 +204,14 @@ int main (int argc, char *argv[])
         case KEY_DOWN:
         case 'k':
             if (length > 0) {
-                int t;
-                for (t = y + 1; t < ysize; t++) {
+                for (int t = y + 1; t < ysize; t++) {
                     if (testlocation (boardhuman, length, x, t, orient, " ")) {
                         y = t;
                         break;
                     }
                 }
             } else {
-                int t;
-                for (t = y + 1; t < ysize; t++) {
+                for (int t = y + 1; t < ysize; t++) {
                     if (testlocation (boardcomputer, 1, x, t, orient, " S")) {
                         y = t;
                         break;
@@ -228,16 +222,14 @@ int main (int argc, char *argv[])
         case KEY_RIGHT:
         case 'l':
             if (length > 0) {
-                int t;
-                for (t = x + 1; t < xsize; t++) {
+                for (int t = x + 1; t < xsize; t++) {
                     if (testlocation (boardhuman, length, t, y, orient, " ")) {
                         x = t;
                         break;
                     }
                 }
             } else {
-                int t;
-                for (t = x + 1; t < xsize; t++) {
+                for (int t = x + 1; t < xsize; t++) {
                     if (testlocation (boardcomputer, 1, t, y, orient, " S")) {
                         x = t;
                         break;
diff --git a/board.c b/board.c
index f5663942c5b0fe6585ffd467050612f34f700e74..abf2f423c25c49804feaca5b8712bcb48213ae36 100644 (file)
--- a/board.c
+++ b/board.c
@@ -44,8 +44,7 @@ int testlocation (board_t *board, int length, int x, int y, int orient, char *sy
     }
 
     int ret = 1;
-    int i;
-    for (i = 0; i < length; i++) {
+    for (int i = 0; i < length; i++) {
         if (!isoneof (board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize], symbs)) {
             ret = 0;
             break;
@@ -76,8 +75,7 @@ int findlocation (board_t *board, int length, int *x, int *y, int *orient, char
 
 void putlocation (board_t *board, int length, int x, int y, int orient, char symb)
 {
-    int i;
-    for (i = 0; i < length; i++) {
+    for (int i = 0; i < length; i++) {
         board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize] = symb;
     }
 }
@@ -117,8 +115,7 @@ int drawbomb (board_t *board, int x, int y)
 int testsunk (board_t *board)
 {
     int ret = 1;
-    int i;
-    for (i = 0; i < board->xsize * board->ysize; i++) {
+    for (int i = 0; i < board->xsize * board->ysize; i++) {
         if (board->tab[i] == 'S') {
             ret = 0;
             break;
index c0302d33fc0f6f1867f27a2c8d4268263360e33e..2b9e91cdfe04b017dc873567edac201a04873a47 100644 (file)
--- a/display.c
+++ b/display.c
@@ -32,10 +32,8 @@ void setcolor (color_t color)
 
 void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show)
 {
-    int x, y;
-
-    for (x = -1; x <= board->xsize; x++) {
-        for (y = -1; y <= board->ysize; y++) {
+    for (int x = -1; x <= board->xsize; x++) {
+        for (int y = -1; y <= board->ysize; y++) {
             int c = ' ';
             if ((x == -1) && (y == -1)) {
                 c = ACS_ULCORNER;
@@ -89,7 +87,6 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show)
 
 void displayelement (int length, int x, int y, int orient, char symb, int show)
 {
-    int i;
     switch (symb) {
     case 'S':
         setcolor (yellow);
@@ -99,7 +96,7 @@ void displayelement (int length, int x, int y, int orient, char symb, int show)
         break;
     }
     char c = (show) ? symb : ' ';
-    for (i = 0; i < length; i++) {
+    for (int i = 0; i < length; i++) {
         mvaddch (y + (orient ^ 1) * i, x + orient * i, c);
     }
     setcolor (white);
diff --git a/log.c b/log.c
index 3f2903c8e61585a9295808aaf3dabcc8f93a1432..689163bbd558ea8ac089161b877cdd5d6efd42c8 100644 (file)
--- a/log.c
+++ b/log.c
@@ -6,11 +6,9 @@
 
 log_t *initlog (int width, int height)
 {
-    int i;
-
     log_t *logs = (log_t *) malloc (sizeof (log_t));
     logs->buffer = (char **) malloc (height * sizeof (char *));
-    for (i = 0; i < height; i++) {
+    for (int i = 0; i < height; i++) {
         logs->buffer[i] = (char *) calloc (1, width + 1);
     }
     logs->width = width;
@@ -21,10 +19,9 @@ log_t *initlog (int width, int height)
 
 void freelog (log_t *logs)
 {
-    int i;
     if (logs) {
         if (logs->buffer) {
-            for (i = 0; i < logs->height; i++) {
+            for (int i = 0; i < logs->height; i++) {
                 free (logs->buffer[i]);
             }
             free (logs->buffer);
@@ -35,7 +32,7 @@ void freelog (log_t *logs)
 
 void displaylog (log_t *logs, char *msg, int xoffset, int yoffset)
 {
-    int i, j;
+    int i;
 
     for (i = 0; i < logs->height; i++) {
         if (*logs->buffer[i] == '\0') {
@@ -58,7 +55,7 @@ void displaylog (log_t *logs, char *msg, int xoffset, int yoffset)
     for (i = 0; i < logs->width; i++) {
         mvaddch (yoffset - 1, xoffset + i, ACS_HLINE);
         mvaddch (yoffset + logs->height, xoffset + i, ACS_HLINE);
-        for (j = 0; j < logs->height; j++) {
+        for (int j = 0; j < logs->height; j++) {
             mvaddch (yoffset + j, xoffset + i, ' ');
         }
     }
index 102bd93a1c3938051631d097a9a9276183fde9f1..09ecb8bc309abe5c9bba31812b7dfecc3743bcb9 100644 (file)
--- a/makefile
+++ b/makefile
@@ -5,6 +5,7 @@ CC = gcc
 #INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
 INCLUDES =
 OFLAGS  = -O4 -Os
+#OFLAGS  = -O0
 #OFLAGS  = -O4 -ffast-math -finline-functions
 #OFLAGS  = -O4 -finline-functions
 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
@@ -13,19 +14,30 @@ OFLAGS  = -O4 -Os
 CFLAGS += -W -Wall -Wextra -g
 #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
-LDFLAGS += -g $(OPTIONS)
+LDFLAGS += -g $(LDOPTS) $(OPTIONS)
 
 LDOPT = linker
-ifeq ($(OS),Windows_NT)
+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
 
-ALLEXE  =
-ALLEXE += battleships
+ALLEXE  = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done)
 
 SHELL = bash
 
@@ -46,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; }
 
+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)
 
+analyze:
+       make purge
+       scan-build make
+       #scan-build -stats make
+
 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))
@@ -74,7 +94,8 @@ purge: clean
        rm -f purge $(ALLEXE:%=%.exe)
        $(call PASS, SUCCESS)
 
-valgrinds: all
+valgrinds:
+       $(MAKE) all
        $(MAKE) $(addprefix valgrind_,$(ALLEXE))
 
 wipe: purge
@@ -83,7 +104,9 @@ wipe: purge
        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
@@ -93,13 +116,14 @@ include $(wildcard *.ld)
 
 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
-       grep '#####' *.c.gcov || true
+       grep '^ *#####' *.c.gcov || true
 
 gprof_%:
        $(MAKE) purge
@@ -122,9 +146,9 @@ gprof_%:
 
 %.test: %.c
        $(call TITLE, "Building $@")
-#      awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
-       perl -- getcomments.pl -p='test:\s' -f='%' $< > $@
+       $(call GETCOMMENTS,test, $<) > $@
        $(call PASS, SUCCESS)
+       -rm -f _
 
 test_%: %.test %.exe
        IFS=$$'\n'; RC=0; \
@@ -133,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; }; \
+         test "$$RC" = 1 -a "$(STOP)" = 1 && break; \
        done; \
        test "$$RC" -ne 1
 
@@ -144,29 +169,29 @@ valgrind_%: %.exe
 %.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 $@")
-       echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='$(LDOPT):\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 $@")
-       $(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 $@")
-       $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='$(LDOPT):\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@
+       $(CC) $(LDFLAGS) $< $(shell $(call GETCOMMENTS,$(LDOPT), ${<:.o=.c})) $(LDLIBS) -o $@
        $(call PASS, SUCCESS)
 
 ## Phony
 
-.PHONY: all clean count depends gcovs purge tests
+.PHONY: all analyze clean count depends gcovs purge tests
 
 ## Precious