Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gets()

0 views
Skip to first unread message

Brian E Rhodefer

unread,
Jun 24, 1992, 8:23:33 PM6/24/92
to
Forgive me if this is a FAQ, but our copy of the FAQanswers evaporated...

Why isn't there a "safe" version of the gets() function that WON't
write past the end of its buffer, even if the stream it's reading
doesn't contain sufficient newline characters?

The strcpy() and strcmp() functions have their "n" counterparts: strncpy(),
strncmp() - why not gets()?

Brian Rhodefer

Barry Margolin

unread,
Jun 24, 1992, 10:44:43 PM6/24/92
to
In article <58...@tekig7.PEN.TEK.COM> bri...@tekig1.PEN.TEK.COM (Brian E Rhodefer) writes:
>Why isn't there a "safe" version of the gets() function that WON't
>write past the end of its buffer, even if the stream it's reading
>doesn't contain sufficient newline characters?

There is: fgets(). It also requires you to supply the stream as an
argument, but that's not much trouble.
--
Barry Margolin
System Manager, Thinking Machines Corp.

bar...@think.com {uunet,harvard}!think!barmar

Kurt Cockrm 762-6417

unread,
Jun 27, 1992, 3:32:03 PM6/27/92
to
In article <58...@tekig7.PEN.TEK.COM> bri...@tekig1.PEN.TEK.COM (Brian E Rhodefer) writes:
>Forgive me if this is a FAQ, but our copy of the FAQanswers evaporated...
>
>Why isn't there a "safe" version of the gets() function that WON't
>write past the end of its buffer, even if the stream it's reading
>doesn't contain sufficient newline characters?

There is (sort of): see the fgets() function manual page [fgets(3)].
It's not quite the same as gets(). The *real* question is: *why*, oh why,
does this bug-function *still* live? Why can't it die? It's even inside
the ANSI C library. Why?? The only reason I can figure is political finagling,
but that doesn't really answer the question. I should think that everybody
would be happy to get rid of it. I can only suppose that it must be due to
the efforts of some stodgy cardiac pace-maker manufacturer who doesn't want
to change their dusty-deck code :) ... Perhaps P. J. Plauger, who has given
insight into the internal processes of the ANSI standardization committee, can
help...

>The strcpy() and strcmp() functions have their "n" counterparts: strncpy(),
>strncmp() - why not gets()?

gets() is not part of the string library.
I don't know who wrote the string library. Dennis Ritchie wrote the stdio
library. I suspect that you are seeing the effects of different people
designing different libraries.

>Brian Rhodefer

ku...@grogatch.celestial.com (Kurt Cockrum)

0 new messages