aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-11 18:08:55 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-11 18:08:55 +0100
commit6e40a34fef4b4b140442cc34408b2442043eba21 (patch)
treeacae1f3828a067e66683bb349054d9c7e7cf8842 /lib
parent05cf1511dfcde10717894bab2004150ad43cca3f (diff)
chagne system structure and fix env
Diffstat (limited to 'lib')
-rw-r--r--lib/io/io.c48
-rw-r--r--lib/io/io.h1
-rw-r--r--lib/sys/types.h2
3 files changed, 51 insertions, 0 deletions
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);
diff --git a/lib/sys/types.h b/lib/sys/types.h
index 0aafec5..de8ce80 100644
--- a/lib/sys/types.h
+++ b/lib/sys/types.h
@@ -3,6 +3,8 @@
#include "sizes.h"
+#define BUFSIZ 16384
+
typedef u64 dev_t;
typedef u32 mode_t;