Don't call ReadArgs() if started from WB.
[tangerine.git] / compiler / clib / close.c
blob2eeb419482c5b9116daf358476994f3f0d4ede71
1 /*
2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function close().
6 */
8 #include "__arosc_privdata.h"
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <proto/exec.h>
13 #include <proto/dos.h>
14 #include <errno.h>
15 #include "__open.h"
16 #include "__errno.h"
18 /*****************************************************************************
20 NAME */
21 #include <unistd.h>
23 int close (
25 /* SYNOPSIS */
26 int fd)
28 /* FUNCTION
29 Closes an open file. If this is the last file descriptor
30 associated with this file, then all allocated resources
31 are freed, too.
33 INPUTS
34 fd - The result of a successful open()
36 RESULT
37 -1 for error or zero on success.
39 NOTES
40 This function must not be used in a shared library or
41 in a threaded application.
43 EXAMPLE
45 BUGS
47 SEE ALSO
48 open(), read(), write(), fopen()
50 INTERNALS
52 ******************************************************************************/
54 fdesc *fdesc;
55 AROS_GET_SYSBASE_OK
56 AROS_GET_DOSBASE
58 if (!(fdesc = __getfdesc(fd)))
60 errno = EBADF;
62 return -1;
65 if (--fdesc->opencount == 0)
67 /* Due to a *stupid* behaviour of the dos.library we cannot handle closing failures cleanly :-(
68 if (
69 fdesc->fh!=__stdfiles[STDIN_FILENO] &&
70 fdesc->fh!=__stdfiles[STDOUT_FILENO] &&
71 fdesc->fh!=__stdfiles[STDERR_FILENO] &&
72 !Close(fdesc->fh)
75 fdesc->opencount++;
76 errno = IoErr2errno(IoErr());
78 return -1;
81 #warning Damn dos.library! We cannot report the error code correctly! This oughta change someday...
82 /* Since the dos.library destroyes the file handle anyway, even if the closing fails, we cannot
83 report the error code correctly, so just close the file and get out of here */
87 fdesc->fh!=__stdfiles[STDIN_FILENO] &&
88 fdesc->fh!=__stdfiles[STDOUT_FILENO] &&
89 fdesc->fh!=__stdfiles[STDERR_FILENO]
92 (void)Close(fdesc->fh);
95 free(fdesc);
98 __setfdesc(fd, NULL);
100 return 0;
101 } /* close */