U
user923005
...
My point is that that function is not "drop-in" compatible with gets(),
because its signature is different. That means you can't use it without
re-writing all of your code base that uses gets().
Any code using gets() is incompetently written and needs to be
rewritten.
Two notes:
1) You might argue that because it has a different name ("mygets"), it
wouldn't be drop-in compatible anyway (i.e., you'd have to edit your
code to use it). But the fact is that IRL, you'd use some trickery of
your OS/platform to allow the function name to be "over-loaded" and thus
not have to change the name. Of course, we can't talk about it (*) here.
2) The real problem with the above is in an existing code base, it may
not be obvious what value to use for "len" (without analyzing the code).
So, again, not a drop-in.
Right. Here we have a buffer of unknown size and we are reading an
arbitrary sized object into our buffer of unknown size. What two
things are wrong with this picture?
The Unix getline() function which allocates when needed is a pretty
good substitute for the basic idea of gets().