2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
8 #include <proto/exec.h>
12 #include <aros/libcall.h>
15 #include "__stdcio_intbase.h"
17 /*****************************************************************************
25 const char * restrict pathname
,
26 const char * restrict mode
)
29 Opens a file with the specified name in the specified mode.
32 pathname - Path and filename of the file you want to open.
33 mode - How to open the file:
35 r: Open for reading. The stream is positioned at the
36 beginning of the file.
38 r+: Open for reading and writing. The stream is positioned
39 at the beginning of the file.
41 w: Open for writing. If the file doesn't exist, then
42 it is created. If it does already exist, then
43 it is truncated. The stream is positioned at the
44 beginning of the file.
46 w+: Open for reading and writing. If the file doesn't
47 exist, then it is created. If it does already
48 exist, then it is truncated. The stream is
49 positioned at the beginning of the file.
51 a: Open for writing. If the file doesn't exist, then
52 it is created. The stream is positioned at the
55 a+: Open for reading and writing. If the file doesn't
56 exist, then it is created. The stream is positioned
57 at the end of the file.
59 b: Open in binary more. This has no effect and is ignored.
62 A pointer to a FILE handle or NULL in case of an error. When NULL
63 is returned, then errno is set to indicate the error.
70 Currently errno is not set on error.
73 fclose(), fread(), fwrite(), fgets(), fgetc(), fputs(), fputc()
77 ******************************************************************************/
79 struct StdCIOIntBase
*StdCIOBase
=
80 (struct StdCIOIntBase
*)__aros_getbase_StdCIOBase();
86 if (l2
== 'b') l2
= mode
[2];
87 hasplus
= (l2
== '+');
89 if (!StdCIOBase
->streampool
)
90 StdCIOBase
->streampool
= CreatePool(MEMF_ANY
, 20*sizeof(FILE), 2*sizeof(FILE));
91 if (!StdCIOBase
->streampool
)
93 SetIoErr(ERROR_NO_FREE_STORE
);
96 file
= AllocPooled(StdCIOBase
->streampool
, sizeof(FILE));
99 SetIoErr(ERROR_NO_FREE_STORE
);
102 file
->fh
= (BPTR
)NULL
;
107 file
->flags
= __STDCIO_STDIO_READ
;
108 fhmode
= MODE_OLDFILE
;
111 file
->flags
= __STDCIO_STDIO_WRITE
;
112 fhmode
= MODE_NEWFILE
;
115 file
->flags
= __STDCIO_STDIO_WRITE
|__STDCIO_STDIO_APPEND
;
116 fhmode
= MODE_READWRITE
;
122 file
->flags
|= __STDCIO_STDIO_RDWR
;
124 file
->fh
= Open(pathname
, fhmode
);
128 if (file
->flags
& __STDCIO_STDIO_APPEND
)
130 if (Seek(file
->fh
, 0, OFFSET_END
) < 0)
134 AddTail((struct List
*)&StdCIOBase
->files
, (struct Node
*)file
);
141 if (file
->fh
) Close(file
->fh
);
142 FreePooled(StdCIOBase
->streampool
, file
, sizeof(FILE));
144 errno
= __stdc_ioerr2errno(IoErr());