So I was reading a book on writing a shell in C and I want to try to write it in C++. I came across the following code:
for( ; ; )
{
if (fputs(PROMPT, stdout) == EOF)
continue;
if (fgets(inbuf, MAX, stdin) == NULL)
continue;
//and so on....
}
I don't understand the usage of
fputs()
here.(a) if stdout is a terminal, does EOF have any meaning? What kind of errors can you get writing to a terminal except maybe the stream is already closed?
(b) if stdout was previously redirected and is really a pipe or file then several different errors seem possible. Where are they listed? See (c) below.
(c) following (b) above, ferror() doesn't seem that helpful. Does its return values map to those of errno and thus the same as using something like perror()? Where are the constants kept in order to do something like
if (ferror() == SYSTEM_ERROR_13)
(d) in the code above, if fputs() did return an error why would a "continue" work? Wouldn't the error need to be cleared first with something like clearerr() or it would just repeatedly fail?
Is the equivalent code in C++:
for( ; ; ) { if (! cout << PROMPT) { cout.clear(); continue; } if (! getline(cin, inbuf)) { cin.clear(); continue; } //and so on.... }