Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 7 Aug 2008 11:00:03 -0500 (CDT)
From:      "Sean C. Farley" <scf@FreeBSD.org>
To:        Gabor Kovesdan <gabor@kovesdan.org>
Cc:        fjoe@FreeBSD.org, hackers@FreeBSD.org
Subject:   Re: strange issue reading /dev/null
Message-ID:  <alpine.BSF.1.10.0808071058020.1056@thor.farley.org>
In-Reply-To: <489B0ACD.80008@kovesdan.org>
References:  <489B0ACD.80008@kovesdan.org>

next in thread | previous in thread | raw e-mail | index | archive | help
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--56599777-1628545831-1218124803=:1056
Content-Type: TEXT/PLAIN; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 8BIT

On Thu, 7 Aug 2008, Gabor Kovesdan wrote:

> Hello,
>
> I'm wondering why fgetc() returns 0xff if called with /dev/null:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> 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?

You are testing c which has not been set.  It works OK if you set c then
do the test:

+       c = fgetc(f);
         if (c != EOF)
-               printf("%c\n", fgetc(f));
+               printf("%c\n", c);

Sean
-- 
scf@FreeBSD.org
--56599777-1628545831-1218124803=:1056--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?alpine.BSF.1.10.0808071058020.1056>