Improve error handling in getline():

- always check the return value from getc(3) for EOF;
- if the attempt to read the TELNET command byte has
  returned EOF, exit from the loop instead of using
  the EOF value as a normal character.

MFC after:	1 week
This commit is contained in:
Yaroslav Tykhiy 2003-07-09 13:15:32 +00:00
parent 4d4e204a98
commit 39b96ba75d

View File

@ -1187,18 +1187,21 @@ getline(char *s, int n, FILE *iop)
while ((c = getc(iop)) != EOF) {
c &= 0377;
if (c == IAC) {
if ((c = getc(iop)) != EOF) {
if ((c = getc(iop)) == EOF)
goto got_eof;
c &= 0377;
switch (c) {
case WILL:
case WONT:
c = getc(iop);
if ((c = getc(iop)) == EOF)
goto got_eof;
printf("%c%c%c", IAC, DONT, 0377&c);
(void) fflush(stdout);
continue;
case DO:
case DONT:
c = getc(iop);
if ((c = getc(iop)) == EOF)
goto got_eof;
printf("%c%c%c", IAC, WONT, 0377&c);
(void) fflush(stdout);
continue;
@ -1207,12 +1210,12 @@ getline(char *s, int n, FILE *iop)
default:
continue; /* ignore command */
}
}
}
*cs++ = c;
if (--n <= 0 || c == '\n')
break;
}
got_eof:
if (c == EOF && cs == s)
return (NULL);
*cs++ = '\0';