Skip to content

Commit

Permalink
Fix handling of EOF in lsh_read_line() (brenns10#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsim0116 committed Aug 25, 2024
1 parent fdf0716 commit bb229d8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,19 @@ char *lsh_read_line(void)
// Read a character
c = getchar();

if (c == EOF) {
if (c == EOF && feof(stdin)) {
free(buffer);
exit(EXIT_SUCCESS);
} else if (c == EOF && ferror(stdin)) {
perror("getchar (EOF)");
free(buffer);
exit(EXIT_FAILURE);
} else if (c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}

buffer[position] = c;
position++;

// If we have exceeded the buffer, reallocate.
Expand Down

0 comments on commit bb229d8

Please sign in to comment.