clean ascii
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 19 Dec 2022 13:43:55 +0000 (14:43 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 19 Dec 2022 13:43:55 +0000 (14:43 +0100)
ascii.c
fdprintf.c
fdprintf.h

diff --git a/ascii.c b/ascii.c
index c15d71bc1021bcbcb5c3bbc253a00f07584e0636..c61554b9da2599f62a0b9274b380f1bccd9b5606 100644 (file)
--- a/ascii.c
+++ b/ascii.c
@@ -40,7 +40,7 @@ char tablechars[256][4] = {
 
 int usage (int ret)
 {
-    int fd = ret ? _fd_stderr : _fd_stdout;
+    int fd = ret ? stdfderr : stdfdout;
     fdprintf (fd, "usage: %s\n", progname);
     fdprintf (fd, " -c : number of columns (%d)\n", nbcols);
     fdprintf (fd, " -h : help message\n");
@@ -55,6 +55,8 @@ int main (int argc, char *argv[])
 {
     int i = 0;
 
+    /* programm name */
+
     progname = argv[0];
     while (progname[i] != '\0') {
         if ((progname[i] == '/') || (progname[i] == '\\')) {
@@ -65,6 +67,8 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* argument processing */
+
     while (argc-- > 1) {
         char *arg = *(++argv);
         if (arg[0] != '-') {
@@ -93,29 +97,36 @@ int main (int argc, char *argv[])
 
     /* main */
 
-    for (i = 0; i < 256; i++) {
+    int n = (256 + nbcols - 1) / nbcols;
+    for (i = 0; i < n * nbcols; i++) {
         char line[] = "   [  ]    x";
+        int j = i / nbcols + (i % nbcols) * n;
 
-        if (i > 99) line[0] = '0' + i / 100;
-        if (i > 9) line[1] = '0' + (i / 10) % 10;
-        line[2] = '0' + i % 10;
-        line[4] = '0' + i / 16;
+        if (j > 255) {
+            PRINTOUT ("\n");
+            continue;
+        }
+
+        if (j > 99) line[0] = '0' + j / 100;
+        if (j > 9) line[1] = '0' + (j / 10) % 10;
+        line[2] = '0' + j % 10;
+        line[4] = '0' + j / 16;
         if (line[4] > '9') line[4] += 'a' - '0' - 10;
-        line[5] = '0' + i % 16;
+        line[5] = '0' + j % 16;
         if (line[5] > '9') line[5] += 'a' - '0' - 10;
         
-        if (tablechars[i][0] != '\0') {
-            int j = 0;
-            while (tablechars[i][j] != '\0') {
-                line[8 + j] = tablechars[i][j];
-                j++;
+        if (tablechars[j][0] != '\0') {
+            int k = 0;
+            while (tablechars[j][k] != '\0') {
+                line[8 + k] = tablechars[j][k];
+                k++;
             }
         } else {
-            line[8] = i;
+            line[8] = j;
         }
-        line[11] = (((i + 1) % nbcols == 0) || (i == 255)) ? '\n' : ' ';
+        line[11] = ((i + 1) % nbcols == 0) ? '\n' : ' ';
 
-        fdprintf (_fd_stdout, "%s", line);
+        PRINTOUT ("%s", line);
     }
 
     return 0;
@@ -126,4 +137,10 @@ int main (int argc, char *argv[])
 // test: ascii.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 
+// test: ascii.exe | awk '/ 64\[40\] @/ { rc=1 } END { exit (1-rc) }'
+// test: ascii.exe | awk '/127\[7f\] DEL/ { rc=1 } END { exit (1-rc) }'
+// test: ascii.exe -c 4 | tail -1 | awk '/63/ { rc=1 } END { exit (1-rc) }'
+// test: ascii.exe -c 5 | tail -1 | awk '/51/ { rc=1 } END { exit (1-rc) }'
+// test: ascii.exe -c 6 | tail -1 | awk '/42/ { rc=1 } END { exit (1-rc) }'
+
 /* vim: set ts=4 sw=4 et: */
index c7ee4c1f8ce0b96b5cef52c467d087d4f3df490b..238878dce42b2877ba90c719b9a62e46bfb4b945 100644 (file)
@@ -17,8 +17,8 @@
 
 #include "fdprintf.h"
 
-int _fd_stdout = STDOUT_FILENO;
-int _fd_stderr = STDERR_FILENO;
+int stdfdout = STDOUT_FILENO;
+int stdfderr = STDERR_FILENO;
 
 unsigned int nextpow (unsigned int x, int base) {
     unsigned int n = 0;
index d924e1c98af1eaabbaef99d2794e863671954d62..3094ef7ae44b3f8cad3ab39868175245176179ee 100644 (file)
@@ -3,11 +3,11 @@
 
 int fdprintf (int fd, const char *fmt, ...);
 
-extern int _fd_stdout;
-extern int _fd_stderr;
+extern int stdfdout;
+extern int stdfderr;
 
-#define PRINTOUT(fmt...) fdprintf (_fd_stdout, fmt)
-#define PRINTERR(fmt...) fdprintf (_fd_stderr, fmt)
+#define PRINTOUT(fmt...) fdprintf (stdfdout, fmt)
+#define PRINTERR(fmt...) fdprintf (stdfderr, fmt)
 
 #endif /* __FDPRINTF_H__ */