Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for input on stdin? 1

Status
Not open for further replies.

msc0tt

IS-IT--Management
Jun 25, 2002
281
CA
I wish to write a 'virtual tapedrive' for use on my Linux system (gcc 3.2.2). The code will be your basic I/O filter (with enhancements of course). If I call it 'vtape', here is how I imagine using it:

Backup:
tar -c /home | vtape
Restore:
vtape | tar -x

Within vtape.c, can I tell which mode the program has been called? In the first case, vtape will slurp in the data stream from stdin (nothing to stdout). In the second case, there is no stdin so it will spew out a data stream to stdout.
Get it?

-with thanks.
 
This seems to work
Code:
#include <stdio.h>
#include <unistd.h>

int main ( void ) {
    if ( isatty(0) ) {
        /* stdin is a terminal, so guess stdout is redirected */
        fprintf( stderr, "vtape | tar -x\n" );
    }
    if ( isatty(1) ) {
        /* stdout is a terminal, so guess stdin is redirected */
        fprintf( stderr, "tar -c /home | vtape\n" );
    }
    return 0;
}

--
 
Excellent. Just tested this and it does indeed do what I need. -many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top