Skip to content

Commit

Permalink
Merge pull request #98 from umutoztunc/fix_fgets
Browse files Browse the repository at this point in the history
Fix fgets implementation
  • Loading branch information
shinh authored Feb 12, 2021
2 parents 2c92a18 + 9c9dc46 commit c30d33f
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions libc/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,20 @@ int putc(int c, FILE* fp) {
}

char* fgets(char* s, int size, FILE* fp) {
int i;
for (i = 0; i < size - 1;) {
if (size <= 0)
return NULL;

int i = 0;
while (i < size - 1) {
int c = fgetc(fp);
if (c == EOF) {
if (c == EOF)
break;
}
s[i++] = c;
if (c == '\n') {
if (c == '\n')
break;
}
}
if (i) {
s[i] = 0;
return s;
} else {
return 0;
}
s[i] = 0;
return s;
}

#endif // ELVM_LIBC_STDIO_H_

0 comments on commit c30d33f

Please sign in to comment.