The danger with fgets() is that it simply truncates over-long input. For
some applications, this can result in reasonable-seeming but wrong values,
which is the worst possible case (if you send a gas bill for six billion
dollars to an old granny then it is merely embarssing, if you send a bill
for two hundred and thirty dollars when the real amount is one hundred and
ninety, you could easily end up in court facing awkward questions).
Perhaps you just need to know how to use fgets() appropriately:
consider:
$ cat sample.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_input(void)
{
char *ptr = 0;
char buff[10] = {0};
size_t len = 0;
fputs("--> ", stdout);
fflush(stdout);
while( buff[strlen(buff) -1 ] != '\n')
{
fgets(buff, sizeof buff, stdin);
len += strlen(buff);
ptr = realloc(ptr, len+1); /* error checking omitted */
strcat(ptr, buff);
}
if(ptr)
ptr[len-1] = '\0'; /* strip trailing newline */
return ptr;
}
int main()
{
while( !feof(stdin) )
{
char *ptr = get_input();
if(ptr)
{
printf( "input was: \"%s\" : %d characters\n",
ptr, strlen(ptr));
free(ptr);
}
}
return 0;
}
$ gcc sample.c -o sample -Wall -W -pedantic -ansi
$ sample
--> a
input was: "a" : 1 characters
--> abcdefgji
input was: "abcdefgji" : 9 characters
--> abcdefghijklmnopqrstuvwxyz
input was: "abcdefghijklmnopqrstuvwxyz" : 26 characters
-->
It doesn't appear that "fgets() is truncating over long input."
At least not on my system.
If this were going into production, I'd want to handle feof() more
gracefully, I'd definitely want to do something if realloc() failed,
and I would set my input buffer to a reasonably large number (perhaps
1024), so that I'm only calling realloc once for most input. But I
don't have to worry about buffer over-runs, or (within the limits of
alloc()) truncating user input.