From: Laurent Mazet Date: Tue, 10 Jan 2023 15:36:14 +0000 (+0100) Subject: output command X-Git-Url: https://secure.softndesign.org/git/?p=hexdump.git;a=commitdiff_plain;h=ce305529de90ec2d743c79432601a455b12bdc92 output command --- diff --git a/hexdump.c b/hexdump.c index 9874343..6674970 100644 --- 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: */