How to determine file being redirected to in C

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Feb 28 14:28:28 AEST 1989


In article <10 at sherpa.UUCP> rac at sherpa.UUCP (Roger A. Cornelius) writes:
>... can someone tell me how to determine (in C)
>if output is being redirected, or more precisely, how to determine the
>file output is being redirected to.  The cat command does this (at
>least the Xenix version of cat, I don't think the BSD version does),

What "cat" actually does is compare the device/inode numbers
for equality; it cannot determine "the" filename because there
may not be one, or it may be ambiguous (and would be expensive
to figure out anyway).  The device/inode numbers are obtained
from the fstat() system call.  These days, the system ID should
also be compared.

To determine a filename, assuming you can, for the inode
behind a given file descriptor, is a tedious process.  Your
best bet is to find another solution to whatever your problem
is.



More information about the Comp.unix.questions mailing list