fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / stdio / fclose.c
blob1caeb4cdea5a9607863799386f1e6ec4169fd874
1 /*
2 FUNCTION
3 <<fclose>>---close a file
5 INDEX
6 fclose
8 ANSI_SYNOPSIS
9 #include <stdio.h>
10 int fclose(FILE *<[fp]>);
12 TRAD_SYNOPSIS
13 #include <stdio.h>
14 int fclose(<[fp]>)
15 FILE *<[fp]>;
17 DESCRIPTION
18 If the file or stream identified by <[fp]> is open, <<fclose>> closes
19 it, after first ensuring that any pending data is written (by calling
20 <<fflush(<[fp]>)>>).
22 RETURNS
23 <<fclose>> returns <<0>> if successful (including when <[fp]> is
24 <<NULL>> or not an open file); otherwise, it returns <<EOF>>.
26 PORTABILITY
27 <<fclose>> is required by ANSI C.
29 Required OS subroutines: <<close>>, <<fstat>>, <<isatty>>, <<lseek>>,
30 <<read>>, <<sbrk>>, <<write>>.
34 * Copyright (c) 1990 The Regents of the University of California.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms are permitted
38 * provided that the above copyright notice and this paragraph are
39 * duplicated in all such forms and that any documentation,
40 * advertising materials, and other materials related to such
41 * distribution and use acknowledge that the software was developed
42 * by the University of California, Berkeley. The name of the
43 * University may not be used to endorse or promote products derived
44 * from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
47 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include "local.h"
53 #include <sys/lock.h>
56 * Close a file.
59 int
60 _DEFUN (fclose, (fp),
61 register FILE * fp)
63 int r;
65 if (fp == NULL)
66 return (0); /* on NULL */
68 _flockfile(fp);
70 CHECK_INIT (fp);
72 if (fp->_flags == 0) /* not open! */
74 _funlockfile(fp);
75 return (0);
77 r = fp->_flags & __SWR ? fflush (fp) : 0;
78 if (fp->_close != NULL && (*fp->_close) (fp->_cookie) < 0)
79 r = EOF;
80 if (fp->_flags & __SMBF)
81 _free_r (_REENT, (char *) fp->_bf._base);
82 if (HASUB (fp))
83 FREEUB (fp);
84 if (HASLB (fp))
85 FREELB (fp);
86 _funlockfile(fp);
87 #ifndef __SINGLE_THREAD__
88 __lock_close_recursive (*(_LOCK_RECURSIVE_T *)&fp->_lock);
89 #endif
90 fp->_flags = 0; /* release this FILE for reuse */
92 return (r);