Skip to content

fix: skip flushing read-only streams in file_close() and return 0 on success #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions chapter_8/exercise_8_03/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions chapter_8/exercise_8_04/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down