- Wrap change of entries in an InitChange/ExitChange pair (without this,
[AROS.git] / compiler / stdc / fclose.c
blob8064b4df9cd1656f4481dd10613253393e96842a
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function fclose().
6 */
8 #include <proto/exec.h>
9 #include <proto/dos.h>
10 #include <aros/libcall.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <string.h>
16 #include "__stdcio_intbase.h"
18 #define DEBUG 0
19 #include <aros/debug.h>
21 /*****************************************************************************
23 NAME */
24 #include <stdio.h>
26 int fclose (
28 /* SYNOPSIS */
29 FILE * stream)
31 /* FUNCTION
32 Closes a stream.
34 INPUTS
35 stream - Stream to close.
37 RESULT
38 Upon successful completion 0 is returned. Otherwise, EOF is
39 returned and the global variable errno is set to indicate the
40 error. In either case no further access to the stream is possible.
42 NOTES
44 EXAMPLE
46 BUGS
48 SEE ALSO
49 fopen()
51 INTERNALS
53 ******************************************************************************/
55 struct StdCIOIntBase *StdCIOBase =
56 (struct StdCIOIntBase *)__aros_getbase_StdCIOBase();
58 int ret = 0;
59 char s[L_tmpnam+20] = "";
61 if (stream->flags & __STDCIO_STDIO_TMP)
63 if (!NameFromFH(stream->fh, s, L_tmpnam+20))
65 /* Just leave the file in T: */
66 D(bug("[fclose]: Could not get name from fh, IoErr()=%d\n", IoErr()));
67 s[0] = 0;
71 if (!(stream->flags & __STDCIO_STDIO_DONTCLOSE))
73 if (Close(stream->fh))
74 ret = 0;
75 else
77 ret = EOF;
78 errno = __stdc_ioerr2errno(IoErr());
82 Remove((struct Node *)stream);
83 if (!(stream->flags & __STDCIO_STDIO_DONTFREE))
84 FreePooled(StdCIOBase->streampool, stream, sizeof(FILE));
86 if (strlen(s) > 0)
88 D(bug("[fclose]: Deleting file '%s'\n", s));
89 DeleteFile(s); /* File will be left there if delete fails */
92 return ret;
93 } /* fclose */