r4722@vps: verhaegs | 2007-05-06 13:11:19 -0400
[cake.git] / rom / dos / open.c
blobd5c2173db107f597a8ce52e42568c92a6aaf21aa
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Open a file with the specified mode.
6 Lang: english
7 */
8 #include <exec/memory.h>
9 #include <exec/lists.h>
10 #include <proto/exec.h>
11 #include <utility/tagitem.h>
12 #include <dos/dosextens.h>
13 #include <dos/filesystem.h>
14 #include <dos/stdio.h>
15 #include <proto/dos.h>
16 #include <proto/utility.h>
17 #include "dos_intern.h"
19 /*****************************************************************************
21 NAME */
22 #include <proto/dos.h>
24 AROS_LH2(BPTR, Open,
26 /* SYNOPSIS */
27 AROS_LHA(CONST_STRPTR, name, D1),
28 AROS_LHA(LONG, accessMode, D2),
30 /* LOCATION */
31 struct DosLibrary *, DOSBase, 5, Dos)
33 /* FUNCTION
34 Opens a file for read and/or write depending on the accessmode given.
36 INPUTS
37 name - NUL terminated name of the file.
38 accessMode - One of MODE_OLDFILE - open existing file
39 MODE_NEWFILE - delete old, create new file
40 exclusive lock
41 MODE_READWRITE - open new one if it doesn't exist
43 RESULT
44 Handle to the file or 0 if the file couldn't be opened.
45 IoErr() gives additional information in that case.
47 NOTES
49 EXAMPLE
51 BUGS
53 SEE ALSO
55 INTERNALS
57 *****************************************************************************/
59 AROS_LIBFUNC_INIT
61 struct FileHandle *ret;
62 BPTR con, ast;
63 LONG error;
64 struct Process *me;
66 /* Sanity check */
67 if (name == NULL) return NULL;
69 /* Get pointer to process structure */
70 me = (struct Process *)FindTask(NULL);
72 /* Create filehandle */
73 ret = (struct FileHandle *)AllocDosObject(DOS_FILEHANDLE,NULL);
75 if(ret != NULL)
77 LONG doappend = 0;
79 /* Get pointer to I/O request. Use stackspace for now. */
80 struct IOFileSys iofs;
82 /* Prepare I/O request. */
83 InitIOFS(&iofs, FSA_OPEN_FILE, DOSBase);
85 /* io_Args[0] is the name which is set by DoName(). */
86 switch(accessMode)
88 case MODE_OLDFILE:
89 iofs.io_Union.io_OPEN_FILE.io_FileMode = FMF_MODE_OLDFILE;
90 ast = con = me->pr_CIS;
91 break;
93 case MODE_NEWFILE:
94 iofs.io_Union.io_OPEN_FILE.io_FileMode = FMF_MODE_NEWFILE;
95 con = me->pr_COS;
96 ast = me->pr_CES ? me->pr_CES : me->pr_COS;
97 break;
99 case MODE_READWRITE:
100 iofs.io_Union.io_OPEN_FILE.io_FileMode = FMF_MODE_READWRITE;
101 con = me->pr_COS;
102 ast = me->pr_CES ? me->pr_CES : me->pr_COS;
103 break;
105 default:
106 /* See if the user requested append mode */
107 doappend = accessMode & FMF_APPEND;
109 /* The append mode is all taken care by dos.library */
110 accessMode &= ~FMF_APPEND;
112 iofs.io_Union.io_OPEN_FILE.io_FileMode = accessMode;
113 ast = con = me->pr_CIS;
114 break;
117 iofs.io_Union.io_OPEN_FILE.io_Protection = 0;
119 if(!Stricmp(name, "IN:") || !Stricmp(name, "STDIN:"))
121 iofs.IOFS.io_Device = ((struct FileHandle *)BADDR(me->pr_CIS))->fh_Device;
122 iofs.IOFS.io_Unit = ((struct FileHandle *)BADDR(me->pr_CIS))->fh_Unit;
123 iofs.io_Union.io_OPEN_FILE.io_Filename = "";
124 DosDoIO(&iofs.IOFS);
125 error = me->pr_Result2 = iofs.io_DosError;
127 else
128 if(!Stricmp(name, "OUT:") || !Stricmp(name, "STDOUT:"))
130 iofs.IOFS.io_Device = ((struct FileHandle *)BADDR(me->pr_COS))->fh_Device;
131 iofs.IOFS.io_Unit = ((struct FileHandle *)BADDR(me->pr_COS))->fh_Unit;
132 iofs.io_Union.io_OPEN_FILE.io_Filename = "";
133 DosDoIO(&iofs.IOFS);
134 error = me->pr_Result2 = iofs.io_DosError;
136 else
137 if(!Stricmp(name, "ERR:") || !Stricmp(name, "STDERR:"))
139 iofs.IOFS.io_Device = ((struct FileHandle *)BADDR(me->pr_CES ? me->pr_CES : me->pr_COS))->fh_Device;
140 iofs.IOFS.io_Unit = ((struct FileHandle *)BADDR(me->pr_CES ? me->pr_CES : me->pr_COS))->fh_Unit;
141 iofs.io_Union.io_OPEN_FILE.io_Filename = "";
142 DosDoIO(&iofs.IOFS);
143 error = me->pr_Result2 = iofs.io_DosError;
145 else
146 if(!Stricmp(name, "CONSOLE:"))
148 iofs.IOFS.io_Device = ((struct FileHandle *)BADDR(con))->fh_Device;
149 iofs.IOFS.io_Unit = ((struct FileHandle *)BADDR(con))->fh_Unit;
150 iofs.io_Union.io_OPEN_FILE.io_Filename = "";
151 DosDoIO(&iofs.IOFS);
152 error = me->pr_Result2 = iofs.io_DosError;
154 else
155 if(!Stricmp(name, "*"))
157 iofs.IOFS.io_Device = ((struct FileHandle *)BADDR(ast))->fh_Device;
158 iofs.IOFS.io_Unit = ((struct FileHandle *)BADDR(ast))->fh_Unit;
159 iofs.io_Union.io_OPEN_FILE.io_Filename = "";
160 DosDoIO(&iofs.IOFS);
161 error = me->pr_Result2 = iofs.io_DosError;
163 else
164 error = DoName(&iofs, name, DOSBase);
166 if(error == 0)
168 ret->fh_Device = iofs.IOFS.io_Device;
169 ret->fh_Unit = iofs.IOFS.io_Unit;
170 if (doappend)
172 /* See if the handler supports FSA_SEEK */
173 if (Seek(MKBADDR(ret), 0, OFFSET_END) != -1)
175 /* if so then set the proper flag in the FileHandle struct */
176 ret->fh_Flags |= FHF_APPEND;
179 if (IsInteractive(MKBADDR(ret)))
180 SetVBuf(MKBADDR(ret), NULL, BUF_LINE, -1);
182 return MKBADDR(ret);
185 FreeDosObject(DOS_FILEHANDLE,ret);
188 else
189 SetIoErr(ERROR_NO_FREE_STORE);
191 return 0;
193 AROS_LIBFUNC_EXIT
194 } /* Open */