OSDN Git Service

2007-03-15 Eric Blake <ebb9@byu.net>
[pf3gnuchains/pf3gnuchains3x.git] / newlib / libc / stdio / fclose.c
1 /*
2  * Copyright (c) 1990, 2007 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17
18 /*
19 FUNCTION
20 <<fclose>>---close a file
21
22 INDEX
23         fclose
24 INDEX
25         _fclose_r
26
27 ANSI_SYNOPSIS
28         #include <stdio.h>
29         int fclose(FILE *<[fp]>);
30         int _fclose_r(struct _reent *<[reent]>, FILE *<[fp]>);
31
32 TRAD_SYNOPSIS
33         #include <stdio.h>
34         int fclose(<[fp]>)
35         FILE *<[fp]>;
36         
37         int fclose(<[fp]>)
38         struct _reent *<[reent]>
39         FILE *<[fp]>;
40
41 DESCRIPTION
42 If the file or stream identified by <[fp]> is open, <<fclose>> closes
43 it, after first ensuring that any pending data is written (by calling
44 <<fflush(<[fp]>)>>).
45
46 The alternate function <<_fclose_r>> is a reentrant version.
47 The extra argument <[reent]> is a pointer to a reentrancy structure.
48
49 RETURNS
50 <<fclose>> returns <<0>> if successful (including when <[fp]> is
51 <<NULL>> or not an open file); otherwise, it returns <<EOF>>.
52
53 PORTABILITY
54 <<fclose>> is required by ANSI C.
55
56 Required OS subroutines: <<close>>, <<fstat>>, <<isatty>>, <<lseek>>,
57 <<read>>, <<sbrk>>, <<write>>.
58 */
59
60 #include <_ansi.h>
61 #include <reent.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <sys/lock.h>
65 #include "local.h"
66
67 int
68 _DEFUN(_fclose_r, (rptr, fp),
69       struct _reent *rptr _AND
70       register FILE * fp)
71 {
72   int r;
73
74   if (fp == NULL)
75     return (0);                 /* on NULL */
76
77   __sfp_lock_acquire ();
78
79   CHECK_INIT (rptr, fp);
80
81   _flockfile (fp);
82   
83   if (fp->_flags == 0)          /* not open! */
84     {
85       _funlockfile (fp);
86       __sfp_lock_release ();
87       return (0);
88     }
89   /* Unconditionally flush to allow special handling for seekable read
90      files to reposition file to last byte processed as opposed to
91      last byte read ahead into the buffer.  */
92   r = fflush (fp);
93   if (fp->_close != NULL && (*fp->_close) (fp->_cookie) < 0)
94     r = EOF;
95   if (fp->_flags & __SMBF)
96     _free_r (rptr, (char *) fp->_bf._base);
97   if (HASUB (fp))
98     FREEUB (rptr, fp);
99   if (HASLB (fp))
100     FREELB (rptr, fp);
101   fp->_flags = 0;               /* release this FILE for reuse */
102   _funlockfile (fp);
103 #ifndef __SINGLE_THREAD__
104   __lock_close_recursive (fp->_lock);
105 #endif
106
107   __sfp_lock_release ();
108
109   return (r);
110 }
111
112 #ifndef _REENT_ONLY
113
114 int
115 _DEFUN(fclose, (fp),
116        register FILE * fp)
117 {
118   return _fclose_r(_REENT, fp);
119 }
120
121 #endif