From 6e40a34fef4b4b140442cc34408b2442043eba21 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 11 Feb 2023 18:08:55 +0100 Subject: chagne system structure and fix env --- lib/io/io.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/io/io.h | 1 + 2 files changed, 49 insertions(+) (limited to 'lib/io') diff --git a/lib/io/io.c b/lib/io/io.c index 2aefe1a..d8e8d18 100644 --- a/lib/io/io.c +++ b/lib/io/io.c @@ -2,6 +2,7 @@ #include "../cstr/cstr.h" #include "../aec/aec.h" +#include "../sys/types.h" void __wstdn(const char *buf, u64 n) { @@ -92,6 +93,53 @@ void wff__(unsigned int fd, const char *buf, const void **args) write(fd, start, buf - start); } + +char __buf[BUFSIZ] = { 0 }; + +int get_next_line_from_fd(unsigned int fd, char *linebuf) +{ + u64 size = 0; + char *line = __buf; + + while (*line && *line != '\n') line++; + + if (!*line) { + size = cstr_length(__buf); + i64 s = read(fd, __buf + size, BUFSIZ - size - 1); + + if (s == 0 && size == 0) { + return -1; + } else if (s == 0) { + for (int i = 0; i < size; ++i) linebuf[i] = __buf[i]; + __buf[0] = 0; + return size; + } + + __buf[s] = 0; + while (*line && *line != '\n') line++; + + if (!*line) { + return -2; + } + } + + *line = 0; + ++line; + + size = cstr_length(__buf); + for (int i = 0; __buf[i]; ++i) linebuf[i] = __buf[i]; + linebuf[size] = 0; + + int i = 0; + for (; *line; ++i) { + __buf[i] = *line; + ++line; + } + __buf[i] = 0; + + return size; +} + #ifdef IO_LIB_UNIT_TEST int main() { wstdf("%s %S%F%s%S\n", "hallo", SGR_UNDERLINE, COLOR_RED, "welt", SGR_RESET); diff --git a/lib/io/io.h b/lib/io/io.h index 6bf0a3b..887d911 100644 --- a/lib/io/io.h +++ b/lib/io/io.h @@ -7,6 +7,7 @@ void wstd(const char *buf); unsigned long rstd(char *buf, unsigned long count); void wf(unsigned int fd, char *buf); void putchar(const char c); +int get_next_line_from_fd(unsigned int fd, char *buf); void wstdf__(const char *buf, const void **args); void wff__(unsigned int fd, const char *buf, const void **args); -- cgit v1.2.3-70-g09d2