diff --git a/lib/libc/io/stdio/puts.c b/lib/libc/io/stdio/puts.c new file mode 100644 index 0000000..e80c70b --- /dev/null +++ b/lib/libc/io/stdio/puts.c @@ -0,0 +1,36 @@ +#include "file.h" + +#include + +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; +}