tools/toollib: remove from main Makefile.in
[AROS.git] / compiler / posixc / close.c
blobff4b18ce65c772d6190b454f8069441cb7e5c8b9
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 function close().
6 */
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
12 #include <errno.h>
13 #include "__fdesc.h"
15 /*****************************************************************************
17 NAME */
18 #include <unistd.h>
20 int close (
22 /* SYNOPSIS */
23 int fd)
25 /* FUNCTION
26 Closes an open file. If this is the last file descriptor
27 associated with this file, then all allocated resources
28 are freed, too.
30 INPUTS
31 fd - The result of a successful open()
33 RESULT
34 -1 for error or zero on success.
36 NOTES
37 This function must not be used in a shared library or
38 in a threaded application.
40 EXAMPLE
42 BUGS
44 SEE ALSO
45 open(), read(), write(), fopen()
47 INTERNALS
49 ******************************************************************************/
51 fdesc *fdesc;
53 if (!(fdesc = __getfdesc(fd)))
55 errno = EBADF;
57 return -1;
60 if (--fdesc->fcb->opencount == 0)
62 /* Due to a *stupid* behaviour of the dos.library we cannot handle closing failures cleanly :-(
63 if (
64 !(fdesc->fcb->privflags & _FCB_DONTCLOSE_FH) &&
65 !Close(fdesc->fh)
68 fdesc->opencount++;
69 errno = __stdc_ioerr2errno(IoErr());
71 return -1;
74 /* FIXME: Damn dos.library! We cannot report the error code correctly! This oughta change someday... */
75 /* Since the dos.library destroys the file handle anyway, even if the closing fails, we cannot
76 report the error code correctly, so just close the file and get out of here */
78 if (!(fdesc->fcb->privflags & _FCB_DONTCLOSE_FH))
80 // don't close directories because we don't Open() them.
81 if (fdesc->fcb->privflags & _FCB_ISDIR)
83 UnLock(fdesc->fcb->handle);
85 else
87 Close(fdesc->fcb->handle);
91 FreeVec(fdesc->fcb);
94 __free_fdesc(fdesc);
95 __setfdesc(fd, NULL);
97 return 0;
98 } /* close */