delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2015/02/26/22:05:47

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:content-type:content-transfer-encoding; q=dns; s=default; b=I/J
XnTaTs8BdUNek/nLVFcwnIvBL90yykOZ91U68pa3SZiY01lnwldFD5vtpwgqmcee
Q6ig2SxcKq2YCKUbTrsB1gIzfJaicTiFXIEOYWDWPOV8BE/YuL9HyqEeXmPW6oOj
HoT+0u3AZCOkaGxp2OkXA7lMlllKYrVsE7pvBGuk=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:content-type:content-transfer-encoding; s=default; bh=yJXVwAqCd
Lam2h8zoEeWYVhhYDk=; b=dHscrAt3X6c5k8WpjzYyLS1Ke7GDjk6duI7cFGxIO
2LA+r1yGRwdCnP+ofbRkccYJwPv1hEkOnWrKJ9NnrjAUWjmOFrTkzyeycKJPvYVd
8SD55eLR1y0mCDwgALt+nwwp1PFehkWN9cbBNs4TGQfC4zpv1kTT23gU9gqqPOJQ
as=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2
X-HELO: limerock03.mail.cornell.edu
X-CornellRouted: This message has been Routed already.
Message-ID: <54EFDEF4.4060308@cornell.edu>
Date: Thu, 26 Feb 2015 22:05:24 -0500
From: Ken Brown <kbrown AT cornell DOT edu>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
MIME-Version: 1.0
To: cygwin <cygwin AT cygwin DOT com>
Subject: freopen/fread/popen bug
X-IsSubscribed: yes

I'm not sure exactly where the bug is, but here's what happens (STC at the end):

1. I use freopen to open a file "foo" and associate it with stdin.

2. I use fread to read a byte from foo.

3. I call popen, expecting the child process to have foo as its stdin, with the 
file-position indicator pointing to the second byte.  But instead the child sees 
an empty stdin.

If I omit step 2, the child process does indeed have foo as its stdin.  Are my 
expectations wrong, or is this a bug?

Ken

STC:

$ cat foo
Contents of foo

$ cat good.c
#include <stdio.h>
#include <string.h>
#include <errno.h>

int
main ()
{
   FILE *f;
   int nread;
   char data[100];
   char *filename = "foo";

   f = freopen (filename, "r", stdin);
   if (!f)
     {
       fprintf (stderr, "Can't freopen %s: %s\n", filename, strerror (errno));
       return 1;
     }
   f = popen ("cat", "r");
   if (!f)
     {
       fprintf (stderr, "popen failed: %s\n", strerror (errno));
       return 1;
     }
   nread = fread (data, 1, 50, f);
   if (nread < 1)
     {
       fprintf (stderr, "Nothing read.\n");
       return 1;
     }
   data[nread] = '\0';
   printf ("%s", data);
   return 0;
}

$ gcc good.c -o good

$ ./good.exe
Contents of foo

[As expected.]

$ cat bad.c
#include <stdio.h>
#include <string.h>
#include <errno.h>

int
main ()
{
   FILE *f, *g;
   int nread;
   char data[100];
   char *filename = "foo";

   f = freopen (filename, "r", stdin);
   if (!f)
     {
       fprintf (stderr, "Can't freopen %s: %s\n", filename, strerror (errno));
       return 1;
     }
   nread = fread (data, 1, 1, f);
   if (nread != 1)
     {
       fprintf (stderr, "fread failed\n");
       return 1;
     }
   g = popen ("cat", "r");
   if (!g)
     {
       fprintf (stderr, "popen failed: %s\n", strerror (errno));
       return 1;
     }
   nread = fread (data, 1, 50, g);
   if (nread < 1)
     {
       fprintf (stderr, "Nothing read.\n");
       return 1;
     }
   data[nread] = '\0';
   printf ("%s", data);
   return 0;
}

$ gcc bad.c -o bad

$ ./bad.exe
Nothing read.

[Bug?]

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019