branch update for HEAD-2003021201
[reactos.git] / lib / crtdll / 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 int
15 fclose(FILE *f)
16 {
17   int r = 0;
18
19   if (f == NULL) {
20     __set_errno (EINVAL);
21     return EOF;
22   }
23
24
25
26 // flush only if stream was opened for writing
27   if ( !(f->_flag&_IOSTRG) ) {
28         if ( OPEN4WRITING(f) )
29                 r = fflush(f);
30         
31         if (_close(fileno(f)) < 0)
32                 r = EOF;
33         if (f->_flag&_IOMYBUF)
34                 free(f->_base);
35   
36 // Kernel might do this later
37         if (f->_flag & _IORMONCL && f->_name_to_remove)
38         {
39                 remove(f->_name_to_remove);
40                 free(f->_name_to_remove);
41                 f->_name_to_remove = 0;
42         }
43   }
44   f->_cnt = 0;
45   f->_base = 0;
46   f->_ptr = 0;
47   f->_bufsiz = 0;
48   f->_flag = 0;
49   f->_file = -1;
50   return r;
51 }