update for HEAD-2003091401
[reactos.git] / lib / msvcrt / stdio / fclose.c
1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
2
3 #include <msvcrt/stdio.h>
4 #include <msvcrt/io.h>
5 #include <msvcrt/sys/types.h>
6 #include <msvcrt/sys/stat.h>
7 #include <msvcrt/stdlib.h>
8 #include <msvcrt/errno.h>
9 #include <msvcrt/internal/file.h>
10
11 // changed check for writable stream
12
13
14 /*
15  * @implemented
16  */
17 int
18 fclose(FILE *f)
19 {
20   int r = 0;
21
22   if (f == NULL) {
23     __set_errno (EINVAL);
24     return EOF;
25   }
26
27
28
29 // flush only if stream was opened for writing
30   if ( !(f->_flag&_IOSTRG) ) {
31         if ( OPEN4WRITING(f) )
32                 r = fflush(f);
33         
34         if (_close(fileno(f)) < 0)
35                 r = EOF;
36         if (f->_flag&_IOMYBUF)
37                 free(f->_base);
38   
39 // Kernel might do this later
40         if (f->_flag & _IORMONCL && f->_name_to_remove)
41         {
42                 remove(f->_name_to_remove);
43                 free(f->_name_to_remove);
44                 f->_name_to_remove = 0;
45         }
46   }
47   f->_cnt = 0;
48   f->_base = 0;
49   f->_ptr = 0;
50   f->_bufsiz = 0;
51   f->_flag = 0;
52   f->_file = -1;
53   return r;
54 }