How to determine file being redirected to in C

Guy Harris guy at auspex.UUCP
Thu Mar 2 06:01:23 AEST 1989


>In BSD UNIX, you can fstat(2) stdout to determine if the output is a
>file or terminal (or whatever) and, what inode number it has.

In *any* UNIX worth of the name, you can do that; I know of none so
feeble-minded that they don't allow that.

>Since several file names can have the same inode, you can't tell exactly
>what file name was specified, but the inode number should be enough
>information for cat and other such applications.

It's certainly almost enough for "cat", since all it cares about is
whether any of the input files are the same as the output file or not. 
The inode number isn't enough, however; you also need the file system
identifier, which is in the "st_dev" field.  If two files have the same
"st_dev" and "st_ino", they're the same file, otherwise not.



More information about the Comp.unix.questions mailing list