How to determine file being redirected to in C
Guy Harris
guy at auspex.UUCP
Tue Feb 28 15:15:29 AEST 1989
>Anyway, 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),
Yes, the BSD version does. So does the System V Release 3 version, and
I think every version since the V7 version does.
However:
1) It doesn't "determine that output is being redirected"; all
it does is determine that one of the input files is the same
as the output file.
2) It doesn't do the latter by finding the path name for any of
the files; it does an "fstat" on the file (descriptor) in
question, and compares the "st_dev" and "st_ino" fields of
the results. That's all it needs to do, since the "st_dev"
and "st_ino" fields are sufficient to uniquely identify a
file; it doesn't have to find the file's name (and that
wouldn't help anyway, since the same file can have more than
one name).
So if you really want the name of the file, you'll have to work *very*
hard to get it (you basically have to scan all mounted file systems, or
at least the ones whose mount points have an "st_dev" that matches that
of the file in question), and you may not get it anyway if the input is
a pipe, or a file that has been unlinked.
More information about the Comp.unix.xenix
mailing list