1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-11 11:30:13 +00:00

Fixed an error: The final linefeed got removed

git-svn-id: svn://svn.cc65.org/cc65/trunk@1857 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2002-12-29 20:19:37 +00:00
parent 46224edc85
commit b1f8ab7810

View File

@ -14,41 +14,43 @@
char* fgets (char* s, unsigned size, FILE* f) char* fgets (char* s, unsigned size, FILE* f)
{ {
int i, c; int i = 0;
int c;
/* We do not handle the case "size == 0" here */ if (size == 0) {
i = 0; --size; /* Invalid size */
while (i < size) { _errno = EINVAL;
return 0;
}
/* Read input */
i = 0;
while (--size) {
/* Get next character */ /* Get next character */
c = fgetc (f); if ((c = fgetc (f)) == EOF) {
if (c == EOF) { s[i] = '\0';
s [i] = 0;
/* Error or EOF */ /* Error or EOF */
if (f->f_flags & _FERROR) { if ((f->f_flags & _FERROR) != 0 || i == 0) {
/* ERROR */ /* ERROR or EOF on first char */
return 0; return 0;
} else { } else {
/* EOF */ /* EOF with data already read */
if (i) { break;
return s;
} else {
return 0;
}
} }
} }
/* One char more */ /* One char more */
s [i++] = c; s[i++] = c;
/* Stop at end of line */ /* Stop at end of line */
if (c == '\n') { if (c == '\n') {
break; break;
} }
} }
/* Replace newline by NUL */ /* Terminate the string */
s [i-1] = '\0'; s[i] = '\0';
/* Done */ /* Done */
return s; return s;
@ -56,4 +58,3 @@ char* fgets (char* s, unsigned size, FILE* f)