/* gobal variables */
+char *buffer[BUFFERSIZE] = {0};
+FILE *fin = NULL;
+FILE *fout = NULL;
char *progname = NULL;
/* help function */
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;
/* print line */
while ((nb - (int)(pt - buffer)) / nbcols > 0) {
printline (pt, nbcols, nbcols, addr, nbdigits);
+ writefile (pt, nbcols);
pt += nbcols;
addr += nbcols;
}
/* last line */
if (nb > 0) {
printline (buffer, nbcols, nb, addr, nbdigits);
+ writefile (pt, nb);
}
return 0;
}
/* check input */
- FILE *fin = NULL;
if (input) {
fin = fopen (input, "rb");
if (!fin) {
}
/* 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)) {
break;
}
}
- if (rc == 0) hexdump (fin, nbcols, printlen);
+ if (rc == 0) hexdump (nbcols, printlen);
break;
case 's': /* substitute mode */
}
}
+ /* 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);
// 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: */