Xref: utzoo comp.unix.questions:11937 comp.unix.xenix:5162 Path: utzoo!attcan!uunet!lll-winken!csd4.milw.wisc.edu!bbn!apple!rutgers!bellcore!texbell!killer!usource!frankb From: frankb@usource.UUCP (Frank Bicknell) Newsgroups: comp.unix.questions,comp.unix.xenix Subject: Re: How to determine file being redirected to in C Summary: how to tell if two files are the same Keywords: redirection Message-ID: <169@usource.UUCP> Date: 28 Feb 89 14:59:38 GMT References: <10@sherpa.UUCP> Distribution: na Organization: UniSource, Inc., Sarasota, FL Lines: 28 In article <10@sherpa.UUCP>, rac@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. ... I presume you know how to determine whether the std* is directed to a terminal with isatty(). Your request seemed to hint at being able to determine whether the stdout and a file given in *argv[] were the same file. One way which comes to mind (this might be the hard way) is to use stat() on the file and fstat() on stdout. 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?) As for the general case of finding out the name of the file to which output (input) has been redirected to (from), that's more difficult, I think. The only way which comes to mind is to take that inode number and device id from fstat() and run system ("ncheck -i inode filesystem") on them. This takes a while, but beats rewriting the code to search through the directory structure (and your code would take as long as theirs, I would assume). -- Frank Bicknell; 1405 Main St, Ste 709; Sarasota, FL 34236-5701 killer!usource!frankb