2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Basic functions for ressource tracking
11 #define ENABLE_RT 0 /* no RT inside this file */
15 #include <exec/lists.h>
16 #include <aros/system.h>
17 #include <exec/tasks.h>
18 #include <exec/ports.h>
19 #include <exec/memory.h>
20 #include <exec/execbase.h>
26 #include <proto/exec.h>
27 #include <proto/arossupport.h>
28 #include <proto/dos.h>
29 #include <proto/alib.h>
40 static IPTR
RT_Open (RTData
* rtd
, FileResource
* rt
, va_list args
, BOOL
* success
);
41 static IPTR
RT_Close (RTData
* rtd
, FileResource
* rt
);
42 static IPTR
RT_ShowErrorFile (RTData
* rtd
, int, FileResource
*, IPTR
, int, const char * file
, ULONG line
, va_list);
43 static IPTR
RT_CheckFile (RTData
* rtd
, int desc
, const char * file
, ULONG line
, ULONG op
, va_list args
);
45 static const RTDesc RT_DosResources
[] =
48 sizeof (FileResource
),
49 (RT_AllocFunc
) RT_Open
,
50 (RT_FreeFunc
) RT_Close
,
52 (RT_ShowError
) RT_ShowErrorFile
,
53 (RT_CheckFunc
) RT_CheckFile
,
57 void RT_InitDos (void)
59 RT_Resources
[RTT_FILE
] = &RT_DosResources
[0];
62 void RT_ExitDos (void)
66 /**************************************
68 **************************************/
70 static IPTR
RT_Open (RTData
* rtd
, FileResource
* rt
, va_list args
, BOOL
* success
)
75 path
= va_arg (args
, STRPTR
);
77 if (!CheckPtr (path
, 0))
79 kprintf ("Open(): Illegal path\n"
82 , rt
->Node
.File
, rt
->Node
.Line
87 rt
->Mode
= va_arg (args
, LONG
);
93 rt
->Mode
!= MODE_OLDFILE
94 && rt
->Mode
!= MODE_NEWFILE
95 && rt
->Mode
!= MODE_READWRITE
98 kprintf ("Open(): Illegal mode %d at %s:%d\n"
100 , rt
->Node
.File
, rt
->Node
.Line
105 rt
->Path
= StrDup (path
);
109 kprintf ("Open(): RT: Out of memory\n");
113 rt
->FH
= Open (rt
->Path
, rt
->Mode
);
123 return (IPTR
)(rt
->FH
);
126 static IPTR
RT_Close (RTData
* rtd
, FileResource
* rt
)
135 static const STRPTR
GetFileMode (LONG mode
)
137 static char buffer
[64];
141 case MODE_OLDFILE
: return "MODE_OLDFILE";
142 case MODE_NEWFILE
: return "MODE_NEWFILE";
143 case MODE_READWRITE
: return "MODE_READWRITE";
146 sprintf (buffer
, "<illegal mode %ld>", mode
);
151 static IPTR
RT_ShowErrorFile (RTData
* rtd
, int rtt
, FileResource
* rt
,
152 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
156 const char * modestr
= (mode
== RT_FREE
) ? "Close" : "Check";
159 fh
= va_arg (args
, BPTR
);
163 case RT_SEARCH_FOUND
:
164 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
166 kprintf ("RT%s: Try to free read-only resource: File\n"
173 , rt
->Node
.File
, rt
->Node
.Line
179 case RT_SEARCH_NOT_FOUND
:
180 kprintf ("RT%s: File not found\n"
194 kprintf ("RTExit: File was not closed\n"
196 " FH=%p Path=%s Mode=%s\n"
197 , rt
->Node
.File
, rt
->Node
.Line
198 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
203 } /* RT_ShowErrorFile */
205 static IPTR
RT_CheckFile (RTData
* rtd
, int rtt
,
206 const char * file
, ULONG line
,
207 ULONG op
, va_list args
)
212 if (RT_Search (rtd
, rtt
, (RTNode
**)prt
, args
) != RT_SEARCH_FOUND
)
223 fh
= va_arg (args
, BPTR
);
224 buffer
= va_arg (args
, APTR
);
225 length
= va_arg (args
, ULONG
);
229 kprintf ("Read(): Illegal filehandle\n"
237 else if (!CheckPtr (buffer
, 0))
239 kprintf ("Read(): Illegal buffer\n"
240 " buffer=%p at %s:%d\n"
241 " FH=%p Path=%s Mode=%s\n"
245 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
246 , rt
->Node
.File
, rt
->Node
.Line
251 else if (!CheckArea (buffer
, length
, 0))
253 kprintf ("Read(): Illegal buffer\n"
254 " buffer=%p, size=%d at %s:%d\n"
255 " FH=%p Path=%s Mode=%s\n"
259 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
260 , rt
->Node
.File
, rt
->Node
.Line
266 return Read (fh
, buffer
, length
);
275 fh
= va_arg (args
, BPTR
);
276 buffer
= va_arg (args
, APTR
);
277 length
= va_arg (args
, ULONG
);
281 kprintf ("Write(): Illegal filehandle\n"
289 else if (!CheckPtr (buffer
, 0))
291 kprintf ("Write(): Illegal buffer\n"
292 " buffer=%p at %s:%d\n"
293 " FH=%p Path=%s Mode=%s\n"
297 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
298 , rt
->Node
.File
, rt
->Node
.Line
303 else if (!CheckArea (buffer
, length
, 0))
305 kprintf ("Write(): Illegal buffer\n"
306 " buffer=%p, size=%d at %s:%d\n"
307 " FH=%p Path=%s Mode=%s\n"
311 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
312 , rt
->Node
.File
, rt
->Node
.Line
318 return Write (fh
, buffer
, length
);