Skip to content

Commit

Permalink
puts: Return EOF on error
Browse files Browse the repository at this point in the history
DONE: RTOS-503
  • Loading branch information
agkaminski committed Jul 25, 2023
1 parent 1197d36 commit feb19e6
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions stdio/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,45 +857,53 @@ int ungetc(int c, FILE *stream)
}


int puts_unlocked(const char *s)
int fputs_unlocked(const char *s, FILE *stream)
{
int len = strlen(s), l = 0, err;
size_t len = strlen(s);
size_t wrote = 0, ret;

while (l < len) {
if ((err = fwrite_unlocked((void *)s + l, 1, len - l, stdout)) < 0) {
return -1;
while (wrote < len) {
ret = fwrite_unlocked(s + wrote, 1, len - wrote, stream);
if (ret == 0) {
return EOF;
}
l += err;

wrote += ret;
}
putchar_unlocked('\n');

return l;
/* Truncation is ok, we could return zero instead */
return (wrote < INT_MAX) ? (int)wrote : INT_MAX;
}


int puts(const char *s)
int fputs(const char *s, FILE *stream)
{
int ret;
mutexLock(stdout->lock);
ret = puts_unlocked(s);
mutexUnlock(stdout->lock);
mutexLock(stream->lock);
ret = fputs_unlocked(s, stream);
mutexUnlock(stream->lock);
return ret;
}


int fputs_unlocked(const char *s, FILE *stream)
int puts_unlocked(const char *s)
{
int len = strlen(s);
return fwrite_unlocked(s, 1, len, stream);
int wrote = fputs_unlocked(s, stdout);

if (wrote != EOF) {
putchar_unlocked('\n');
}

return wrote;
}


int fputs(const char *s, FILE *stream)
int puts(const char *s)
{
int ret;
mutexLock(stream->lock);
ret = fputs_unlocked(s, stream);
mutexUnlock(stream->lock);
mutexLock(stdout->lock);
ret = puts_unlocked(s);
mutexUnlock(stdout->lock);
return ret;
}

Expand Down

0 comments on commit feb19e6

Please sign in to comment.