From e9fdc57bf878f1bc5cc5743dfaeeaef743439291 Mon Sep 17 00:00:00 2001 From: login Date: Sat, 7 Jan 2023 22:36:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=B7=BB=E5=8A=A0=E4=BA=86fo?= =?UTF-8?q?pen=E5=AF=B9mode=E5=8F=82=E6=95=B0=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E3=80=82=E8=AF=B7=E6=B3=A8=E6=84=8F=EF=BC=8C=E5=AE=83=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=AE=8C=E5=85=A8=E9=81=B5=E5=BE=AAposix=EF=BC=8C?= =?UTF-8?q?=E4=B9=9F=E4=B8=8ELinux=E7=9A=84=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E6=9D=A5=E4=BD=BF=E7=94=A8Rust=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E5=AE=8C=E5=96=84=E5=AE=83=E3=80=82=20(#141)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/libs/libc/src/stdio.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/user/libs/libc/src/stdio.c b/user/libs/libc/src/stdio.c index 37a2c834..bf937bfd 100644 --- a/user/libs/libc/src/stdio.c +++ b/user/libs/libc/src/stdio.c @@ -49,20 +49,35 @@ int fclose(FILE *stream) { if (stream->fd >= 3) { - int retcval = close(stream); + int retcval = close(stream->fd); free(stream); - return; + return 0; } else return 0; } +// FIXME: 请注意,这个函数的实现,没有遵照posix,行为也与Linux的不一致,请在将来用Rust重构时改变它,以使得它的行为与Linux的一致。 FILE *fopen(const char *restrict pathname, const char *restrict mode) { FILE *stream = malloc(sizeof(FILE)); memset(stream, 0, sizeof(FILE)); + int o_flags = 0; - int fd = open(pathname, mode); + if (strcmp(mode, "r") == 0) + o_flags = O_RDONLY; + else if (strcmp(mode, "r+") == 0) + o_flags = O_RDWR; + else if (strcmp(mode, "w") == 0) + o_flags = O_WRONLY; + else if (strcmp(mode, "w+") == 0) + o_flags = O_RDWR | O_CREAT; + else if (strcmp(mode, "a") == 0) + o_flags = O_APPEND | O_CREAT; + else if (strcmp(mode, "a+") == 0) + o_flags = O_APPEND | O_CREAT; + + int fd = open(pathname, o_flags); if (fd >= 0) stream->fd = fd; return stream;