/* ** hexdump.c - Program to dump the contents of a file to hexadecimal ** ** File created: 8/8/2000 ** Last revision: 8/10/2000 ** ** Copyright (C) 2000 Sean R. Wells ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation; either version 2 of the License, or ** (at your option any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ** */ #include typedef struct arg_info { FILE *infile; int width; int suppress_offset; int suppress_text; } arg_info; int InitializeArgs ( arg_info *arguments ) { ( *arguments ).width = 16; ( *arguments ).suppress_offset = 0; ( *arguments ).suppress_text = 0; return ( 0 ); } int ProcessArgs ( int argc, char **argv, arg_info *arguments ) { register FILE *input_file; int i; if ( argc < 2 ) { fprintf ( stderr, "hexdump version 1.0b Copyright (C) 2000 Sean R. Wells\n"); fprintf ( stderr, "Usage: hexdump [-w #columns] [-st] [so]\n" ); fprintf ( stderr, " -w #columns sets the width of the output\n" ); fprintf ( stderr, " -st suppresses the text interpretation\n" ); fprintf ( stderr, " -so suppresses printing of offset\n" ); return ( -1 ); } input_file = fopen ( argv[1], "rb" ); if ( !input_file ) { fprintf ( stderr, "ERROR - could not open input file\n" ); return ( -1 ); } ( *arguments ).infile = input_file; for ( i = 2; i < argc; i++ ) { if ( ( strcmp ( argv[i], "-w" ) == 0 ) && ( argc > i ) ) { ( *arguments ).width = atoi ( argv[i+1] ); } if ( strcmp ( argv[i], "-so" ) == 0 ) { ( *arguments ).suppress_offset = 1; } if ( strcmp ( argv[i], "-st" ) == 0 ) { ( *arguments ).suppress_text = 1; } } return ( 0 ); } int DumpFile ( arg_info arguments ) { int i, i2; int ch; unsigned char text_conv[128]; long int file_size = 0; ch = getc ( arguments.infile ); while ( ch != EOF ) { if ( arguments.suppress_offset == 0 ) printf ( "0x%3x ", file_size ); for ( i = 0; ( i < arguments.width ) && ( ch != EOF ); i++ ) { if ( ( unsigned char ) ch < 16 ) printf ( "0" ); printf ( "%x ", ( unsigned char ) ch ); if ( ch > 31 ) text_conv[i] = ( unsigned char ) ch; else text_conv[i] = '.'; text_conv[i+1] = '\0'; ch = getc ( arguments.infile ); file_size++; } for ( i2 = i; i2 < arguments.width; printf ( " " ), i2++ ); if ( arguments.suppress_text == 0 ) printf ( "%s", text_conv ); printf ( "\n" ); } fclose ( arguments.infile ); return ( 0 ); } int main ( argc, argv ) int argc; char **argv; { long int file_size = 0; arg_info arguments; InitializeArgs ( &arguments ); if ( ProcessArgs ( argc, argv, &arguments ) == -1 ) return ( 0 ); file_size = DumpFile ( arguments ); return ( 0 ); }