grub2: bring back build of aros-side grub2 tools
[AROS.git] / rom / dos / close.c
blob1953024116003ab3ccdc659e30aa71709dcf8952
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 #include <proto/exec.h>
10 #include <dos/dosextens.h>
12 #include <proto/dos.h>
13 #include "dos_intern.h"
14 #include <aros/debug.h>
16 /*****************************************************************************
18 NAME */
19 #include <proto/dos.h>
21 AROS_LH1(BOOL, Close,
23 /* SYNOPSIS */
24 AROS_LHA(BPTR, file, D1),
26 /* LOCATION */
27 struct DosLibrary *, DOSBase, 6, Dos)
29 /* FUNCTION
30 Close a filehandle opened with Open(). If the file was used
31 with buffered I/O, the final write may fail and thus Close()
32 may return an error. The file is closed in any case.
34 INPUTS
35 file - filehandle
37 RESULT
38 0 if there was an error. != 0 on success.
40 NOTES
42 EXAMPLE
44 BUGS
46 SEE ALSO
48 INTERNALS
50 *****************************************************************************/
52 AROS_LIBFUNC_INIT
54 /* Get pointer to filehandle */
55 struct FileHandle *fh = (struct FileHandle *)BADDR(file);
56 /* The returncode defaults to OK. */
57 BOOL ret = 1;
59 D(bug("[Close] %p: fh = %p\n", file, fh));
60 ASSERT_VALID_PTR_OR_NULL(fh);
62 /* 0 handles are OK */
63 if(file == BNULL)
64 return ret;
66 /* Func3 == -1: file was already closed. */
67 if (fh->fh_Func3 == -1)
68 Alert(AN_FileReclosed);
70 /* If the filehandle has a pending write on it Flush() the buffer. */
71 if(fh->fh_Flags & FHF_WRITE)
72 ret = Flush(file);
74 ret = dopacket1(DOSBase, NULL, fh->fh_Type, ACTION_END, fh->fh_Arg1);
76 /* Free the filehandle which was allocated in Open(), CreateDir()
77 and such. */
78 fh->fh_Func3 = -1;
79 FreeDosObject(DOS_FILEHANDLE, fh);
81 return ret;
83 AROS_LIBFUNC_EXIT
84 } /* Close */