From: eah1@gauguin.wustl.edu (Edward Anthony Hutchins)
Subject: gcc + fopen() = bug?
Organization: Washington University, School of Engineering, St. Louis MO
Date: Thu, 9 Apr 1992 21:45:08 GMT

Can anyone tell me why the fopen() doesn't work??? (I'm using osk and
the gcc compiler). Thanks in advance! BTW, I will NOT go back to using
a mondo-retardo compiler like os-9's standard cc! I'd drop down to
assembly first! :-) P.S. this code works like a charm on my IBM/386 under
MSDOS 5.0, on my Amiga under AmigaDOS (which rules compared to os-9 :-)
and on my NeXT under Mach/UNIX (which is almost as good as AmigaDOS, only
S-L-O-W-E-R).

--- cut here ---
/*
** XD - hex dump
*/

#include <stdio.h>
#include <ctype.h>

/*
** typedefs and defines
*/

#define VERSION "1.1"

typedef enum { TRUE = 1, FALSE = 0 } BOOL;

/* so why the hell were these not defined in stdio.h??? */
enum { SEEK_SET, SEEK_CUR, SEEK_END };

/*
** ShowHelp - print a help page
*/

void ShowHelp( void )
{
    fputs( "xd version " VERSION " Copyleft (C) 1990 Hutchins Software\n"
    "usage: \tXD [<-|/><opt>...] [<file>]\n"
    "\t-h|?\tshow this help page\n"
    "\t-d\tdisplay decimal line numbers\n"
    "\t-o\tomit the ASCII display\n"
    "\t-s <n>\tstart at byte <n>\n"
    "\t-n <n>\tdisplay <n> bytes\n", stderr );
}

/*
** main - do it!
*/

void main( int argc, char *argv[] )
{
    char    c;
    char    *b;
    int     nPos = 1;
    long    lCnt, lStart, lNum;
    FILE    *fIn = stdin;
    BOOL    bOmitASCII = FALSE;
    BOOL    bNumDec = FALSE;
    BOOL    bDumpFile = FALSE;
    char    buff[32];

    lCnt = lStart = lNum = 0;

    while (nPos < argc)
    {
        b = argv[nPos++];
        if (*b == '-' || *b == '/')
        {
            while (*(++b)) switch (tolower( *b ))
            {
            case 'd':
                bNumDec ^= TRUE;
                break;

            case 'o':
                bOmitASCII ^= TRUE;
                break;

            case 's':
                if (b[1])
                {
                    lStart = atol( &b[1] );
                    while (isdigit( b[1] ) || b[1] == '-') ++b;
                    break;
                }
                if (nPos > argc)
                {
                    fputs( "ERROR - expecting a start number\n", stderr );
                    exit( 10 );
                }
                lStart = atol( argv[nPos++] );
                break;

            case 'n':
                if (b[1])
                {
                    lNum = atol( &b[1] );
                    while (isdigit( b[1] ) || b[1] == '-') ++b;
                    break;
                }
                if (nPos > argc)
                {
                    fputs( "ERROR - expecting a count number\n", stderr );
                    exit( 10 );
                }
                lNum = atol( argv[nPos++] );
                break;

            default:
            case 'h':
            case '?':
                ShowHelp();
                exit( 0 );
            }
        }
        else /* must be a file, so dump it */
        {
            printf( "Hex dump of %s\n", b );
            fIn = fopen( b, "rb" );
            if (fIn == NULL)
            {
                printf( "ERROR - file not opened\n" );
                exit( 0 );
            }
            bDumpFile = TRUE;
        }
    }

    b = buff;
    nPos = 0;

    if (bDumpFile && lStart)
    {
        if (lStart > 0) fseek( fIn, lStart, SEEK_SET );
        else fseek( fIn, -lStart, SEEK_END );
        lCnt = ftell( fIn );
        if (lCnt != lStart)
        {
            printf( "ERROR - seek failed\n" );
            fclose( fIn );
            exit( 0 );
        }
    }

    /* stop when these two are equal */
    lNum += lCnt;

    c = fgetc( fIn );
    while (!feof( fIn ))
    {
        if (nPos == 0)
        {
            if (bNumDec) printf( "%10ld: ", lCnt );
            else printf( "%10lx: ", lCnt );
        }

        if (nPos == 7)
            printf( "%2.2x-", (0xFF & (unsigned int)c) );
        else
            printf( "%2.2x ", (0xFF & (unsigned int)c) );

        *b++ = ((isprint( c )) ? c : ' ');
        ++lCnt;

        if (++nPos == 16)
        {
            *b = '\0'; b = buff; nPos = 0;
            if (bOmitASCII) printf( "\n" );
            else printf( "|%16.16s|\n", buff );
        }

        if (lCnt == lNum) break;
        c = fgetc( fIn );
    }

    if (nPos)
    {
        while (nPos++ < 16)
        {
            printf( "   " );
            *b++ = ' ';
        }
        *b = '\0';
        if (bOmitASCII) printf( "\n" );
        else printf( "|%16.16s|\n", buff );
    }

    if (bDumpFile) fclose( fIn );
}
--- cut here ---

P.P.S. I hope you find this little utility useful...

	- Ed
_____________________________________________________________________________
:-)  ('')  (-:  (,,)  :-)  ('')  (-:  (,,)  :-) | see lidflip instructions on
Edward Hutchins, eah1@cec1.wustl.edu            | other side of card