Mail Archives: djgpp-workers/2003/05/26/14:42:33
ggets (and fggets) is my (public domain) implementation of a safe
gets(). It is available at:
<http://cbfalconer.home.att.net/download/ggets.zip>
The following is an extract from ggets.h:
/* fggets and ggets [which is fggets(ln, stdin)] set *ln to
a buffer filled with the next complete line from the text
stream f. The storage has been allocated within fggets,
and is normally reduced to be an exact fit. The trailing
\n has been removed, so the resultant line is ready for
dumping with puts. The buffer will be as large as is
required to hold the complete line.
Note: this means a final file line without a \n terminator
has an effective \n appended, as EOF occurs within the read.
If no error occurs fggets returns 0. If an EOF occurs on
the input file, EOF is returned. For memory allocation
errors some positive value is returned. In this case *ln
may point to a partial line. For other errors memory is
freed and *ln is set to NULL.
Freeing of assigned storage is the callers responsibility
*/
int fggets(char* *ln, FILE *f);
#define ggets(ln) fggets(ln, stdin)
------------- end extract ----------
The interface is designed to minimize the requirements on the
caller, which is why newbies use gets in the first place. Thus I
rejected any pre-assigned buffer, which would prevent guaranteeing
that the returned value is suitable for free() or realloc(). To
separate EOF and other errors from the normal return requires more
than a NULL error return. Like gets the returned string *always*
has the terminating \n stripped barring an unterminated final line
which has no such to strip.
Although no other system known to me includes this routine, why
shouldn't DJGPP pioneer here? Maybe it will catch on. It is not
a large chunk of code.
--
Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT worldnet DOT att DOT net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
- Raw text -