linux/bootstrap: use Forbid/Permit only when thread is main AROS thread
[AROS.git] / compiler / stdc / fopen.c
blobf71da1c9727efbb39314d7a8f527ad7b66a82077
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function fopen().
6 */
7 #include <dos/dos.h>
8 #include <proto/exec.h>
9 #include <proto/dos.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <aros/libcall.h>
14 #include "__stdio.h"
15 #include "__stdcio_intbase.h"
17 /*****************************************************************************
19 NAME */
20 #include <stdio.h>
22 FILE * fopen (
24 /* SYNOPSIS */
25 const char * restrict pathname,
26 const char * restrict mode)
28 /* FUNCTION
29 Opens a file with the specified name in the specified mode.
31 INPUTS
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
53 end of the file.
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.
61 RESULT
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.
65 NOTES
67 EXAMPLE
69 BUGS
70 Currently errno is not set on error.
72 SEE ALSO
73 fclose(), fread(), fwrite(), fgets(), fgetc(), fputs(), fputc()
75 INTERNALS
77 ******************************************************************************/
79 struct StdCIOIntBase *StdCIOBase =
80 (struct StdCIOIntBase *)__aros_getbase_StdCIOBase();
81 int fhmode;
82 char l2, hasplus;
83 FILE *file = NULL;
85 l2 = mode[1];
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);
94 goto error;
96 file = AllocPooled(StdCIOBase->streampool, sizeof(FILE));
97 if (!file)
99 SetIoErr(ERROR_NO_FREE_STORE);
100 goto error;
102 file->fh = (BPTR)NULL;
104 switch(mode[0])
106 case 'r':
107 file->flags = __STDCIO_STDIO_READ;
108 fhmode = MODE_OLDFILE;
109 break;
110 case 'w':
111 file->flags = __STDCIO_STDIO_WRITE;
112 fhmode = MODE_NEWFILE;
113 break;
114 case 'a':
115 file->flags = __STDCIO_STDIO_WRITE|__STDCIO_STDIO_APPEND;
116 fhmode = MODE_READWRITE;
117 break;
118 default:
119 goto error;
121 if (hasplus)
122 file->flags |= __STDCIO_STDIO_RDWR;
124 file->fh = Open(pathname, fhmode);
125 if (!file->fh)
126 goto error;
128 if (file->flags & __STDCIO_STDIO_APPEND)
130 if (Seek(file->fh, 0, OFFSET_END) < 0)
131 goto error;
134 AddTail((struct List *)&StdCIOBase->files, (struct Node *)file);
136 return file;
138 error:
139 if (file)
141 if (file->fh) Close(file->fh);
142 FreePooled(StdCIOBase->streampool, file, sizeof(FILE));
144 errno = __stdc_ioerr2errno(IoErr());
145 return NULL;
146 } /* fopen */