Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions posix/stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,6 @@ long ulimit(int __cmd, ...)
return 0;
}

char *gets(char *str)
{
return NULL;
}

char *tmpnam(char *str)
{
return NULL;
Expand Down
58 changes: 49 additions & 9 deletions stdio/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,21 +734,36 @@ int fputc(int c, FILE *stream)

char *fgets_unlocked(char *str, int n, FILE *stream)
{
if (n < 1) {
return NULL;
}

if (n == 1) {
str[0] = '\0';
return str;
}

int c, i = 0;
while ((c = fgetc_unlocked(stream)) != EOF) {
str[i++] = c;
if (c == '\n' || i == n - 1) {

while (i < n - 1) {
c = getc_unlocked(stream);
if (c == EOF) {
if (i == 0) {
return NULL;
}
break;
}
}

if (i) {
str[i] = 0;
}
else {
return NULL;
str[i] = c;
i++;

if (c == '\n') {
break;
}
}

str[i] = '\0';

return str;
}

Expand Down Expand Up @@ -938,6 +953,31 @@ int getc_unlocked(FILE *stream)
}


char *gets(char *str)
{
int c;
char *ret;
mutexLock(stdin->lock);
c = getc_unlocked(stdin);
if (c == EOF) {
ret = NULL;
}
else {
ret = str;
do {
if (c == '\n') {
break;
}
*str = c;
str++;
} while ((c = getc_unlocked(stdin)) != EOF);
*str = '\0';
}
mutexUnlock(stdin->lock);
return ret;
}


int getc(FILE *stream)
{
int ret;
Expand Down
Loading