libc: io: implement puts
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#include "file.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
extern int __fputc(int c, struct __opaque_file *stream);
|
||||
|
||||
int puts(const char *restrict str)
|
||||
{
|
||||
struct __opaque_file *stream = stdout;
|
||||
__libc_file_lock(stream);
|
||||
if (stream->f_flags & (FILE_EOF | FILE_ERR)) {
|
||||
__libc_file_unlock(stream);
|
||||
return EOF;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
int err = 0;
|
||||
for (i = 0; str[i]; i++) {
|
||||
err = __fputc(str[i], stream);
|
||||
if (err < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (err >= 0) {
|
||||
err = __fputc('\n', stream);
|
||||
}
|
||||
|
||||
__libc_file_unlock(stream);
|
||||
|
||||
if (i > 0) {
|
||||
return i;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
Reference in New Issue
Block a user