delorie.com/archives/browse.cgi | search |
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:date:from:to:subject:message-id:reply-to | |
:references:mime-version:content-type:in-reply-to; q=dns; s= | |
default; b=PhjR/84vcHFRHCiQSUCbh5KZbCRg4OutIMBftZSfQoPKwwNYqsO11 | |
JpuecitbO9jy2KNzPiVcNvO+FYJp/5J3E4TKnszefJPhOaV+HCDeD1eyXJ02ppoP | |
O9DYV7pqLEawYy5oy2xp0djPt8mWVy5Aw0Wn0TXMoL39XUz1tJifU0= | |
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:date:from:to:subject:message-id:reply-to | |
:references:mime-version:content-type:in-reply-to; s=default; | |
bh=NKW76EqeqLQqK6XVRJJ1kogJPMs=; b=DpRLrH5rmq6V1s0CdRa+6bmsp4T1 | |
BfltNKAmnOQMmfoan8P2wTg2fZ5CP+uTZoeJUL1T2j4QPhHNvfIcw3c9qE/m2GYQ | |
yIC7IgQ9dmU78RkwgKDd+/YKhMzqJJfhum2/4Mlki6oeSVCDPSl7mx3tUQGTQQJh | |
i+7t9LBZS5EvtiI= | |
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=-5.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 |
X-HELO: | calimero.vinschen.de |
Date: | Fri, 27 Feb 2015 14:37:22 +0100 |
From: | Corinna Vinschen <corinna-cygwin AT cygwin DOT com> |
To: | cygwin AT cygwin DOT com |
Subject: | Re: freopen/fread/popen bug |
Message-ID: | <20150227133722.GM11124@calimero.vinschen.de> |
Reply-To: | cygwin AT cygwin DOT com |
Mail-Followup-To: | cygwin AT cygwin DOT com |
References: | <54EFDEF4 DOT 4060308 AT cornell DOT edu> <20150227084311 DOT GF11124 AT calimero DOT vinschen DOT de> <54F065A0 DOT 9030608 AT cornell DOT edu> |
MIME-Version: | 1.0 |
In-Reply-To: | <54F065A0.9030608@cornell.edu> |
User-Agent: | Mutt/1.5.23 (2014-03-12) |
--YIwHDYD8sUXtBKvt Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Feb 27 07:40, Ken Brown wrote: > On 2/27/2015 3:43 AM, Corinna Vinschen wrote: > >On Feb 26 22:05, Ken Brown wrote: > >>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 t= he > >>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? > > > >Wrong expectations. Keep in mind that the default read mode using > >stdio functions is buffered. So your fread fills the buffer in f. > >The buffer is typically something like 1K or 4K. If the file is > >shorter than that, the file pointer will be set to EOF when calling > >popen. Try this before calling fread: > > > > setvbuf(f, NULL, _IONBF, 0); >=20 > In the actual code that I'm debugging (part of texinfo), I think that wou= ld > create an unacceptable performance penalty for the child process. >=20 > What's really happening is that we need to peek at the first few bytes of= f > before deciding which program to call in popen. After peeking, there's a > call to fseek(f,0,0) before the popen, with the intention that the child > receives a file pointer set to the beginning of the file. This is non-portable. If the seek operation can be performed within the bytes already in the stream buffer, then fseek is not required to call lseek. This is an old discussion in how far the state of streams has to be adjusted when calling execve and what a child process may expect. There are two ways to handle it and two schools of thought, so there's no right or wrong. The only portable way to get what you want with fully buffered streams is to call fflush() on the streams before calling fork/exec (which is what popen is, basically). Corinna --=20 Corinna Vinschen Please, send mails regarding Cygwin to Cygwin Maintainer cygwin AT cygwin DOT com Red Hat --YIwHDYD8sUXtBKvt Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBAgAGBQJU8HMSAAoJEPU2Bp2uRE+gjbIP/0c+FtOOu7UJH1Ma7/Bnzy9y d9ZXUZLVDO+91S0W4Li4Ddviu1j+ZmXsCdnKbeKQe8feP+cBjFM+F42gW8Q6Hht4 Ef8yABPVWkDRipuPzcF7vCldbWQVzapRj+GR0bhSG+g2HQYZ3pF4i7iGxONNiKnJ F4Jr2jSFSLAvDxZSKNpIMqWKKYOpXxSHezZM/A7Dy7b7yw57bRyKihTURhOKCkyh V7C1p7sNNoJRAXmQeVI5iqjUW3w/OigAeKly5t88Ij2w4oftjFdW49QKMk8wXxHV 17zm5jX89P15e7jV2/AjwyL6x014mCx0KRCD42CbwW9zRKgYKnvVWjFApb+aSCuL vSuls1Ek8MnF2NSqh2n8dyWp7yGt0ZDkH7wuGn25dbut6viD7AGa8gRCcC31+M6p uZTfZhMtd2aeWqnd24LxUiFgkeDo/2Bsgzd96KDPzUQmpR8LPkl67f/KbETG/ee7 amJZbLMiXXbR7fjy1v/rb5gvuGSE4kbaHr8Hu7GtjsETkga1mrZ1+eIVqnGq2Lko VRzbXIGdpgh1hK7Lqraba+CKuehPBoQbEdtK9KzHG8zfZw7IGSOM1shi2yR1rQSf OxQHb6T3oCENuGxlgw01LWhpX7Hk3VGl6Pyv9w7lKCJbgzcj+BSltD/kB9As8jTa qqVY3xaHvBsWUKR0JPNI =mRg7 -----END PGP SIGNATURE----- --YIwHDYD8sUXtBKvt--
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |