output command
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 10 Jan 2023 15:36:14 +0000 (16:36 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 10 Jan 2023 15:36:14 +0000 (16:36 +0100)
hexdump.c

index 987434368174826baf62ce63c503ededbc366d33..6674970df831478cf7c0317e0cb28de540448195 100644 (file)
--- a/hexdump.c
+++ b/hexdump.c
@@ -23,6 +23,9 @@
 
 /* gobal variables */
 
+char *buffer[BUFFERSIZE] = {0};
+FILE *fin = NULL;
+FILE *fout = NULL;
 char *progname = NULL;
 
 /* help function */
@@ -81,9 +84,18 @@ void printline (char *buffer, int nbcols, int nb, int addr, int nbdigits) {
     printf ("\n");
 }
 
-/* indent function */
+/* write file function */
 
-int hexdump (FILE *fin, int nbcols, int len) {
+int writefile (char *pt, int nb) {
+    if (fout) {
+        fwrite (pt, 1, nb, fout);
+    }
+    return 1;
+}
+
+/* hexadecimal dump function */
+
+int hexdump (int nbcols, int len) {
     char buffer[BUFFERSIZE] = {0};
     int i;
 
@@ -117,6 +129,7 @@ int hexdump (FILE *fin, int nbcols, int len) {
         /* print line */
         while ((nb - (int)(pt - buffer)) / nbcols > 0) {
             printline (pt, nbcols, nbcols, addr, nbdigits);
+            writefile (pt, nbcols);
             pt += nbcols;
             addr += nbcols;
         }
@@ -137,6 +150,7 @@ int hexdump (FILE *fin, int nbcols, int len) {
     /* last line */
     if (nb > 0) {
         printline (buffer, nbcols, nb, addr, nbdigits);
+        writefile (pt, nb);
     }
 
     return 0;
@@ -196,7 +210,6 @@ int main (int argc, char *argv[])
     }
 
     /* check input */
-    FILE *fin = NULL;
     if (input) {
         fin = fopen (input, "rb");
         if (!fin) {
@@ -208,20 +221,19 @@ int main (int argc, char *argv[])
     }
 
     /* check output */
-    FILE *fout = NULL;
     if (output) {
-        fout = fopen (input, "wb");
+        fout = fopen (output, "wb");
         if (!fout) {
             VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
             fclose (fin);
             return 1;
         }
     } else {
-        fout = stdout;
+        //fout = stdout;
     }
 
     if (commands == NULL) {
-        hexdump (fin, nbcols, -1);
+        hexdump (nbcols, -1);
     } else {
         VERBOSE (DEBUG, printf ("commands: %s\n", commands));
         while ((*commands != '\0') && (rc == 0)) {
@@ -263,7 +275,7 @@ int main (int argc, char *argv[])
                         break;
                     }
                 }
-                if (rc == 0) hexdump (fin, nbcols, printlen);
+                if (rc == 0) hexdump (nbcols, printlen);
                 break;
 
             case 's': /* substitute mode */
@@ -276,6 +288,16 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* end of file */
+    if ((rc == 0) && (fout != NULL)) {
+        while (!feof (fin)) {
+            int nbread = fread (buffer, 1, BUFFERSIZE, fin);
+            if (nbread) {
+                fwrite (buffer, 1, nbread, fout);
+            }
+        }
+    }
+
     /* close all */
     if (fin) fclose (fin);
     if (fout) fclose (fout);
@@ -288,6 +310,9 @@ int main (int argc, char *argv[])
 // test: hexdump.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: hexdump.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 // test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
-// test: hexdump.exe -i hexdump.c -n 3|head -2|tail -1| grep '0x0003: 64 65 70  dep'
+// test: hexdump.exe -i hexdump.c -n 3 | head -2 | tail -1 | grep -q '0x0003: 64 65 70  dep'
+// test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
+// test: cmp hexdump.c test.c
+// test: rm test.c
 
 /* vim: set ts=4 sw=4 et: */