Skip to content

Commit 7d7037d

Browse files
authored
Merge pull request #77 from MikloshXXX/fix-flushing
fix: skip flushing read-only streams in file_close() and return 0 on success
2 parents b6cc99f + e16673c commit 7d7037d

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

chapter_8/exercise_8_03/syscalls.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ FILE *file_open(char *name, char *mode)
168168

169169
int file_close(FILE *file_p)
170170
{
171-
if (file_flush(file_p) == EOF)
171+
if (file_p->flag._WRITE == 1 && file_flush(file_p) == EOF)
172172
{
173173
return EOF;
174174
}
@@ -179,7 +179,7 @@ int file_close(FILE *file_p)
179179
file_p->counter = 0;
180180
close(file_p->file_descriptor);
181181

182-
return NULL;
182+
return 0;
183183
}
184184

185185
int main(void)
@@ -204,6 +204,7 @@ int main(void)
204204
{
205205
putc(c, file_out_p);
206206
}
207+
file_close(file_in_p);
207208
file_close(file_out_p);
208209

209210
return EXIT_SUCCESS;

chapter_8/exercise_8_04/syscalls.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ FILE *file_open(char *name, char *mode)
168168

169169
int file_close(FILE *file_p)
170170
{
171-
if (file_flush(file_p) == EOF)
171+
if (file_p->flag._WRITE == 1 && file_flush(file_p) == EOF)
172172
{
173173
return EOF;
174174
}
@@ -179,7 +179,7 @@ int file_close(FILE *file_p)
179179
file_p->counter = 0;
180180
close(file_p->file_descriptor);
181181

182-
return NULL;
182+
return 0;
183183
}
184184

185185
int file_seek(FILE *file_p, long offset, int whence)
@@ -227,6 +227,7 @@ int main(void)
227227
{
228228
putc(c, file_out_p);
229229
}
230+
file_close(file_in_p);
230231
file_close(file_out_p);
231232

232233
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)