2310863495@qq.com 1 anno fa
parent
commit
ef9e475f71

+ 1 - 1
include/stdio.h

@@ -68,7 +68,7 @@ extern FILE* stderr;
 /* File open and close */
 int fclose(FILE *);
 FILE *fdopen(int, const char *);
-int fopen(const char *, const char *);
+FILE *fopen(const char *, const char *);
 FILE *freopen(const char *, const char *, FILE *);
 
 /* Formatted I/O status */

+ 4 - 1
src/internal/stdio_impl.h

@@ -29,6 +29,9 @@ size_t __mlibc_towrite(FILE *);
 
 void __mlibc_stdio_exit(void);
 
-FILE *mlibc_file_add(FILE *f);
+FILE *__mlibc_file_add(FILE *f);
+void __mlibc_file_remove(FILE *f);
+FILE *__mlibc_file_head(void);
+int __mlibc_fmode_to_flags(const char *mode);
 
 #endif /* MLIBC_STDIO_IMPL_H__ */

+ 13 - 1
src/stdio/__mlibc_file_mgt.c

@@ -10,10 +10,22 @@
 #include "../internal/stdio_impl.h"
 
 FILE **head;
-FILE *mlibc_file_add(FILE *f)
+FILE *__mlibc_file_add(FILE *f)
 {
     f->next = *head;
     if(*head) (*head)->prev = f;
     *head = f;
     return f;
+}
+
+void __mlibc_file_remove(FILE *f)
+{
+    if (f->prev) f->prev->next = f->next;
+	if (f->next) f->next->prev = f->prev;
+	if (*head == f) *head = f->next;
+}
+
+FILE *__mlibc_file_head(void)
+{
+    return *head;
 }

+ 23 - 0
src/stdio/__mlibc_fmode.c

@@ -0,0 +1,23 @@
+#include "../internal/stdio_impl.h"
+#include <fcntl.h>
+#include <string.h>
+
+int __mlibc_fmode_to_flags(const char *mode)
+{
+    int flags = 0;
+
+	if (strchr(mode, '+'))
+        flags = O_RDWR;
+	else if (*mode == 'r')
+        flags = O_RDONLY;
+	else
+        flags = O_WRONLY;
+
+	if (*mode != 'r') flags |= O_CREAT;
+
+	if (*mode == 'w') flags |= O_TRUNC;
+
+	if (*mode == 'a') flags |= O_APPEND;
+
+	return flags;
+}

+ 4 - 4
src/stdio/__mlibc_write.c

@@ -20,8 +20,8 @@ static ssize_t __mlibc_writev(int fd, iovec_t *iov, size_t iov_size)
 
     for(; i < iov_size; i++)
     {
-        ret = __mlibc_sys_write(fd, (iov + i)->buf, (iov + i)->buf_size);
-        if(ret <= 0)
+        ret = __mlibc_sys_write(fd, iov[i].buf, iov[i].buf_size);
+        if(ret < 0)
         {
             break;
         }
@@ -42,7 +42,7 @@ size_t __mlibc_write(FILE *f, unsigned char *buf, size_t buf_size)
         { .buf = f->wbase, .buf_size = f->wpos - f->wbase },
         { .buf = buf, .buf_size = buf_size}
     };
-    iovec_t *iov_p;
+    iovec_t *iov_p = iov;
     size_t total = iov_p[0].buf_size + iov_p[1].buf_size;
     ssize_t ret = 0;
     int iov_cnt = 2;
@@ -57,7 +57,7 @@ size_t __mlibc_write(FILE *f, unsigned char *buf, size_t buf_size)
             f->wpos = f->wbase = f->buf;
             return buf_size;
         }
-        if(ret < 0) // write failed
+        if(ret <= 0) // write failed
         {
             f->wpos = f->wbase = f->wend = 0;
             f->flags |= F_ERR;

+ 26 - 0
src/stdio/fclose.c

@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) mlibc & plct lab
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2024/5/24   0Bitbiscuits  the first version
+ */
+#include <stdlib.h>
+#include "../internal/stdio_impl.h"
+
+int fclose(FILE *f)
+{
+    int res = 0;
+    
+    res = fflush(f);
+    res |= f->close(f);
+
+    __mlibc_file_remove(f);
+
+    free(f->getln_buf);
+    free(f);
+
+    return 0;
+}

+ 19 - 3
src/stdio/fdopen.c

@@ -14,6 +14,21 @@
 #include <stdlib.h>
 #include <compiler.h>
 
+/**
+ * @brief This function is not responsible for creating files,
+ * it primarily handles associating the fd with the FILE structure.
+ * 
+ * @param fd The file descriptor
+ * @param mode The file open mode.
+ * support:
+ * r: you can readonly
+ * w: clean the file and writeonly
+ * a: append the file
+ * r+: you can read and write file
+ * w+: file will clean first and you can read and write file
+ * a+: append the file
+ * @return FILE* A pointer to the FILE structure associated with the file descriptor.
+ */
 mlibc_weak FILE *fdopen(int fd, const char *mode)
 {
     FILE *f;
@@ -22,13 +37,13 @@ mlibc_weak FILE *fdopen(int fd, const char *mode)
 	if(!strchr("rwa", *mode)) 
     {
         /* error operation */
-		return 0;
+		return NULL;
 	}
 
 	/* allocate memory for file and buffer */
 	if(!(f=malloc(sizeof(FILE) + UNGET + BUFSIZ)))
     {
-        return 0;
+        return NULL;
     }
 
 	/* zero-fill FILE */
@@ -42,6 +57,7 @@ mlibc_weak FILE *fdopen(int fd, const char *mode)
 	/* Set append mode on fd if opened for append */
 	if (*mode == 'a') {
 		int flags = __mlibc_sys_fcntl(fd, F_GETFL);
+
 		if (!(flags & O_APPEND))
         { 
             __mlibc_sys_fcntl(fd, F_SETFL, flags | O_APPEND);
@@ -63,5 +79,5 @@ mlibc_weak FILE *fdopen(int fd, const char *mode)
 	f->seek = __mlibc_lseek;
 	f->close = __mlibc_close;
 
-	return mlibc_file_add(f);
+	return __mlibc_file_add(f);
 }

+ 56 - 0
src/stdio/fflush.c

@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) mlibc & plct lab
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2024/5/24  0Bitbiscuits  the first version
+ */
+#include "../internal/stdio_impl.h"
+#include <fcntl.h>  
+
+int fflush(FILE *f)
+{
+    int r = 0;
+
+    /* If f is null, flush all file buffer */
+    if(!f)
+    {
+        if(stdout)
+            r |= fflush(stdout);
+        if(stderr)
+            r |= fflush(stderr);
+
+        for(f = __mlibc_file_head(); f; f = f->next)
+        {
+            /* use write buffer */
+            if(f->wpos != f->wbase)
+            {
+                r |= fflush(f);
+            }
+        }
+
+        return r;
+    }
+
+    /* f is not null */
+    /* If writing, flush output */
+    if(f->wpos != f->wbase)
+    {
+        f->write(f, 0, 0);
+        if(!f->wpos)
+        {
+            return EOF;
+        }
+    }
+
+    /* If reading, sync position to the end of file */
+	if (f->rpos != f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
+
+	/* Clear read and write modes */
+	f->wpos = f->wbase = f->wend = 0;
+	f->rpos = f->rend = 0;
+
+    return 0;
+}

+ 54 - 0
src/stdio/fopen.c

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) mlibc & plct lab
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2024/5/24  0Bitbiscuits  the first version
+ */
+#include "../internal/stdio_impl.h"
+#include <sys/sys_fio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <compiler.h>
+
+/**
+ * @brief 
+ * 
+ * @param path The file path
+ * @param mode The file open mode.
+ * support:
+ * r: you can readonly
+ * w: clean the file and writeonly
+ * a: append the file
+ * r+: you can read and write file
+ * w+: file will clean first and you can read and write file
+ * a+: append the file
+ * @return FILE* A pointer to the FILE structure associated with the file descriptor.
+ */
+FILE *fopen(const char *path, const char *mode)
+{
+    FILE *f;
+	int fd;
+	int flags = 0;
+
+	/* Check for valid initial mode character */
+	if (!strchr("rwa", *mode)) {
+		/* error operation */
+		return 0;
+	}
+
+	/* Compute the flags to pass to open() */
+	flags = __mlibc_fmode_to_flags(mode);
+
+	fd = __mlibc_sys_open(path, flags);
+	if (fd < 0) return 0;
+
+	f = fdopen(fd, mode);
+	if (f) return f;
+
+	__mlibc_sys_close(fd);
+
+	return NULL;
+}