full adaptation to c99 master
authorLaurent Mazet <mazet@softndesign.org>
Tue, 15 Apr 2025 08:14:23 +0000 (10:14 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 15 Apr 2025 08:14:23 +0000 (10:14 +0200)
color.c
display.c
image.c
image.h
makefile
ndisplay.c
pnm.c

diff --git a/color.c b/color.c
index c8385c80f132428f435f7672df1d80d922b5d4e9..e49fea8d2448c043010f85f218276cbfd5960a32 100644 (file)
--- a/color.c
+++ b/color.c
@@ -309,8 +309,7 @@ color_t *_findcolor (float red, float green, float blue, color_t *colors, int nb
 {
     float dist = 3;
     color_t *color = NULL;
 {
     float dist = 3;
     color_t *color = NULL;
-    int i;
-    for (i = 0; i < nbcolors; i++) {
+    for (int i = 0; i < nbcolors; i++) {
         float r = (colors + i)->red - red;
         float g = (colors + i)->green - green;
         float b = (colors + i)->blue - blue;
         float r = (colors + i)->red - red;
         float g = (colors + i)->green - green;
         float b = (colors + i)->blue - blue;
index b818bd73fca948e0b4a2bd6144764d1853c4e5b3..ae4d96823d8d74a6181d58b67d79de541bff4e47 100644 (file)
--- a/display.c
+++ b/display.c
@@ -129,9 +129,8 @@ int main (int argc, char *argv[])
         return 1;
     }
 
         return 1;
     }
 
-    int k, l;
-    for (l = 0; l < image->height; l++) {
-        for (k = 0; k < image->width; k++) {
+    for (int l = 0; l < image->height; l++) {
+        for (int k = 0; k < image->width; k++) {
             int ind = k + image->width * l;
             color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), colormap);
             switch (colormap) {
             int ind = k + image->width * l;
             color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), colormap);
             switch (colormap) {
diff --git a/image.c b/image.c
index 45d1aec5425fe962f7d4d3c1c80e6435c1916776..e72dd0fc544ab9b1eec2f8d84fcd977b6cdb7b12 100644 (file)
--- a/image.c
+++ b/image.c
@@ -25,7 +25,7 @@ void freeimage (image_t *image)
     free (image);
 }
 
     free (image);
 }
 
-int contain (char *str, char c)
+int contain (char *str, int c)
 {
     while (*str != c) {
         if (*str++ == '\0') {
 {
     while (*str != c) {
         if (*str++ == '\0') {
@@ -44,9 +44,8 @@ image_t *expandimage (image_t *image, int scale)
 {
     image_t *nimage = newimage (image->width * scale, image->height * scale, image->maxvalue);
 
 {
     image_t *nimage = newimage (image->width * scale, image->height * scale, image->maxvalue);
 
-    int i, j;
-    for (i = 0; i < nimage->width; i++) {
-        for (j = 0; j < nimage->height; j++) {
+    for (int i = 0; i < nimage->width; i++) {
+        for (int j = 0; j < nimage->height; j++) {
             int pos = i / scale + (j / scale) * image->width;
             int Pos = i + j * nimage->width;
             nimage->red[Pos] = image->red[pos];
             int pos = i / scale + (j / scale) * image->width;
             int Pos = i + j * nimage->width;
             nimage->red[Pos] = image->red[pos];
@@ -62,15 +61,14 @@ image_t *shrinkimage (image_t *image, int scale)
 {
     image_t *nimage = newimage ((image->width + scale - 1) / scale, (image->height + scale - 1) / scale, image->maxvalue);
 
 {
     image_t *nimage = newimage ((image->width + scale - 1) / scale, (image->height + scale - 1) / scale, image->maxvalue);
 
-    int i, j;
-    for (i = 0; i < nimage->width; i++) {
-        for (j = 0; j < nimage->height; j++) {
+    for (int i = 0; i < nimage->width; i++) {
+        for (int j = 0; j < nimage->height; j++) {
             float red = 0;
             float green = 0;
             float blue = 0;
             float red = 0;
             float green = 0;
             float blue = 0;
-            int k, l, n = 0;
-            for (k = 0; k < scale; k++) {
-                for (l = 0; l < scale; l++) {
+            int n = 0;
+            for (int k = 0; k < scale; k++) {
+                for (int l = 0; l < scale; l++) {
                     int I = i * scale + k;
                     int J = j * scale + l;
                     if ((I < image->width) && (J < image->height)) {
                     int I = i * scale + k;
                     int J = j * scale + l;
                     if ((I < image->width) && (J < image->height)) {
diff --git a/image.h b/image.h
index 01f67f5e72f28651fcc5a388c5c6d5a43486eb44..610a0a151397cfaf6d25f0d13daa3a07217b5ae3 100644 (file)
--- a/image.h
+++ b/image.h
@@ -16,7 +16,7 @@ image_t *newimage (int width, int height, int maxvalue);
 
 void freeimage (image_t *image);
 
 
 void freeimage (image_t *image);
 
-int contain (char *str, char c);
+int contain (char *str, int c);
 
 float correction (float value, float factor);
 
 
 float correction (float value, float factor);
 
index 4bd023850c045da6a6ab686cf1e15e7bba68c06c..09ecb8bc309abe5c9bba31812b7dfecc3743bcb9 100644 (file)
--- a/makefile
+++ b/makefile
@@ -5,12 +5,14 @@ CC = gcc
 #INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
 INCLUDES =
 OFLAGS  = -O4 -Os
 #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
 #OFLAGS += -minline-all-stringops -fsingle-precision-constant
 #OFLAGS += -malign-double
 CFLAGS += -W -Wall -Wextra -g
 #OFLAGS  = -O4 -ffast-math -finline-functions
 #OFLAGS  = -O4 -finline-functions
 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
 #OFLAGS += -minline-all-stringops -fsingle-precision-constant
 #OFLAGS += -malign-double
 CFLAGS += -W -Wall -Wextra -g
+#CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
 LDFLAGS += -g $(LDOPTS) $(OPTIONS)
 
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
 LDFLAGS += -g $(LDOPTS) $(OPTIONS)
 
@@ -18,15 +20,14 @@ LDOPT = linker
 MV = mv
 ifneq (, $(findstring linux, $(MAKE_HOST)))
 # Linux
 MV = mv
 ifneq (, $(findstring linux, $(MAKE_HOST)))
 # Linux
-CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
 else ifneq (, $(findstring mingw, $(MAKE_HOST)))
 # Windows MinGw
 else ifneq (, $(findstring mingw, $(MAKE_HOST)))
 # Windows MinGw
-CFLAGS += -DPDC_WIDE
+CFLAGS += -DWIN32
 #LDLIBS += -lws2_32
 LDOPT = winlnk
 else ifneq (, $(findstring cygwin, $(MAKE_HOST)))
 #LDLIBS += -lws2_32
 LDOPT = winlnk
 else ifneq (, $(findstring cygwin, $(MAKE_HOST)))
+CFLAGS += -DWIN32
 # Windows CygWin
 # Windows CygWin
-CFLAGS += -DPDC_WIDE
 LDOPT = winlnk
 else ifneq (, $(findstring msdos, $(MAKE_HOST)))
 # MSDOS
 LDOPT = winlnk
 else ifneq (, $(findstring msdos, $(MAKE_HOST)))
 # MSDOS
@@ -36,9 +37,7 @@ endif
 
 # Targets
 
 
 # Targets
 
-ALLEXE  =
-ALLEXE += display
-ALLEXE += ndisplay
+ALLEXE  = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done)
 
 SHELL = bash
 
 
 SHELL = bash
 
@@ -67,6 +66,11 @@ GETCOMMENTS = awk '/\/\*\s*$(1):/,/\*\// { sub(/.*\/\*\s*$(1):/, ""); sub (/\s*\
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
+analyze:
+       make purge
+       scan-build make
+       #scan-build -stats make
+
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
@@ -119,7 +123,7 @@ gcov_%:
        touch gcov
        rm -f gcov $(wildcard *.gcda *.gcno)
        $(MAKE) purge
        touch gcov
        rm -f gcov $(wildcard *.gcda *.gcno)
        $(MAKE) purge
-       grep '#####' *.c.gcov || true
+       grep '^ *#####' *.c.gcov || true
 
 gprof_%:
        $(MAKE) purge
 
 gprof_%:
        $(MAKE) purge
@@ -153,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
 
@@ -186,7 +191,7 @@ valgrind_%: %.exe
 
 ## Phony
 
 
 ## Phony
 
-.PHONY: all clean count depends gcovs purge tests
+.PHONY: all analyze clean count depends gcovs purge tests
 
 ## Precious
 
 
 ## Precious
 
index 1eec4d48a7e4a2d7b10090bf4ccff55d089f1b9c..e2a5f72535c5b8343d0871fe7e9dc9c76412017e 100644 (file)
@@ -1,5 +1,5 @@
 /* depend: */
 /* depend: */
-/* cflags: */
+/* cflags: -D_XOPEN_SOURCE=500 */
 /* linker: color.o debug.o image.o pnm.o -lncursesw -lm */
 /* doslnk: color.o debug.o image.o pnm.o -lpdc~2 -lm */
 /* winlnk: color.o debug.o image.o pnm.o -lpdcursesw -lm */
 /* linker: color.o debug.o image.o pnm.o -lncursesw -lm */
 /* doslnk: color.o debug.o image.o pnm.o -lpdc~2 -lm */
 /* winlnk: color.o debug.o image.o pnm.o -lpdcursesw -lm */
@@ -55,13 +55,12 @@ int usage (int ret)
 int main (int argc, char *argv[])
 {
     char *filename = NULL;
 int main (int argc, char *argv[])
 {
     char *filename = NULL;
-    int i;
 
     /* get basename */
     char *pt = progname = argv[0];
     while (*pt) {
         if ((*pt == '/') || (*pt == '\\')) {
 
     /* get basename */
     char *pt = progname = argv[0];
     while (*pt) {
         if ((*pt == '/') || (*pt == '\\')) {
-           progname = pt + 1;
+            progname = pt + 1;
         }
         pt++;
     }
         }
         pt++;
     }
@@ -75,43 +74,43 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
         }
         char c = arg[1];
         switch (c) {
-        case 'g':
-            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if (arg == NULL) {
-                VERBOSE (ERROR, fprintf (stderr, "%s: no gamma specified\n", progname));
-                return usage (1);
-            }
-            gf[0] = strtof (arg, &pt);
-            if (*pt == '\0') {
-                gf[1] = gf[2] = gf[0];
-            } else if (*pt == ':') {
-                gf[1] = strtof (pt + 1, &pt);
-                if (*pt == ':') {
-                    gf[2] = strtof (pt + 1, &pt);
+            case 'g':
+                arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+                if (arg == NULL) {
+                    VERBOSE (ERROR, fprintf (stderr, "%s: no gamma specified\n", progname));
+                    return usage (1);
+                }
+                gf[0] = strtof (arg, &pt);
+                if (*pt == '\0') {
+                    gf[1] = gf[2] = gf[0];
+                } else if (*pt == ':') {
+                    gf[1] = strtof (pt + 1, &pt);
+                    if (*pt == ':') {
+                        gf[2] = strtof (pt + 1, &pt);
+                    } else {
+                        gf[2] = 0;
+                    }
                 } else {
                 } else {
-                    gf[2] = 0;
+                    gf[1] = gf[2] = 0;
                 }
                 }
-            } else {
-                gf[1] = gf[2] = 0;
-            }
-            break;
-        case 't':
-            mode = 1;
-            break;
-        case 'v':
-            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
-            if (arg == NULL) {
-                VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
-                return usage (1);
-            }
-            verbose = atoi (arg);
-            break;
-        case 'w':
-            mode = 2;
-            break;
-        case 'h':
-        default:
-            return usage (c != 'h');
+                break;
+            case 't':
+                mode = 1;
+                break;
+            case 'v':
+                arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+                if (arg == NULL) {
+                    VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
+                    return usage (1);
+                }
+                verbose = atoi (arg);
+                break;
+            case 'w':
+                mode = 2;
+                break;
+            case 'h':
+            default:
+                return usage (c != 'h');
         }
     }
 
         }
     }
 
@@ -134,7 +133,7 @@ int main (int argc, char *argv[])
     }
 
     /* pre-calculate expanded and shrinked images */
     }
 
     /* pre-calculate expanded and shrinked images */
-    for (i = 2; i <= MAXSCALE; i++) {
+    for (int i = 2; i <= MAXSCALE; i++) {
         images[i] = shrinkimage (images[1], i);
     }
     images[0] = expandimage (images[1], 2);
         images[i] = shrinkimage (images[1], i);
     }
     images[0] = expandimage (images[1], 2);
@@ -160,8 +159,7 @@ int main (int argc, char *argv[])
     int ycursor = 0;
 
     /* init colormap */
     int ycursor = 0;
 
     /* init colormap */
-    int c;
-    for (c = 0; c < MAXCOLORS; c++) {
+    for (int c = 0; c < MAXCOLORS; c++) {
         init_pair (c, 0, c);
     }
 
         init_pair (c, 0, c);
     }
 
@@ -169,8 +167,6 @@ int main (int argc, char *argv[])
     int stop = 0;
     int change = 1;
     while (!stop) {
     int stop = 0;
     int change = 1;
     while (!stop) {
-        int x, y;
-
         /* check cursor location */
         if (images[scale]->width - xcursor < COLS / mode) {
             int newxcursor = max (0, images[scale]->width - COLS / mode);
         /* check cursor location */
         if (images[scale]->width - xcursor < COLS / mode) {
             int newxcursor = max (0, images[scale]->width - COLS / mode);
@@ -196,8 +192,8 @@ int main (int argc, char *argv[])
             int width = min (images[scale]->width - xcursor, COLS / mode);
             int height = min (images[scale]->height - ycursor, LINES);
             VERBOSE (DEBUG, FILE *fd = fopen ("my.log", "a"); fprintf (fd, "(%d, %d)\n", width, height); fclose (fd));
             int width = min (images[scale]->width - xcursor, COLS / mode);
             int height = min (images[scale]->height - ycursor, LINES);
             VERBOSE (DEBUG, FILE *fd = fopen ("my.log", "a"); fprintf (fd, "(%d, %d)\n", width, height); fclose (fd));
-            for (x = 0; x < width; x++) {
-                for (y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                for (int y = 0; y < height; y++) {
 
                     int ind = x + xcursor + images[scale]->width * (y + ycursor);
                     //if (ind >= images[scale]->width * images[scale]->height) {
 
                     int ind = x + xcursor + images[scale]->width * (y + ycursor);
                     //if (ind >= images[scale]->width * images[scale]->height) {
@@ -317,7 +313,7 @@ int main (int argc, char *argv[])
 
     /* cleaning before quiting */
     endwin ();
 
     /* cleaning before quiting */
     endwin ();
-    for (i = 0; i <= MAXSCALE; i++) {
+    for (int i = 0; i <= MAXSCALE; i++) {
         freeimage (images[i]);
     }
 
         freeimage (images[i]);
     }
 
diff --git a/pnm.c b/pnm.c
index 9751168d75e7c74a29c4f69b4fbbe7d51a01bed8..551181a736d672edf4fa8b2e275e59dc097583b4 100644 (file)
--- a/pnm.c
+++ b/pnm.c
@@ -9,8 +9,7 @@ int testpnm (FILE *fd)
     int mode = 0;
     /* check magic number */
     char magic[3] = {0};
     int mode = 0;
     /* check magic number */
     char magic[3] = {0};
-    fread (magic, 3, 1, fd);
-    if ((contain ("P", magic[0])) && (contain (" \t\n\r", magic[2]))) {
+    if ((fread (magic, 3, 1, fd) == 1) && (contain ("P", magic[0])) && (contain (" \t\n\r", magic[2]))) {
         if ((magic[1] >= '1') && (magic[1] <= '6')) {
             mode = magic[1] - '0';
         }
         if ((magic[1] >= '1') && (magic[1] <= '6')) {
             mode = magic[1] - '0';
         }
@@ -22,15 +21,14 @@ int getparam (FILE *fd, int *value)
 {
     int start = 0;
     *value = 0;
 {
     int start = 0;
     *value = 0;
-    while (!feof (fd)) {
-        int c = fgetc (fd);
+    int c;
+    while ((c = fgetc (fd)) != EOF) {
         if (contain ("#", c)) {
         if (contain ("#", c)) {
-            while (!feof (fd)) {
-                c = fgetc (fd);
-                if (contain ("\n\r", c)) {
-                    break;
+            do {
+                if ((c = fgetc (fd)) == EOF) {
+                    return 0;
                 }
                 }
-            }
+            } while (!contain ("\n\r", c));
         } else if (contain ("0123456789", c)) {
             *value = 10 * *value + c - '0';
             start = 1;
         } else if (contain ("0123456789", c)) {
             *value = 10 * *value + c - '0';
             start = 1;
@@ -48,11 +46,9 @@ int getparam (FILE *fd, int *value)
 int getrawvalue (FILE *fd, int nbbytes)
 {
     int value = fgetc (fd);
 int getrawvalue (FILE *fd, int nbbytes)
 {
     int value = fgetc (fd);
-    if (value != EOF) {
-        if (nbbytes == 2) {
-            int value0 = fgetc (fd);
-            value = (value0 != EOF) ? value * 256 + value0 : EOF;
-        }
+    if ((value != EOF) && (nbbytes == 2)) {
+        int value0 = fgetc (fd);
+        value = (value0 != EOF) ? value * 256 + value0 : EOF;
     }
     return value;
 }
     }
     return value;
 }
@@ -74,14 +70,15 @@ int readpbma (FILE *fd, image_t *image)
 {
     int i = 0;
     while (i < image->width * image->height) {
 {
     int i = 0;
     while (i < image->width * image->height) {
-        int c = fgetc (fd);
-        if (contain ("#", c)) {
-            while (!feof (fd)) {
-                c = fgetc (fd);
-                if (contain ("\n\r", c)) {
-                    break;
+        int c;
+        if ((c = fgetc (fd)) == EOF) {
+            return 0;
+        } else if (contain ("#", c)) {
+            do {
+                if ((c = fgetc (fd)) == EOF) {
+                    return 0;
                 }
                 }
-            }
+            } while (!contain ("\n\r", c));
         } else if (contain ("01", c)) {
             image->red[i] = image->green[i] = image->blue[i] = '1' - c;
             i++;
         } else if (contain ("01", c)) {
             image->red[i] = image->green[i] = image->blue[i] = '1' - c;
             i++;
@@ -99,7 +96,9 @@ int readpbmr (FILE *fd, image_t *image)
     int value = EOF;
     while (i < image->width * image->height) {
         if (k == 0) {
     int value = EOF;
     while (i < image->width * image->height) {
         if (k == 0) {
-            value = fgetc (fd);
+            if ((value = fgetc (fd)) == EOF) {
+                break;
+            }
             k = 8;
         }
         image->red[i] = image->green[i] = image->blue[i] = !((value >> --k) & 1);
             k = 8;
         }
         image->red[i] = image->green[i] = image->blue[i] = !((value >> --k) & 1);
@@ -116,8 +115,7 @@ int readpgm (FILE *fd, image_t *image, int mode)
     int i = 0;
     mode = (mode) ? ((image->maxvalue > 255) ? 2 : 1) : 0;
     int value = EOF;
     int i = 0;
     mode = (mode) ? ((image->maxvalue > 255) ? 2 : 1) : 0;
     int value = EOF;
-    while (i < image->width * image->height) {
-        value = getvalue (fd, mode);
+    while ((i < image->width * image->height) && ((value = getvalue (fd, mode)) != EOF)) {
         image->red[i] = image->green[i] = image->blue[i] = value / (float)image->maxvalue;
         i++;
     }
         image->red[i] = image->green[i] = image->blue[i] = value / (float)image->maxvalue;
         i++;
     }
@@ -130,11 +128,17 @@ int readppm (FILE *fd, image_t *image, int mode)
     mode = (mode) ? ((image->maxvalue > 255) ? 2 : 1) : 0;
     int value = EOF;
     while (i < image->width * image->height) {
     mode = (mode) ? ((image->maxvalue > 255) ? 2 : 1) : 0;
     int value = EOF;
     while (i < image->width * image->height) {
-        value = getvalue (fd, mode);
+        if ((value = getvalue (fd, mode)) == EOF) {
+            break;
+        }
         image->red[i] = value / (float)image->maxvalue;
         image->red[i] = value / (float)image->maxvalue;
-        value = getvalue (fd, mode);
+        if ((value = getvalue (fd, mode)) == EOF) {
+            break;
+        }
         image->green[i] = value / (float)image->maxvalue;
         image->green[i] = value / (float)image->maxvalue;
-        value = getvalue (fd, mode);
+        if ((value = getvalue (fd, mode)) == EOF) {
+            break;
+        }
         image->blue[i] = value / (float)image->maxvalue;
         i++;
     }
         image->blue[i] = value / (float)image->maxvalue;
         i++;
     }