Implemented fputs

This commit is contained in:
Max Wash
2020-04-01 11:58:54 +01:00
parent 08734094d7
commit 703d9a178f
5 changed files with 38 additions and 2 deletions

View File

@@ -13,6 +13,8 @@
#define SEEK_CUR __SEEK_CUR
#define SEEK_END __SEEK_END
#define EOF -1
#if defined(__cplusplus)
extern "C" {
#endif
@@ -24,6 +26,7 @@ extern FILE *stdout;
extern FILE *stderr;
extern int fileno(FILE *fp);
extern int fputs(const char *str, FILE *fp);
#if defined(__cplusplus)
}

View File

@@ -0,0 +1,9 @@
#include <stdio.h>
#include <__fio.h>
int fputs(const char *str, FILE *fp)
{
while (*str) {
__fio_write(fp, str++, 1);
}
}

View File

@@ -14,6 +14,8 @@ struct __io_file {
};
extern int __fileno(struct __io_file *f);
extern unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz);
extern unsigned int __fio_flush(struct __io_file *f);
#if defined(__cplusplus)
}

View File

@@ -1,5 +1,6 @@
#include "__fio.h"
#include "__syscall.h"
#include "unistd.h"
struct __io_file __stdin = {};
struct __io_file __stdout = {};
@@ -20,3 +21,25 @@ int __fileno(struct __io_file *f)
{
return f->fd;
}
unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz)
{
for (unsigned int i = 0; i < sz; i++) {
if (f->buf_idx >= __FIO_BUFSZ) {
__fio_flush(f);
}
f->buf[f->buf_idx++] = buf[i];
if (buf[i] == '\n') {
__fio_flush(f);
}
}
return sz;
}
unsigned int __fio_flush(struct __io_file *f)
{
write(f->fd, f->buf, f->buf_idx);
f->buf_idx = 0;
}

View File

@@ -3,7 +3,6 @@
int main(int argc, char **argv)
{
const char *msg = "Hello!\n";
write(fileno(stdout), msg, 7);
fputs("Hello, world!\n", stdout);
return 0;
}