warn line input overflow

This commit is contained in:
Yuichi Nishiwaki 2013-10-09 17:14:48 +09:00
parent 3d70907833
commit 9d33e0e079
1 changed files with 11 additions and 1 deletions

View File

@ -1,9 +1,11 @@
#include <stdio.h> #include <stdio.h>
#define LINE_MAX_LENGTH 256
int int
main() main()
{ {
char line[256], last_char; char line[LINE_MAX_LENGTH], last_char;
int char_index; int char_index;
while (1) { while (1) {
@ -13,6 +15,8 @@ main()
while ((last_char = getchar()) != '\n') { while ((last_char = getchar()) != '\n') {
if (last_char == EOF) if (last_char == EOF)
goto eof; goto eof;
if (char_index == LINE_MAX_LENGTH)
goto overflow;
line[char_index++] = last_char; line[char_index++] = last_char;
} }
line[char_index] = '\0'; line[char_index] = '\0';
@ -25,6 +29,12 @@ main()
eof: eof:
puts(""); puts("");
goto exit;
overflow:
puts("** [fatal] line input overflow");
goto exit;
exit:
return 0; return 0;
} }