From: Laurent Mazet Date: Mon, 19 Dec 2022 13:43:55 +0000 (+0100) Subject: clean ascii X-Git-Url: https://secure.softndesign.org/git/?p=ascii.git;a=commitdiff_plain;h=a8476deb9f38ea2080c878c33e4ec02946f02394 clean ascii --- diff --git a/ascii.c b/ascii.c index c15d71b..c61554b 100644 --- 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: */ diff --git a/fdprintf.c b/fdprintf.c index c7ee4c1..238878d 100644 --- a/fdprintf.c +++ b/fdprintf.c @@ -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; diff --git a/fdprintf.h b/fdprintf.h index d924e1c..3094ef7 100644 --- a/fdprintf.h +++ b/fdprintf.h @@ -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__ */