mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibC: fgets() shouldn't stop on '\0'
This commit is contained in:
parent
f89944e804
commit
8b0d530584
1 changed files with 9 additions and 11 deletions
|
@ -105,24 +105,22 @@ int fflush(FILE* stream)
|
||||||
|
|
||||||
char* fgets(char* buffer, int size, FILE* stream)
|
char* fgets(char* buffer, int size, FILE* stream)
|
||||||
{
|
{
|
||||||
assert(stream);
|
ASSERT(stream);
|
||||||
|
ASSERT(size);
|
||||||
ssize_t nread = 0;
|
ssize_t nread = 0;
|
||||||
for (;;) {
|
while (nread < (size + 1)) {
|
||||||
if (nread >= size)
|
|
||||||
break;
|
|
||||||
int ch = fgetc(stream);
|
int ch = fgetc(stream);
|
||||||
if (ch == EOF) {
|
if (ch == EOF)
|
||||||
if (nread == 0)
|
|
||||||
return nullptr;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
buffer[nread++] = ch;
|
buffer[nread++] = ch;
|
||||||
if (!ch || ch == '\n')
|
if (ch == '\n')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (nread < size)
|
if (nread) {
|
||||||
buffer[nread] = '\0';
|
buffer[nread] = '\0';
|
||||||
return buffer;
|
return buffer;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fgetc(FILE* stream)
|
int fgetc(FILE* stream)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue