C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
TYPEIT.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* TYPEIT.C illustrates reassigning handles and streams using functions:
 *      freopen         _dup             _dup2
 *
 * The example also illustrates:
 *      _setargv
 *
 * To make the program handle wildcards, link it with the SETARGV.OBJ
 * file. You can do this in the PWB environment by creating a program list
 * containing TYPEIT.C and SETARGV.OBJ (include the path or put in current
 * directory). You must also turn off the Extended Dictionary flag
 * within the PWB environment or use the /NOE linker option outside the
 * environment. For example:
 *    CL typeit.c setargv /link /NOE
 */
 
#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <process.h>
 
void main( int argc, char **argv )
{
    FILE *ftmp;
    int htmp;
 
    /* Duplicate handle of stdin. Save the original to restore later. */
    htmp = _dup( _fileno( stdin ) );
 
    /* Process each command line argument. */
    while( *(++argv) != NULL )
    {
        /* Original stdin used for _getch. */
        printf( "Press any key to display file: %s\n", *argv );
        _getch();
 
        /* Reassign stdin to input file. */
        ftmp = freopen( *argv, "rb", stdin );
 
        if( (ftmp == NULL) || (htmp == -1 ) )
        {
            _dup2( htmp, _fileno( stdin ) );
            perror( "Can't reassign standard input" );
            exit( 1 );
        }
 
        /* Spawn MORE, which will receive open input file as its standard
         * input. MORE can be the DOS MORE or the sample program MORE.C.
         */
        _spawnlp( _P_WAIT, "MORE", "MORE", NULL );
 
        /* Reassign stdin back to original so that we can use the
         * original stdin to get a key.
         */
        _dup2( htmp, _fileno( stdin ) );
    }
    exit( 0 );
}
                                    -♦-