On Fri, Nov 14, 2008 at 03:19:53PM -0600, Dee Ayy wrote:
> On Fri, Nov 14, 2008 at 2:06 AM, Steve Checkoway <email@hidden> wrote:
> > On Nov 13, 2008, at 6:06 PM, Dee Ayy wrote:
> >> When I go through my typical gdb session, it reports on the line
> >> while(fgets(stream, StreamSize, stdin) != NULL){
> >> regarding
> >> fgets
> >
> > My guess is that stream is NULL.
> That might be a good problem to have (if it's really because it's
> running too fast on the Mac and has no data to process). It had been
> running in Ubuntu on VMware Fusion on the Mac. I'll have to check if
> my "wait for more data" code is working.
I meant that you have something like
char *stream = NULL;
while( fgets(stream, StreamSize, stdin) != NULL ) { /* ... */ }
That's going to crash when it tries to write into stream.
If stdin has been closed (because the input has reached end of file or
has been explicitly closed by whatever is piping your input to your
code), then fgets() will return NULL. Otherwise, fgets() is going to
block until there is some data to return (i.e., it'll block until it
reads a newline or the file is closed--so the _next_ time you call
fgets(), it will return NULL). It should not crash.
I'm too lazy to open my laptop and run this on Mac OS X, but you can
see the behavior of calling fgets() with a NULL buffer pretty easily:
[hilbert:~] steve$ gcc -o blarg -x c - <<EOF
> #include <stdio.h>
> int main() { char *stream = NULL; fgets( stream, 1, stdin ); return
> 0; }
> EOF
[hilbert:~] steve$ ./blarg
Segmentation fault
--
Steve Checkoway
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden