How to determine file being redirected to in C

Guy Harris guy at auspex.UUCP
Fri Mar 3 19:33:47 AEST 1989


>One way which comes to mind (this might be the hard way) is
>to use stat() on the file and fstat() on stdout.

The only "easier" way might be to open the file first and do an "fstat"
on it as well; it's probably slightly more efficient.

>Then you compare st_ino and st_dev in both returned structures.  If both
>sets match (ie r.st_ino == f.st_ino && r.st_dev == f.st_dev)
>then they must be the same file.  (Network fans: is this true
>for remotely-mounted filesystems?)

It's true of the SunOS NFS implementation, and probably on all NFS
implementations derived from it.  The AT&T S5R3 RFS implementation looks
as if it attempts to preserve this property, also.  Any file system
implementation that *doesn't* preserve that property is broken; POSIX
quite explicitly says that "st_dev" and "st_ino" uniquely identify a file.



More information about the Comp.unix.questions mailing list