Bug 000066

When Created: 03/12/1996 10:41:19
Against DJGPP version: 2.00
By whom:
Abstract: system() redirection: output can end up in the wrong file
I've come across a bug in the redirection of output from system()
calls. To see the problem, you need two simple programs:

/*--------- a.c --------- */
#include <stdlib.h>
#include <stdio.h>
int main() {
  printf("Hello from a\n");
  system("b > bbb\n");
/*------ end of a.c ------*/

/*--------- b.c --------- */
#include <stdio.h>
int main() {
  printf("Hello from b\n");
/*------ end of b.c ------*/

If you run

c:\> a

then the expected happens: "Hello from a" is printed on the terminal and
"Hello from b" appears in bbb. However, if you run

c:\> a > aaa

you expect "Hello from a" to appear in aaa, and "Hello from b" in bbb.
In fact, aaa is empty and bbb contains both lines. This program works
fine under V1. The same bug also appears if you execute

        system("a > aaa");

in a third program (which is the situation in which I actually
encountered the problem).

Workaround added: 03/12/1996 10:45:33
By whom:
The bug can be avoided by adding an explicit call to fflush before the
system() call. (Thanks to Eli Zaretskii)

As Eli pointed out, that doesn't completely explain things as the 

in the printf should cause an fflush because it is line buffered.

Solution added: 04/05/1996 04:41:44
By whom:
system() should fflush stdout before it calls dup2 to redirect it.
That's because dup2 will close the handle, but any bytes left in the
DJGPP buffers won't be written when the handle is closed, and will
instead wind up in the redirectred file.

Fixed in version 2.01 on 06/13/1996 00:06:51
By whom:

  webmaster     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010