home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
DUMP.ZIP
/
DUMP.C
< prev
next >
Wrap
Text File
|
1990-01-03
|
4KB
|
96 lines
/*
DUMP.C for OS/2 version 1.2.
Displays the binary contents of a file in hex and ASCII on the
standard output device. Demonstrates direct calls to OS/2 API
from a C program. Supports long-names. Uses the new DosOpen2
API for compatibility with OS/2 2.0.
This program does not require Microsoft or IBM Programmer's Toolkit
header files, but must be linked using LINK.EXE and DOSCALLS.LIB
from retail OS/2 version 1.2.
Compile: cl -c /Zi dump.c
link /CO dump,dump,,doscalls,dump.def;
Usage is: dump pathname.ext [>destination]
Copyright (C) 1989 Ray Duncan
*/
#include <stdio.h>
#define RECSIZE 16 // size of file records
#define MAXPATHNAME 260 // max length of pathname
#define MAXFILENAME 255 // max length of filename
#define API unsigned extern far pascal // OS/2 API function prototypes
API DosClose(unsigned);
API DosOpen2(char far *, unsigned far *, unsigned far *, unsigned long,
unsigned, unsigned, unsigned long, void far *, unsigned long);
API DosQPathInfo(void far *, unsigned, char far *, int, unsigned long);
API DosRead(unsigned, void far *, unsigned, unsigned far *);
main(int argc, char *argv[])
{
char fbuff[RECSIZE]; // data block from file
char qname[MAXPATHNAME]; // receives qualified filename
unsigned long foffset = 0L; // file offset in bytes
unsigned handle, action, length; // DosOpen2, DosRead variables
unsigned OpenFlag = 0x01; // fail if file not found
unsigned long OpenMode = 0x40; // read only, deny none
if(argc < 2) // check command tail
{
fprintf(stderr, "\ndump: missing filename\n");
exit(1);
}
if(DosOpen2(argv[1], &handle, &action, 0L, 0, OpenFlag, OpenMode, NULL, 0L))
{
fprintf(stderr, "\ndump: can't find %s\n", argv[1]);
exit(1);
}
// get fully qualified filename
if(!DosQPathInfo(argv[1], 5, qname, sizeof(qname), 0L))
printf("\n%s", strlwr(qname)); // and display it
while((DosRead(handle, fbuff, RECSIZE, &length) == 0)
&& (length != 0))
{
dump_rec(fbuff, foffset, length);
foffset += RECSIZE;
}
printf("\n"); // extra blank line
DosClose(handle); // close the input file
exit(0); // return success code
}
/*
Display record (16 bytes) in hex and ASCII on standard output.
*/
dump_rec(char *buffer, long foffset, int length)
{
int i; // index to current record
if(foffset % 128 == 0) // maybe print heading
printf("\n\n 0 1 2 3 4 5 6 7 8 9 A B C D E F");
printf("\n%04lX ", foffset); // file offset
for(i = 0; i < length; i++) // print hex equiv. of bytes
printf(" %02X", (unsigned char) buffer[i]);
if(length != 16) // space over if last rec.
for(i=0; i<(16-length); i++) printf(" ");
printf(" ");
for(i = 0; i < length; i++) // print ASCII equiv. of bytes
{
if(buffer[i] < 32 || buffer[i] > 126) putchar('.');
else putchar(buffer[i]);
}
}