added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / devs / nil_handler / nil_handler.c
blob553a392a0b55a637e9ea697fab25a71c34697049
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang:
7 */
8 #include <exec/errors.h>
9 #include <exec/resident.h>
10 #include <exec/memory.h>
11 #include <libraries/expansion.h>
12 #include <proto/exec.h>
13 #include <proto/expansion.h>
14 #include <utility/tagitem.h>
15 #include <dos/dosextens.h>
16 #include <dos/filesystem.h>
17 #include <proto/dos.h>
18 #include <aros/libcall.h>
19 #include <aros/symbolsets.h>
20 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
21 #include "nil_handler_gcc.h"
22 #endif
24 #include LC_LIBDEFS_FILE
26 #include <string.h>
28 static int OpenDev(LIBBASETYPEPTR nilbase, struct IOFileSys *iofs);
30 static int GM_UNIQUENAME(Init)(LIBBASETYPEPTR nilbase)
32 nilbase->dosbase=(struct DosLibrary *)OpenLibrary("dos.library",39);
33 if(nilbase->dosbase!=NULL)
35 if (nilbase->seglist==NULL) /* Are we a ROM module? */
37 struct DeviceNode *dn;
38 /* Install NIL: handler into device list
40 * KLUDGE: The mountlists for NIL: should be into dos.library bootstrap routines.
43 if((dn = AllocMem(sizeof (struct DeviceNode) + 4 + AROS_BSTR_MEMSIZE4LEN(3),
44 MEMF_CLEAR|MEMF_PUBLIC)))
46 struct IOFileSys dummyiofs;
48 if (OpenDev(nilbase, &dummyiofs))
50 BSTR s = (BSTR)MKBADDR(((IPTR)dn + sizeof(struct DeviceNode) + 3) & ~3);
52 nilbase->device.dd_Library.lib_OpenCnt++;
54 AROS_BSTR_putchar(s, 0, 'N');
55 AROS_BSTR_putchar(s, 1, 'I');
56 AROS_BSTR_putchar(s, 2, 'L');
57 AROS_BSTR_setstrlen(s, 3);
59 dn->dn_Type = DLT_DEVICE;
60 dn->dn_Ext.dn_AROS.dn_Unit = dummyiofs.IOFS.io_Unit;
61 dn->dn_Ext.dn_AROS.dn_Device = dummyiofs.IOFS.io_Device;
62 dn->dn_Handler = NULL;
63 dn->dn_Startup = NULL;
64 dn->dn_Name = s;
65 dn->dn_Ext.dn_AROS.dn_DevName = AROS_BSTR_ADDR(dn->dn_Name);
67 if (AddDosEntry((struct DosList *)dn))
68 return TRUE;
71 FreeMem(dn, sizeof (struct DeviceNode));
74 else
75 return TRUE;
78 return FALSE;
81 static int GM_UNIQUENAME(Open)
83 LIBBASETYPEPTR nilbase,
84 struct IOFileSys *iofs,
85 ULONG unitnum,
86 ULONG flags
89 /* Mark Message as recently used. */
90 iofs->IOFS.io_Message.mn_Node.ln_Type=NT_REPLYMSG;
92 if (OpenDev(nilbase, iofs))
93 return TRUE;
95 iofs->IOFS.io_Error=IOERR_OPENFAIL;
97 return FALSE;
100 static int OpenDev(LIBBASETYPEPTR nilbase, struct IOFileSys *iofs)
102 ULONG *dev;
104 dev=AllocMem(sizeof(ULONG),MEMF_PUBLIC|MEMF_CLEAR);
105 if(dev!=NULL)
107 iofs->IOFS.io_Unit = (struct Unit *)dev;
108 iofs->IOFS.io_Device = &nilbase->device;
109 iofs->IOFS.io_Error = 0;
110 return TRUE;
112 else
114 iofs->io_DosError=ERROR_NO_FREE_STORE;
115 return FALSE;
120 static int GM_UNIQUENAME(Close)(LIBBASETYPEPTR nilbase, struct IOFileSys *iofs)
122 ULONG *dev;
124 dev=(ULONG *)iofs->IOFS.io_Unit;
125 if(*dev)
127 iofs->io_DosError=ERROR_OBJECT_IN_USE;
128 return FALSE;
131 /* Let any following attemps to use the device crash hard. */
132 FreeMem(dev,sizeof(ULONG));
133 iofs->io_DosError=0;
135 return TRUE;
138 ADD2INITLIB(GM_UNIQUENAME(Init),0)
139 ADD2OPENDEV(GM_UNIQUENAME(Open),0)
140 ADD2CLOSEDEV(GM_UNIQUENAME(Close),0)
142 AROS_LH1(void, beginio,
143 AROS_LHA(struct IOFileSys *, iofs, A1),
144 struct nilbase *, nilbase, 5, Nil)
146 AROS_LIBFUNC_INIT
147 LONG error=0;
150 Do everything quick no matter what. This is possible
151 because I never need to Wait().
153 switch(iofs->IOFS.io_Command)
155 case FSA_OPEN:
156 case FSA_OPEN_FILE:
157 /* No names allowed on NIL: */
158 if (iofs->io_Union.io_NamedFile.io_Filename[0])
160 error=ERROR_OBJECT_NOT_FOUND;
161 break;
163 Forbid();
164 ++*(ULONG *)iofs->IOFS.io_Unit;
165 Permit();
166 break;
168 case FSA_READ:
169 iofs->io_Union.io_READ.io_Length=0;
170 break;
172 case FSA_WRITE:
173 break;
175 case FSA_SEEK:
176 iofs->io_Union.io_SEEK.io_Offset = 0;
177 break;
179 case FSA_CLOSE:
180 Forbid();
181 --*(ULONG *)iofs->IOFS.io_Unit;
182 Permit();
183 break;
185 case FSA_IS_INTERACTIVE:
186 iofs->io_Union.io_IS_INTERACTIVE.io_IsInteractive = TRUE;
187 break;
188 case FSA_SET_FILE_SIZE:
189 case FSA_EXAMINE:
190 case FSA_EXAMINE_NEXT:
191 case FSA_EXAMINE_ALL:
192 case FSA_CREATE_DIR:
193 case FSA_CREATE_HARDLINK:
194 case FSA_CREATE_SOFTLINK:
195 case FSA_RENAME:
196 case FSA_DELETE_OBJECT:
197 error = ERROR_NOT_IMPLEMENTED;
198 break;
200 default:
201 error = ERROR_ACTION_NOT_KNOWN;
202 break;
205 /* Set error code */
206 iofs->io_DosError=error;
208 /* If the quick bit is not set send the message to the port */
209 if(!(iofs->IOFS.io_Flags&IOF_QUICK))
210 ReplyMsg(&iofs->IOFS.io_Message);
212 AROS_LIBFUNC_EXIT
215 AROS_LH1(LONG, abortio,
216 AROS_LHA(struct IOFileSys *, iofs, A1),
217 struct nilbase *, nilbase, 6, Nil)
219 AROS_LIBFUNC_INIT
220 /* Everything already done. */
221 return 0;
222 AROS_LIBFUNC_EXIT