|
1 /* FCLOSE.C |
|
2 * |
|
3 * Portions Copyright (c) 1997-1999 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. |
|
5 */ |
|
6 |
|
7 /* |
|
8 FUNCTION |
|
9 <<fclose>>---close a file |
|
10 |
|
11 INDEX |
|
12 fclose |
|
13 |
|
14 ANSI_SYNOPSIS |
|
15 #include <stdio.h> |
|
16 int fclose(FILE *<[fp]>); |
|
17 |
|
18 TRAD_SYNOPSIS |
|
19 #include <stdio.h> |
|
20 int fclose(<[fp]>) |
|
21 FILE *<[fp]>; |
|
22 |
|
23 DESCRIPTION |
|
24 If the file or stream identified by <[fp]> is open, <<fclose>> closes |
|
25 it, after first ensuring that any pending data is written (by calling |
|
26 <<fflush(<[fp]>)>>). |
|
27 |
|
28 RETURNS |
|
29 <<fclose>> returns <<0>> if successful (including when <[fp]> is |
|
30 <<NULL>> or not an open file); otherwise, it returns <<EOF>>. |
|
31 |
|
32 PORTABILITY |
|
33 <<fclose>> is required by ANSI C. |
|
34 |
|
35 Required OS subroutines: <<close>>, <<fstat>>, <<isatty>>, <<lseek>>, |
|
36 <<read>>, <<sbrk>>, <<write>>. |
|
37 */ |
|
38 |
|
39 /* |
|
40 * Copyright (c) 1990 The Regents of the University of California. |
|
41 * All rights reserved. |
|
42 * |
|
43 * Redistribution and use in source and binary forms are permitted |
|
44 * provided that the above copyright notice and this paragraph are |
|
45 * duplicated in all such forms and that any documentation, |
|
46 * advertising materials, and other materials related to such |
|
47 * distribution and use acknowledge that the software was developed |
|
48 * by the University of California, Berkeley. The name of the |
|
49 * University may not be used to endorse or promote products derived |
|
50 * from this software without specific prior written permission. |
|
51 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
|
52 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
|
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
|
54 */ |
|
55 |
|
56 #include <stdio_r.h> |
|
57 #include <stdlib_r.h> |
|
58 #include "LOCAL.H" |
|
59 |
|
60 /* |
|
61 * Close a file. |
|
62 */ |
|
63 |
|
64 /** |
|
65 Close a stream. |
|
66 Close the file associated with the specified stream |
|
67 after flushing all buffers associated with it. |
|
68 @return If the stream is successfully closed 0 is returned. |
|
69 If any error EOF is returned. |
|
70 @param fp Pointer to FILE structure specifying the stream to be closed. |
|
71 */ |
|
72 EXPORT_C int fclose (FILE * fp) |
|
73 { |
|
74 int r; |
|
75 |
|
76 if (fp == NULL) |
|
77 return (0); /* on NULL */ |
|
78 |
|
79 CHECK_INIT (fp); |
|
80 |
|
81 if (fp->_flags == 0) /* not open! */ |
|
82 return (0); |
|
83 r = fp->_flags & __SWR ? fflush (fp) : 0; |
|
84 if (fp->_close != NULL && (*fp->_close) (fp->_cookie) < 0) |
|
85 r = EOF; |
|
86 if (fp->_flags & __SMBF) |
|
87 _free_r (fp->_data, (char *) fp->_bf._base); |
|
88 if (HASUB (fp)) |
|
89 FREEUB (fp); |
|
90 if (HASLB (fp)) |
|
91 FREELB (fp); |
|
92 fp->_flags = 0; /* release this FILE for reuse */ |
|
93 return (r); |
|
94 } |