2 Copyright © 1995-2007, 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
)
74 path
= va_arg (args
, STRPTR
);
76 if (!CheckPtr (path
, 0))
78 kprintf ("Open(): Illegal path\n"
81 , rt
->Node
.File
, rt
->Node
.Line
86 rt
->Mode
= va_arg (args
, LONG
);
92 rt
->Mode
!= MODE_OLDFILE
93 && rt
->Mode
!= MODE_NEWFILE
94 && rt
->Mode
!= MODE_READWRITE
97 kprintf ("Open(): Illegal mode %d at %s:%d\n"
99 , rt
->Node
.File
, rt
->Node
.Line
104 rt
->Path
= StrDup (path
);
108 kprintf ("Open(): RT: Out of memory\n");
112 rt
->FH
= Open (rt
->Path
, rt
->Mode
);
122 return (IPTR
)(rt
->FH
);
125 static IPTR
RT_Close (RTData
* rtd
, FileResource
* rt
)
133 static CONST_STRPTR
GetFileMode (LONG mode
)
135 static char buffer
[64];
139 case MODE_OLDFILE
: return "MODE_OLDFILE";
140 case MODE_NEWFILE
: return "MODE_NEWFILE";
141 case MODE_READWRITE
: return "MODE_READWRITE";
144 sprintf (buffer
, "<illegal mode %ld>", (long)mode
);
149 static IPTR
RT_ShowErrorFile (RTData
* rtd
, int rtt
, FileResource
* rt
,
150 IPTR ret
, int mode
, const char * file
, ULONG line
, va_list args
)
154 const char * modestr
= (mode
== RT_FREE
) ? "Close" : "Check";
157 fh
= va_arg (args
, BPTR
);
161 case RT_SEARCH_FOUND
:
162 if (rt
->Node
.Flags
& RTNF_DONT_FREE
)
164 kprintf ("RT%s: Try to free read-only resource: File\n"
171 , rt
->Node
.File
, rt
->Node
.Line
177 case RT_SEARCH_NOT_FOUND
:
178 kprintf ("RT%s: File not found\n"
192 kprintf ("RTExit: File was not closed\n"
194 " FH=%p Path=%s Mode=%s\n"
195 , rt
->Node
.File
, rt
->Node
.Line
196 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
201 } /* RT_ShowErrorFile */
203 static IPTR
RT_CheckFile (RTData
* rtd
, int rtt
,
204 const char * file
, ULONG line
,
205 ULONG op
, va_list args
)
210 if (RT_Search (rtd
, rtt
, (RTNode
**)prt
, args
) != RT_SEARCH_FOUND
)
221 fh
= va_arg (args
, BPTR
);
222 buffer
= va_arg (args
, APTR
);
223 length
= va_arg (args
, ULONG
);
227 kprintf ("Read(): Illegal filehandle\n"
235 else if (!CheckPtr (buffer
, 0))
237 kprintf ("Read(): Illegal buffer\n"
238 " buffer=%p at %s:%d\n"
239 " FH=%p Path=%s Mode=%s\n"
243 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
244 , rt
->Node
.File
, rt
->Node
.Line
249 else if (!CheckArea (buffer
, length
, 0))
251 kprintf ("Read(): Illegal buffer\n"
252 " buffer=%p, size=%d at %s:%d\n"
253 " FH=%p Path=%s Mode=%s\n"
257 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
258 , rt
->Node
.File
, rt
->Node
.Line
264 return Read (fh
, buffer
, length
);
273 fh
= va_arg (args
, BPTR
);
274 buffer
= va_arg (args
, APTR
);
275 length
= va_arg (args
, ULONG
);
279 kprintf ("Write(): Illegal filehandle\n"
287 else if (!CheckPtr (buffer
, 0))
289 kprintf ("Write(): Illegal buffer\n"
290 " buffer=%p at %s:%d\n"
291 " FH=%p Path=%s Mode=%s\n"
295 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
296 , rt
->Node
.File
, rt
->Node
.Line
301 else if (!CheckArea (buffer
, length
, 0))
303 kprintf ("Write(): Illegal buffer\n"
304 " buffer=%p, size=%d at %s:%d\n"
305 " FH=%p Path=%s Mode=%s\n"
309 , rt
->FH
, rt
->Path
, GetFileMode (rt
->Mode
)
310 , rt
->Node
.File
, rt
->Node
.Line
316 return Write (fh
, buffer
, length
);