OSDN Git Service

rework internal uClibc mutexes to support futex locking, and nptl
[uclinux-h8/uClibc.git] / libc / stdio / _fopen.c
1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #include "_stdio.h"
9
10
11 /*
12  * Cases:
13  *  fopen64  : filename != NULL, stream == NULL, filedes == -2
14  *  fopen    : filename != NULL, stream == NULL, filedes == -1
15  *  freopen  : filename != NULL, stream != NULL, filedes == -1
16  *  fdopen   : filename == NULL, stream == NULL, filedes valid
17  *
18  *  fsfopen  : filename != NULL, stream != NULL, filedes == -1
19  */
20
21 #if (O_ACCMODE != 3) || (O_RDONLY != 0) || (O_WRONLY != 1) || (O_RDWR != 2)
22 #error Assumption violated - mode constants
23 #endif
24
25 #ifndef O_LARGEFILE
26 #define O_LARGEFILE 0
27 #endif
28
29 /* Internal function -- reentrant (locks open file list) */
30
31 FILE attribute_hidden *_stdio_fopen(intptr_t fname_or_mode,
32                                    register const char * __restrict mode,
33                                    register FILE * __restrict stream, int filedes)
34 {
35         __mode_t open_mode;
36         int i;
37
38         /* Parse the specified mode. */
39         open_mode = O_RDONLY;
40         if (*mode != 'r') {                     /* Not read... */
41                 open_mode = (O_WRONLY | O_CREAT | O_TRUNC);
42                 if (*mode != 'w') {             /* Not write (create or truncate)... */
43                         open_mode = (O_WRONLY | O_CREAT | O_APPEND);
44                         if (*mode != 'a') {     /* Not write (create or append)... */
45                         DO_EINVAL:
46                                 __set_errno(EINVAL); /* So illegal mode. */
47                                 if (stream) {
48                                 FREE_STREAM:
49                                         assert(!(stream->__modeflags & __FLAG_FREEBUF));
50                                         __STDIO_STREAM_FREE_FILE(stream);
51                                 }
52                                 return NULL;
53                         }
54                 }
55         }
56
57         if ((mode[1] == 'b')) {         /* Binary mode (NO-OP currently). */
58                 ++mode;
59         }
60
61         if (mode[1] == '+') {                   /* Read and Write. */
62                 ++mode;
63                 open_mode |= (O_RDONLY | O_WRONLY);
64                 open_mode += (O_RDWR - (O_RDONLY | O_WRONLY));
65         }
66
67 #ifdef __UCLIBC_MJN3_ONLY__
68 #warning CONSIDER: Implement glibc ccs option to bind a codeset?
69 #warning CONSIDER: Implement glibc mmap option for readonly files?
70 #warning CONSIDER: Implement a text mode using custom read/write funcs?
71 #endif
72 #if defined(__UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__) || defined(__UCLIBC_HAS_FOPEN_LARGEFILE_MODE__)
73
74         while (*++mode) {
75 # ifdef __UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__
76                 if (*mode == 'x') {        /* Open exclusive (a glibc extension). */
77                         open_mode |= O_EXCL;
78                         continue;
79                 }
80 # endif
81 # ifdef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
82                 if (*mode == 'F') {             /* Open as large file (uClibc extension). */
83                         open_mode |= O_LARGEFILE;
84                         continue;
85                 }
86 # endif
87         }
88
89 #endif
90
91         if (!stream) {            /* Need to allocate a FILE (not freopen). */
92                 if ((stream = malloc(sizeof(FILE))) == NULL) {
93                         return stream;
94                 }
95                 stream->__modeflags = __FLAG_FREEFILE;
96 #ifdef __STDIO_BUFFERS
97                 stream->__bufstart = NULL; /* We allocate a buffer below. */
98 #endif
99 #ifdef __UCLIBC_HAS_THREADS__
100                 /* We only initialize the mutex in the non-freopen case. */
101                 /* stream->__user_locking = _stdio_user_locking; */
102                 STDIO_INIT_MUTEX(stream->__lock);
103 #endif
104         }
105
106 #ifdef __UCLIBC_MJN3_ONLY__
107 #warning TODO: Verify fdopen append behavior of glibc.
108 #endif
109
110         if (filedes >= 0) {                     /* Handle fdopen trickery. */
111                 stream->__filedes = filedes;
112                 /* NOTE: it is insufficient to just check R/W/RW agreement.
113                  * We must also check largefile compatibility if applicable.
114                  * Also, if append mode is desired for fdopen but O_APPEND isn't
115                  * currently set, then set it as recommended by SUSv3.  However,
116                  * if append mode is not specified for fdopen but O_APPEND is set,
117                  * leave it set (glibc compat). */
118                 i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
119
120                 /* NOTE: fopencookie needs changing if the basic check changes! */
121                 if ((i & ((int)fname_or_mode + 1)) != i) /* Basic agreement? */
122                         goto DO_EINVAL;
123                 if ((open_mode & ~(__mode_t)fname_or_mode) & O_APPEND) {
124                         if (fcntl(filedes, F_SETFL, O_APPEND))  /* Need O_APPEND. */
125                                 goto DO_EINVAL;
126                 }
127                 /* For later... to reflect largefile setting in stream flags. */
128                 __STDIO_WHEN_LFS( open_mode |= (((__mode_t) fname_or_mode)
129                                                                                 & O_LARGEFILE) );
130         } else {
131                 __STDIO_WHEN_LFS( if (filedes < -1) open_mode |= O_LARGEFILE );
132                 if ((stream->__filedes = open(((const char *) fname_or_mode),
133                                                                           open_mode, 0666)) < 0) {
134                         goto FREE_STREAM;
135                 }
136         }
137
138         stream->__modeflags &= __FLAG_FREEFILE;
139 /*      stream->__modeflags &= ~(__FLAG_READONLY|__FLAG_WRITEONLY); */
140
141         stream->__modeflags |=          /* Note: Makes assumptions about flag vals! */
142 #if (O_APPEND != __FLAG_APPEND) || ((O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0))
143 # if (O_APPEND != __FLAG_APPEND)
144                 ((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
145 # else
146                 (open_mode & O_APPEND) |
147 # endif
148 # if (O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0)
149                 ((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
150 # else
151                 (open_mode & O_LARGEFILE) |
152 # endif
153 #else
154                 (open_mode & (O_APPEND|O_LARGEFILE)) | /* i386 linux and elks */
155 #endif
156                 ((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
157
158 #ifdef __STDIO_BUFFERS
159         if (stream->__filedes != INT_MAX) {
160                 /* NB: fopencookie uses bogus filedes == INT_MAX,
161                  * avoiding isatty() in that case.
162                  */
163                 i = errno; /* preserve errno against isatty call. */
164                 if (isatty(stream->__filedes))
165                         stream->__modeflags |= __FLAG_LBF;
166                 __set_errno(i);
167         }
168
169         if (!stream->__bufstart) {
170                 if ((stream->__bufstart = malloc(BUFSIZ)) != NULL) {
171                         stream->__bufend = stream->__bufstart + BUFSIZ;
172                         stream->__modeflags |= __FLAG_FREEBUF;
173                 } else {
174 # if __STDIO_BUILTIN_BUF_SIZE > 0
175                         stream->__bufstart = stream->__builtinbuf;
176                         stream->__bufend = stream->__builtinbuf + sizeof(stream->__builtinbuf);
177 # else  /* __STDIO_BUILTIN_BUF_SIZE > 0 */
178                         stream->__bufend = stream->__bufstart;
179 # endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
180                 }
181         }
182
183         __STDIO_STREAM_DISABLE_GETC(stream);
184         __STDIO_STREAM_DISABLE_PUTC(stream);
185         __STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
186 #endif
187
188         __STDIO_STREAM_RESET_GCS(stream);
189
190 #ifdef __UCLIBC_HAS_WCHAR__
191         stream->__ungot_width[0] = 0;
192 #endif
193 #ifdef __STDIO_MBSTATE
194         __INIT_MBSTATE(&(stream->__state));
195 #endif
196
197 #ifdef __UCLIBC_HAS_THREADS__
198         /* Even in the freopen case, we reset the user locking flag. */
199         stream->__user_locking = _stdio_user_locking;
200         /* STDIO_INIT_MUTEX(stream->__lock); */
201 #endif
202
203 #ifdef __STDIO_HAS_OPENLIST
204 #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
205         if (!(stream->__modeflags & __FLAG_FREEFILE))
206         {
207                 /* An freopen call so the file was never removed from the list. */
208         }
209         else
210 #endif
211         {
212                 /* We have to lock the del mutex in case another thread wants to fclose()
213                  * the last file. */
214                 __STDIO_THREADLOCK_OPENLIST_DEL;
215                 __STDIO_THREADLOCK_OPENLIST_ADD;
216                 stream->__nextopen = _stdio_openlist; /* New files are inserted at */
217                 _stdio_openlist = stream;                         /*   the head of the list. */
218                 __STDIO_THREADUNLOCK_OPENLIST_ADD;
219                 __STDIO_THREADUNLOCK_OPENLIST_DEL;
220         }
221 #endif
222
223         __STDIO_STREAM_VALIDATE(stream);
224
225         return stream;
226 }