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 
_stdio_fopen(intptr_t fname_or_mode,register const char * __restrict mode,register FILE * __restrict stream,int filedes)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 #if defined(__UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__) || defined(__UCLIBC_HAS_FOPEN_LARGEFILE_MODE__) || \
68     defined(__UCLIBC_HAS_FOPEN_CLOSEEXEC_MODE__)
69 
70 	while (*++mode) {
71 # ifdef __UCLIBC_HAS_FOPEN_EXCLUSIVE_MODE__
72 		if (*mode == 'x') {	   /* Open exclusive (a glibc extension). */
73 			open_mode |= O_EXCL;
74 			continue;
75 		}
76 # endif
77 # ifdef __UCLIBC_HAS_FOPEN_LARGEFILE_MODE__
78 		if (*mode == 'F') {		/* Open as large file (uClibc extension). */
79 			open_mode |= O_LARGEFILE;
80 			continue;
81 		}
82 # endif
83 # ifdef __UCLIBC_HAS_FOPEN_CLOSEEXEC_MODE__
84 		if (*mode == 'e') {		/* Close on exec (a glibc extension). */
85 			open_mode |= O_CLOEXEC;
86 			continue;
87 		}
88 # endif
89  	}
90 
91 #endif
92 
93 	if (!stream) {		  /* Need to allocate a FILE (not freopen). */
94 		if ((stream = malloc(sizeof(FILE))) == NULL) {
95 			return stream;
96 		}
97 		stream->__modeflags = __FLAG_FREEFILE;
98 #ifdef __STDIO_BUFFERS
99 		stream->__bufstart = NULL; /* We allocate a buffer below. */
100 #endif
101 #ifdef __UCLIBC_HAS_THREADS__
102 		/* We only initialize the mutex in the non-freopen case. */
103 		/* stream->__user_locking = _stdio_user_locking; */
104 		STDIO_INIT_MUTEX(stream->__lock);
105 #endif
106 	}
107 
108 	if (filedes >= 0) {			/* Handle fdopen trickery. */
109 		stream->__filedes = filedes;
110 		/* NOTE: it is insufficient to just check R/W/RW agreement.
111 		 * We must also check largefile compatibility if applicable.
112 		 * Also, if append mode is desired for fdopen but O_APPEND isn't
113 		 * currently set, then set it as recommended by SUSv3.  However,
114 		 * if append mode is not specified for fdopen but O_APPEND is set,
115 		 * leave it set (glibc compat). */
116 		i = (open_mode & (O_ACCMODE|O_LARGEFILE)) + 1;
117 
118 		/* NOTE: fopencookie needs changing if the basic check changes! */
119 		if ((i & ((int)fname_or_mode + 1)) != i) /* Basic agreement? */
120 			goto DO_EINVAL;
121 		if ((open_mode & ~(__mode_t)fname_or_mode) & O_APPEND) {
122 			if (fcntl(filedes, F_SETFL, O_APPEND))	/* Need O_APPEND. */
123 				goto DO_EINVAL;
124 		}
125 		/* For later... to reflect largefile setting in stream flags. */
126 		__STDIO_WHEN_LFS( open_mode |= (((__mode_t) fname_or_mode)
127 										& O_LARGEFILE) );
128 	} else {
129 		__STDIO_WHEN_LFS( if (filedes < -1) open_mode |= O_LARGEFILE );
130 		if ((stream->__filedes = open(((const char *) fname_or_mode),
131 									  open_mode, 0666)) < 0) {
132 			goto FREE_STREAM;
133 		}
134 	}
135 
136 	stream->__modeflags &= __FLAG_FREEFILE;
137 /* 	stream->__modeflags &= ~(__FLAG_READONLY|__FLAG_WRITEONLY); */
138 
139 	stream->__modeflags |=		/* Note: Makes assumptions about flag vals! */
140 #if (O_APPEND != __FLAG_APPEND) || ((O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0))
141 # if (O_APPEND != __FLAG_APPEND)
142 		((open_mode & O_APPEND) ? __FLAG_APPEND : 0) |
143 # else
144 		(open_mode & O_APPEND) |
145 # endif
146 # if (O_LARGEFILE != __FLAG_LARGEFILE) && (O_LARGEFILE != 0)
147 		((open_mode & O_LARGEFILE) ? __FLAG_LARGEFILE : 0) |
148 # else
149 		(open_mode & O_LARGEFILE) |
150 # endif
151 #else
152 		(open_mode & (O_APPEND|O_LARGEFILE)) | /* i386 linux and elks */
153 #endif
154 		((((open_mode & O_ACCMODE) + 1) ^ 0x03) * __FLAG_WRITEONLY);
155 
156 #ifdef __STDIO_BUFFERS
157 	if (stream->__filedes != INT_MAX) {
158 		/* NB: fopencookie uses bogus filedes == INT_MAX,
159 		 * avoiding isatty() in that case.
160 		 */
161 		i = errno; /* preserve errno against isatty call. */
162 		if (isatty(stream->__filedes))
163 			stream->__modeflags |= __FLAG_LBF;
164 		__set_errno(i);
165 	}
166 
167 	if (!stream->__bufstart) {
168 		if ((stream->__bufstart = malloc(BUFSIZ)) != NULL) {
169 			stream->__bufend = stream->__bufstart + BUFSIZ;
170 			stream->__modeflags |= __FLAG_FREEBUF;
171 		} else {
172 # if __STDIO_BUILTIN_BUF_SIZE > 0
173 			stream->__bufstart = stream->__builtinbuf;
174 			stream->__bufend = stream->__builtinbuf + sizeof(stream->__builtinbuf);
175 # else  /* __STDIO_BUILTIN_BUF_SIZE > 0 */
176 			stream->__bufend = stream->__bufstart;
177 # endif /* __STDIO_BUILTIN_BUF_SIZE > 0 */
178 		}
179 	}
180 
181 	__STDIO_STREAM_DISABLE_GETC(stream);
182 	__STDIO_STREAM_DISABLE_PUTC(stream);
183 	__STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
184 #endif
185 
186 #ifdef __UCLIBC_HAS_WCHAR__
187 	stream->__ungot_width[0] = 0;
188 #endif
189 #ifdef __STDIO_MBSTATE
190 	__INIT_MBSTATE(&(stream->__state));
191 #endif
192 
193 #ifdef __UCLIBC_HAS_THREADS__
194 	/* Even in the freopen case, we reset the user locking flag. */
195 	stream->__user_locking = _stdio_user_locking;
196 	/* STDIO_INIT_MUTEX(stream->__lock); */
197 #endif
198 
199 #ifdef __STDIO_HAS_OPENLIST
200 #if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
201 	if (!(stream->__modeflags & __FLAG_FREEFILE))
202 	{
203 		/* An freopen call so the file was never removed from the list. */
204 	}
205 	else
206 #endif
207 	{
208 		/* We have to lock the del mutex in case another thread wants to fclose()
209 		 * the last file. */
210 		__STDIO_THREADLOCK_OPENLIST_DEL;
211 		__STDIO_THREADLOCK_OPENLIST_ADD;
212 		stream->__nextopen = _stdio_openlist; /* New files are inserted at */
213 		_stdio_openlist = stream;			  /*   the head of the list. */
214 		__STDIO_THREADUNLOCK_OPENLIST_ADD;
215 		__STDIO_THREADUNLOCK_OPENLIST_DEL;
216 	}
217 #endif
218 
219 	__STDIO_STREAM_VALIDATE(stream);
220 
221 	return stream;
222 }
223