Made hexin and hexout prompts adapt to isatty()
authorRick van Rein <rick@openfortress.nl>
Sat, 2 Feb 2019 12:14:36 +0000 (13:14 +0100)
committerRick van Rein <rick@openfortress.nl>
Sat, 2 Feb 2019 12:14:36 +0000 (13:14 +0100)
hexin.c
hexout.c

diff --git a/hexin.c b/hexin.c
index 15d3832..0b7eb8e 100644 (file)
--- a/hexin.c
+++ b/hexin.c
@@ -57,14 +57,18 @@ int main (int argc, char *argv []) {
        size_t len;
        unsigned char buf [BYTES_PER_LINE];
        char ch;
+       int prompt;
 
+       prompt = isatty (0);
        while (status != endfile) {
                if (status == endline) {
                        status = normal;
                }
-               usleep (1000000L); // Yield to others -- better prompt printing
-               fprintf (stderr, "%08lx>", offset);
-               fflush (stderr);
+               if (prompt) {
+                       usleep (1000000L); // Yield to others -- better prompt printing
+                       fprintf (stderr, "%08lx>", offset);
+                       fflush (stderr);
+               }
                len = 0;
                while ((len < BYTES_PER_LINE) && (status == normal)) {
                        if (getbyte (&buf [len])) {
index d2e4746..9b4ac8f 100644 (file)
--- a/hexout.c
+++ b/hexout.c
@@ -31,6 +31,9 @@ int main (int argc, char *argv []) {
 */
        size_t len;
        size_t offset = 0;
+       int prompt;
+       char comma [80];
+
        if (signal (SIGALRM, tick) == SIG_ERR) {
                perror ("Failed to install interval handler");
                exit (1);
@@ -43,6 +46,7 @@ int main (int argc, char *argv []) {
                        len = read (0, buf, BYTES_PER_LINE),
                        (len > 0) || ( errno == SIGALRM ) ) {
 */
+       prompt = isatty (1);
        while (len = read (0, buf, 1), len > 0) {
                size_t i;
                int len2;
@@ -56,10 +60,16 @@ int main (int argc, char *argv []) {
                if (len2 >= 0) {
                        len += len2;
                }
-               printf ("%08lx", offset);
+               if (prompt) {
+                       snprintf (comma, sizeof(comma)-1, "%08lx", offset);
+               } else {
+                       comma [0] = '\0';
+               }
                offset += len;
                for (i=0; i<len; i++) {
-                       printf (" %02x", buf [i]);
+                       printf ("%s%02x", comma, buf [i]);
+                       comma [0] = ' ';
+                       comma [1] = '\0';
                }
                printf ("\r\n");
                fflush (stdout);