OSDN Git Service

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