diff options
author | Robert de Bath <rdebath@poboxes.com> | 1997-07-20 12:16:17 +0200 |
---|---|---|
committer | Lubomir Rintel <lkundrak@v3.sk> | 2013-10-23 23:39:55 +0200 |
commit | 9d97bc3cb3aecd3416fb7c4be3ca2f436665b696 (patch) | |
tree | b1e5b67ef9e065efb6a4c9977ecfac8dedbad39b /tests | |
parent | e63c244cb22bf48ca1d2695784a072269d19ea96 (diff) | |
download | dev86-9d97bc3cb3aecd3416fb7c4be3ca2f436665b696.tar.gz |
Import Dev86src-0.12.4.tar.gzv0.12.4
Diffstat (limited to 'tests')
-rw-r--r-- | tests/README | 3 | ||||
-rw-r--r-- | tests/hd.c | 81 |
2 files changed, 59 insertions, 25 deletions
diff --git a/tests/README b/tests/README index 1f9bcfd..996660f 100644 --- a/tests/README +++ b/tests/README @@ -18,7 +18,4 @@ wc.c Word,line and character counts. grab.c Grabs _all_ available memory (3GB+ on Linux-i386) then frees rand.c Generates random numbers and pipes them to 'hd' -BTW: i386 OMAGIC files can be converted to deformed ELFs with this: - $ objcopy -O elf32-i386 sync - -Robert @@ -9,6 +9,10 @@ long offset = 0; FILE *fd; +FILE * ofd = stdout; +char * outfile = 0; +int reverse = 0; + main(argc, argv) int argc; char **argv; @@ -21,25 +25,52 @@ char **argv; if (aflag && argv[ar][0] == '-') switch (argv[ar][1]) { - case 'r': - return reverse_hd(argc, argv); - case 'o': + case 'r': /* Reverse */ + reverse = 1; + break; + case 's': /* Skip */ offset = strtol(argv[ar] + 2, (void *) 0, 0); break; case '-': aflag = 0; break; + case 'o': /* Output */ + if( argv[ar][2] ) outfile = argv[ar]+2; + else + { + if( ++ar >= argc ) Usage(); + outfile = argv[ar]; + } + break; default: Usage(); } else { + if( outfile ) + { + if( ofd != stdout ) fclose(ofd); +#ifdef MSDOS + if( reverse ) + ofd = fopen(outfile, "wb"); + else +#endif + ofd = fopen(outfile, "w"); + if( ofd == 0 ) + { + fprintf(stderr, "Cannot open file '%s'\n", outfile); + exit(9); + } + } fd = fopen(argv[ar], "rb"); if (fd == 0) fprintf(stderr, "Cannot open file '%s'\n", argv[ar]); else { - do_fd(); + if( reverse ) + do_rev_fd(); + else + do_fd(); fclose(fd); } done = 1; @@ -51,14 +82,19 @@ char **argv; #else { fd = stdin; - do_fd(); + if( reverse ) + do_rev_fd(); + else + do_fd(); } #endif + + exit(0); } Usage() { - fprintf(stderr, "Usage: hd [-r]|[[-oOffset] file]\n"); + fprintf(stderr, "Usage: hd [-o Outfile][-r]|[[-sSkip_bytes] file]\n"); exit(1); } @@ -109,28 +145,28 @@ int eofflag; { if (lastaddr + 16 == address) { - printf("*\n"); - fflush(stdout); + fprintf(ofd, "*\n"); + fflush(ofd); } return; } } lastaddr = address; - printf("%06lx:", address); + fprintf(ofd, "%06lx:", address); for (j = 0; j < 16; j++) { if (j == 8) - putchar(' '); + fputc(' ', ofd); if (num[j] >= 0) - printf(" %02x", num[j]); + fprintf(ofd, " %02x", num[j]); else - printf(" "); + fprintf(ofd, " "); lastnum[j] = num[j]; num[j] = -1; } - printf(" %.16s\n", chr); + fprintf(ofd, " %.16s\n", chr); } @@ -139,13 +175,12 @@ int eofflag; * file */ -/* -- 0 1 2 3 4 5 6 7 8 9 a b c d e f */ -static char *datafmt = "%x: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n"; -reverse_hd() +do_rev_fd() { char str[160]; char * ptr; - int c[16], d[16], x, i, nxtaddr, addr; + int c[16], i, nxtaddr, addr; + int zap_last = 1; for (i = 0; i < 16; i++) c[i] = 0; @@ -153,30 +188,32 @@ reverse_hd() for (nxtaddr = 0;;) { - if (gets(str) == NULL) + if (fgets(str, sizeof(str), fd) == NULL) break; str[57] = 0; ptr = str; + if( *ptr == '*' ) zap_last = 0; if( !isxdigit(*ptr) ) continue; addr = strtol(ptr, &ptr, 16); if( *ptr == ':' ) ptr++; if (nxtaddr == 0) nxtaddr = addr; + if( zap_last ) memset(c, 0, sizeof(c)); + else zap_last = 1; while (nxtaddr < addr) { - nxtaddr += 16; - for (i = 0; i < 16; i++) - putchar(c[i]); + for (i = 0; nxtaddr < addr && i < 16; i++, nxtaddr++) + fputc(c[i], ofd); } for (i = 0; i < 16 && *ptr; i++) { char * ptr2; c[i] = strtol(ptr, &ptr2, 16); if( ptr == ptr2 ) break; - putchar(c[i]); + fputc(c[i], ofd); ptr = ptr2; } nxtaddr += 16; |