Xref: utzoo comp.unix.questions:11953 comp.unix.xenix:5173 Path: utzoo!attcan!uunet!lll-winken!csd4.milw.wisc.edu!mailrus!uflorida!haven!adm!cmcl2!phri!marob!daveh From: daveh@marob.MASA.COM (Dave Hammond) Newsgroups: comp.unix.questions,comp.unix.xenix Subject: Re: How to determine file being redirected to in C Keywords: redirection Message-ID: <574@marob.MASA.COM> Date: 1 Mar 89 12:35:40 GMT References: <10@sherpa.UUCP> Reply-To: daveh@marob.masa.com (Dave Hammond) Followup-To: comp.unix.questions Distribution: na Organization: ESCC New York City Lines: 22 In article <10@sherpa.UUCP> rac@sherpa.UUCP (Roger A. Cornelius) writes: >.................. 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 [...] > > cat catfile > catfile >produces the error "cat: input catfile is output". 1. Determine if your stdout has been redirected (isatty(fileno(stdout)) will fail if stdout is redirected). 2. Do an fstat() on fileno(stdout), and a stat() on the other file in question. 3. Compare the st_ino and st_dev members of the two stat structures. If the inode (st_ino) and device-of-directory-entry (st_dev) for the two match, your standard output and the other file are the same. Hope this helps. -- Dave Hammond daveh@marob.masa.com