From owner-freebsd-standards@FreeBSD.ORG Sat May 21 20:52:29 2005 Return-Path: Delivered-To: freebsd-standards@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 12F6C16A4CE for ; Sat, 21 May 2005 20:52:29 +0000 (GMT) Received: from aiolos.otenet.gr (aiolos.otenet.gr [195.170.0.23]) by mx1.FreeBSD.org (Postfix) with ESMTP id 696D643D1F for ; Sat, 21 May 2005 20:52:28 +0000 (GMT) (envelope-from keramida@freebsd.org) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.27]) j4LKqQAX003339; Sat, 21 May 2005 23:52:26 +0300 Received: from gothmog.gr (patr530-a033.otenet.gr [212.205.215.33]) j4LKoZNq014454; Sat, 21 May 2005 23:50:36 +0300 Received: from gothmog.gr (gothmog [127.0.0.1]) by gothmog.gr (8.13.3/8.13.3) with ESMTP id j4LKqMix001516; Sat, 21 May 2005 23:52:22 +0300 (EEST) (envelope-from keramida@freebsd.org) Received: (from giorgos@localhost) by gothmog.gr (8.13.3/8.13.3/Submit) id j4LKqMEH001515; Sat, 21 May 2005 23:52:22 +0300 (EEST) (envelope-from keramida@freebsd.org) Date: Sat, 21 May 2005 23:52:22 +0300 From: Giorgos Keramidas To: Steve Kargl Message-ID: <20050521205222.GA1014@gothmog.gr> References: <20050521193605.GB51782@troutmask.apl.washington.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050521193605.GB51782@troutmask.apl.washington.edu> cc: freebsd-standards@freebsd.org Subject: Re: Read /dev/null differs from POSIX X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 May 2005 20:52:29 -0000 On 2005-05-21 12:36, Steve Kargl wrote: > >From http://www.opengroup.org/onlinepubs/009695399/toc.htm > > /dev/null -- An infinite data source and data sink. Data > written to /dev/null shall be discarded. Reads from > /dev/null shall always return end-of-file (EOF). > > This program > > #include > int main(void) { > int i=0,j; > FILE *fp; > fp = fopen("/dev/null", "r"); > while(fread(&j, sizeof(int), 1, fp) != 1) { > i++; > if (i == 5) break; > } The condition of the while loop above is bogus. fread() of any amount of data from /dev/null returns 0 items, but the loop will still iterate, increasing i as if some data was read. Rewriting the loop as shown below reveals that /dev/null indeed works as expected: for (i = 0; fread(&j, sizeof(j), 1, fp) == 1; i++) printf("step %d read %lu bytes\n", i, (unsigned long)sizeof(j)); This *NEVER* prints a "step X read Y bytes" message from /dev/null. > if (j == EOF) > printf("EOF\n"); > else > printf("j = %d\n", j); This is bogus. fread() doesn't set the output buffer to EOF but marks the `fp' stream as end-of-file'd. The correct way to check for end-of-file on stdio.h streams is: if (feof(fp)) ... ;