bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] in-place edit request


From: Andrew J. Schorr
Subject: Re: [bug-gawk] in-place edit request
Date: Sun, 23 Dec 2012 13:23:34 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

On Sun, Dec 23, 2012 at 12:07:56PM -0600, address@hidden wrote:
> If stdout is connected to a terminal, may be reopening /dev/tty or /dev/fd/1
> will work.
>
>  OR
> 
> use dup() to duplicate stdout first before freopen(). I don't think gawk
> uses printf or even fprintf for output. But, stdout may be hardcoded
> in places (e.g close).

Using dup and /dev/fd may work, but I am concerned about portability.

> In that case, if someone is really ambitious can try
> 
>       *stdout = *fp
> 
> and/or use fgetpos/fsetpos for extra insurance.

Since stdout is declared as 'extern FILE *stdout', one could potentially say:

   stdout = fp;

However, my googling suggests that stdout need not be an assignable lvalue.  In
a test I just ran, reassigning stdout does seem to work on Linux:

#include <stdio.h>

int
main(int argc, char **argv)
{
   FILE *fp;
   char instr[1024];

   if (!(fp = fopen(argv[1], "w"))) {
      fprintf(stderr, "fopen(%s) failed\n", argv[1]);
      return 1;
   }
   stdout = fp;
   while (fgets(instr, sizeof(instr), stdin))
      fputs(instr, stdout);
   return 0;
}

But I do not believe that this is a supported usage.  It does not seem to work
on Cygwin.  I also tried '*stdout = *fp' on Cygwin, and that also did not
work for me.

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]