From owner-freebsd-hackers@FreeBSD.ORG Thu Aug 7 16:25:08 2008 Return-Path: Delivered-To: hackers@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5A7A9106568C for ; Thu, 7 Aug 2008 16:25:08 +0000 (UTC) (envelope-from jdc@parodius.com) Received: from mx01.sc1.parodius.com (mx01.sc1.parodius.com [72.20.106.3]) by mx1.freebsd.org (Postfix) with ESMTP id 47E598FC0C for ; Thu, 7 Aug 2008 16:25:08 +0000 (UTC) (envelope-from jdc@parodius.com) Received: by mx01.sc1.parodius.com (Postfix, from userid 1000) id 153D01CC0B0; Thu, 7 Aug 2008 09:25:08 -0700 (PDT) Date: Thu, 7 Aug 2008 09:25:08 -0700 From: Jeremy Chadwick To: Gabor Kovesdan Message-ID: <20080807162508.GA37318@eos.sc1.parodius.com> References: <489B0ACD.80008@kovesdan.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <489B0ACD.80008@kovesdan.org> User-Agent: Mutt/1.5.18 (2008-05-17) Cc: fjoe@FreeBSD.org, hackers@FreeBSD.org Subject: Re: strange issue reading /dev/null X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Aug 2008 16:25:08 -0000 On Thu, Aug 07, 2008 at 04:46:37PM +0200, Gabor Kovesdan wrote: > Hello, > > I'm wondering why fgetc() returns 0xff if called with /dev/null: > > #include > #include > > int > main(void) > { > int c; > FILE *f; > > f = fopen("/dev/null", "r"); > > if (c != EOF) > printf("%c\n", fgetc(f)); > } > > > gcc foo.c > > ./a.out > ÿ > > This causes a bug in BSD grep as /dev/null is not distinguished from > ordinary files in the code, thus I was expecting it just returned EOF, > but in reality this is not the case. How such cases should be handled? Your code is wrong -- you're not calling feof(). Please read the RETURN VALUES section of fgetc(3) in full, and slowly. :-) And your if() statement serves no purpose there. -- | Jeremy Chadwick jdc at parodius.com | | Parodius Networking http://www.parodius.com/ | | UNIX Systems Administrator Mountain View, CA, USA | | Making life hard for others since 1977. PGP: 4BD6C0CB |