Indentation fix, cleanup.
[AROS.git] / compiler / arossupport / rt_dos.c
blobef6b62c284c2909698e6a2de15d1ba242585b289
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Basic functions for ressource tracking
6 Lang: english
7 */
9 #include "rt.h"
10 #if 0
11 #define ENABLE_RT 0 /* no RT inside this file */
12 #define RT_INTERNAL 1
13 #include <aros/rt.h>
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>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include "etask.h"
24 #endif
25 #include <stdio.h>
26 #include <proto/exec.h>
27 #include <proto/arossupport.h>
28 #include <proto/dos.h>
29 #include <proto/alib.h>
31 typedef struct
33 RTNode Node;
34 BPTR FH;
35 STRPTR Path;
36 ULONG Mode;
38 FileResource;
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[] =
47 { /* RTT_FILE */
48 sizeof (FileResource),
49 (RT_AllocFunc) RT_Open,
50 (RT_FreeFunc) RT_Close,
51 RT_Search,
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 /**************************************
67 RT Files
68 **************************************/
70 static IPTR RT_Open (RTData * rtd, FileResource * rt, va_list args, BOOL * success)
72 STRPTR path;
74 path = va_arg (args, STRPTR);
76 if (!CheckPtr (path, 0))
78 kprintf ("Open(): Illegal path\n"
79 " path=%p at %s:%d\n"
80 , path
81 , rt->Node.File, rt->Node.Line
83 return 0ul;
86 rt->Mode = va_arg (args, LONG);
88 rt->FH = BNULL;
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"
98 , rt->Mode
99 , rt->Node.File, rt->Node.Line
102 else
104 rt->Path = StrDup (path);
106 if (!rt->Path)
108 kprintf ("Open(): RT: Out of memory\n");
110 else
112 rt->FH = Open (rt->Path, rt->Mode);
114 if (!rt->FH)
115 FreeVec (rt->Path);
119 if (rt->FH)
120 *success = TRUE;
122 return (IPTR)(rt->FH);
123 } /* RT_Open */
125 static IPTR RT_Close (RTData * rtd, FileResource * rt)
127 Close (rt->FH);
128 FreeVec (rt->Path);
130 return TRUE;
131 } /* RT_Close */
133 static CONST_STRPTR GetFileMode (LONG mode)
135 static char buffer[64];
137 switch (mode)
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);
146 return buffer;
147 } /* GetFileMode */
149 static IPTR RT_ShowErrorFile (RTData * rtd, int rtt, FileResource * rt,
150 IPTR ret, int mode, const char * file, ULONG line, va_list args)
152 if (mode != RT_EXIT)
154 const char * modestr = (mode == RT_FREE) ? "Close" : "Check";
155 BPTR fh;
157 fh = va_arg (args, BPTR);
159 switch (ret)
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"
165 " %s at %s:%d\n"
166 " Added at %s:%d\n"
167 " FH=%p\n"
168 , modestr
169 , modestr
170 , file, line
171 , rt->Node.File, rt->Node.Line
172 , rt->FH
175 break;
177 case RT_SEARCH_NOT_FOUND:
178 kprintf ("RT%s: File not found\n"
179 " %s at %s:%d\n"
180 " FH=%p\n"
181 , modestr
182 , modestr
183 , file, line
184 , fh
186 break;
188 } /* switch */
190 else
192 kprintf ("RTExit: File was not closed\n"
193 " Opened at %s:%d\n"
194 " FH=%p Path=%s Mode=%s\n"
195 , rt->Node.File, rt->Node.Line
196 , rt->FH, rt->Path, GetFileMode (rt->Mode)
200 return ret;
201 } /* RT_ShowErrorFile */
203 static IPTR RT_CheckFile (RTData * rtd, int rtt,
204 const char * file, ULONG line,
205 ULONG op, va_list args)
207 FileResource * rt;
208 APTR prt = &rt;
210 if (RT_Search (rtd, rtt, (RTNode **)prt, args) != RT_SEARCH_FOUND)
211 rt = NULL;
213 switch (op)
215 case RTTO_Read:
217 BPTR fh;
218 APTR buffer;
219 ULONG length;
221 fh = va_arg (args, BPTR);
222 buffer = va_arg (args, APTR);
223 length = va_arg (args, ULONG);
225 if (!rt)
227 kprintf ("Read(): Illegal filehandle\n"
228 " fh=%p at %s:%d\n"
229 , fh
230 , file, line
233 return -1;
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"
240 " opened at %s:%d\n"
241 , buffer
242 , file, line
243 , rt->FH, rt->Path, GetFileMode (rt->Mode)
244 , rt->Node.File, rt->Node.Line
247 return -1;
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"
254 " opened at %s:%d\n"
255 , buffer, length
256 , file, line
257 , rt->FH, rt->Path, GetFileMode (rt->Mode)
258 , rt->Node.File, rt->Node.Line
261 return -1;
264 return Read (fh, buffer, length);
267 case RTTO_Write:
269 BPTR fh;
270 APTR buffer;
271 ULONG length;
273 fh = va_arg (args, BPTR);
274 buffer = va_arg (args, APTR);
275 length = va_arg (args, ULONG);
277 if (!rt)
279 kprintf ("Write(): Illegal filehandle\n"
280 " fh=%p at %s:%d\n"
281 , fh
282 , file, line
285 return -1;
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"
292 " opened at %s:%d\n"
293 , buffer
294 , file, line
295 , rt->FH, rt->Path, GetFileMode (rt->Mode)
296 , rt->Node.File, rt->Node.Line
299 return -1;
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"
306 " opened at %s:%d\n"
307 , buffer, length
308 , file, line
309 , rt->FH, rt->Path, GetFileMode (rt->Mode)
310 , rt->Node.File, rt->Node.Line
313 return -1;
316 return Write (fh, buffer, length);
321 return 0L;
322 } /* RT_CheckFile */