From: ed_davis2 on
I have a program that processes a list of files, as in:

foo file1 file2 ... filen

Or:

foo "*"

Additionally, output can be redirected:

foo "*" >foo.bar

How do I keep foo from (also) processing foo.bar?

Once I open foo.bar, is there a way I can tell that the handle
associated with foo.bar is redirected?

Language is C, platforms are Windows and Linux.

I'd like to do this in a platform independent way if possible.

Thanks!


From: Jens Thoms Toerring on
ed_davis2 <ed_davis2(a)yahoo.com> wrote:
> I have a program that processes a list of files, as in:

> foo file1 file2 ... filen

> Or:

> foo "*"

> Additionally, output can be redirected:

> foo "*" >foo.bar

> How do I keep foo from (also) processing foo.bar?

Speeking strictly from a UNIX perspective (I have no experience
with Windows): if foo.bar didn't exist before it won't be in
the list resulting from the expansion of '*' by the shell since
it only gets created by the shell after '*' has been dealt with.

> Once I open foo.bar, is there a way I can tell that the handle
> associated with foo.bar is redirected?

Not you are opening foo.bar, the shell does and then starts
your program with stdout redirected to foo.bar. What you could
try is calling fstat() on the stdout file descriptor (1) and
check if the inode number is the same as the one of one (or
more, don't forget hard links) of the files you got passed.
On the other hand, if the program gets started with e.g.

foo * | tee foo.bar

then even this won't help you since your program is writing
to a pipe to the 'tee' utility and not to the file - 'tee'
is doing that. And, of course, you also will have to consider
soft links...

I guess that's a difficult thing to get right in most and
nearly impossible in all cases. it probably would be a lot
simpler to simply request an output file from the user,
accepting e.g. '-' for stdout.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de
From: Ed Prochak on
On Jun 24, 2:46 pm, ed_davis2 <ed_dav...(a)yahoo.com> wrote:
> I have a program that processes a list of files, as in:
>
> foo file1 file2 ... filen
>
> Or:
>
> foo "*"
>
> Additionally, output can be redirected:
>
> foo "*" >foo.bar
>
> How do I keep foo from (also) processing foo.bar?
>
> Once I open foo.bar, is there a way I can tell that the handle
> associated with foo.bar is redirected?
>
> Language is C, platforms are Windows and Linux.
>
> I'd like to do this in a platform independent way if possible.
>
> Thanks!

The real question is why do you need this functionality? The whole
point of output redirection is to avoid adding complexity to the
application by letting the O/S and shell handle the bulk of I/O
issues. The platform independent solution is to deal with this is the
shell wrapper script.

Ed