From e16673c0c022a989a1f7922eb3b1aeceefa66ddf Mon Sep 17 00:00:00 2001 From: "mykola.tkachenko" Date: Fri, 31 Jan 2025 08:41:25 +0200 Subject: [PATCH] fix: skip flushing read-only streams in file_close() and return 0 on success --- chapter_8/exercise_8_03/syscalls.c | 5 +++-- chapter_8/exercise_8_04/syscalls.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/chapter_8/exercise_8_03/syscalls.c b/chapter_8/exercise_8_03/syscalls.c index d6321c8..f9a1f72 100644 --- a/chapter_8/exercise_8_03/syscalls.c +++ b/chapter_8/exercise_8_03/syscalls.c @@ -168,7 +168,7 @@ FILE *file_open(char *name, char *mode) int file_close(FILE *file_p) { - if (file_flush(file_p) == EOF) + if (file_p->flag._WRITE == 1 && file_flush(file_p) == EOF) { return EOF; } @@ -179,7 +179,7 @@ int file_close(FILE *file_p) file_p->counter = 0; close(file_p->file_descriptor); - return NULL; + return 0; } int main(void) @@ -204,6 +204,7 @@ int main(void) { putc(c, file_out_p); } + file_close(file_in_p); file_close(file_out_p); return EXIT_SUCCESS; diff --git a/chapter_8/exercise_8_04/syscalls.c b/chapter_8/exercise_8_04/syscalls.c index 7cc8f83..cad0a98 100644 --- a/chapter_8/exercise_8_04/syscalls.c +++ b/chapter_8/exercise_8_04/syscalls.c @@ -168,7 +168,7 @@ FILE *file_open(char *name, char *mode) int file_close(FILE *file_p) { - if (file_flush(file_p) == EOF) + if (file_p->flag._WRITE == 1 && file_flush(file_p) == EOF) { return EOF; } @@ -179,7 +179,7 @@ int file_close(FILE *file_p) file_p->counter = 0; close(file_p->file_descriptor); - return NULL; + return 0; } int file_seek(FILE *file_p, long offset, int whence) @@ -227,6 +227,7 @@ int main(void) { putc(c, file_out_p); } + file_close(file_in_p); file_close(file_out_p); return EXIT_SUCCESS;