* sysdeps/ia64/fpu/e_logl.c: File removed.
[glibc.git] / libio / iofopncook.c
blobeba3d435dcfa5254cce226351c32db3f7c60269c
1 /* Copyright (C) 1993,95,97,99,2000,2002,2004, 2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA.
20 As a special exception, if you link the code in this file with
21 files compiled with a GNU compiler to produce an executable,
22 that does not cause the resulting executable to be covered by
23 the GNU Lesser General Public License. This exception does not
24 however invalidate any other reasons why the executable file
25 might be covered by the GNU Lesser General Public License.
26 This exception applies to code released by its copyright holders
27 in files containing the exception. */
29 #include <libioP.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <shlib-compat.h>
34 /* Prototyped for local functions. */
35 static _IO_ssize_t _IO_cookie_read (register _IO_FILE* fp, void* buf,
36 _IO_ssize_t size);
37 static _IO_ssize_t _IO_cookie_write (register _IO_FILE* fp,
38 const void* buf, _IO_ssize_t size);
39 static _IO_off64_t _IO_cookie_seek (_IO_FILE *fp, _IO_off64_t offset, int dir);
40 static int _IO_cookie_close (_IO_FILE* fp);
42 static _IO_ssize_t
43 _IO_cookie_read (fp, buf, size)
44 _IO_FILE *fp;
45 void *buf;
46 _IO_ssize_t size;
48 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
50 if (cfile->__io_functions.read == NULL)
51 return -1;
53 return cfile->__io_functions.read (cfile->__cookie, buf, size);
56 static _IO_ssize_t
57 _IO_cookie_write (fp, buf, size)
58 _IO_FILE *fp;
59 const void *buf;
60 _IO_ssize_t size;
62 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
64 if (cfile->__io_functions.write == NULL)
65 return -1;
67 return cfile->__io_functions.write (cfile->__cookie, buf, size);
70 static _IO_off64_t
71 _IO_cookie_seek (fp, offset, dir)
72 _IO_FILE *fp;
73 _IO_off64_t offset;
74 int dir;
76 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
78 return ((cfile->__io_functions.seek == NULL
79 || (cfile->__io_functions.seek (cfile->__cookie, &offset, dir)
80 == -1)
81 || offset == (_IO_off64_t) -1)
82 ? _IO_pos_BAD : offset);
85 static int
86 _IO_cookie_close (fp)
87 _IO_FILE *fp;
89 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
91 if (cfile->__io_functions.close == NULL)
92 return 0;
94 return cfile->__io_functions.close (cfile->__cookie);
98 static _IO_off64_t
99 _IO_cookie_seekoff (fp, offset, dir, mode)
100 _IO_FILE *fp;
101 _IO_off64_t offset;
102 int dir;
103 int mode;
105 /* We must force the fileops code to always use seek to determine
106 the position. */
107 fp->_offset = _IO_pos_BAD;
108 return INTUSE(_IO_file_seekoff) (fp, offset, dir, mode);
112 static const struct _IO_jump_t _IO_cookie_jumps = {
113 JUMP_INIT_DUMMY,
114 JUMP_INIT(finish, INTUSE(_IO_file_finish)),
115 JUMP_INIT(overflow, INTUSE(_IO_file_overflow)),
116 JUMP_INIT(underflow, INTUSE(_IO_file_underflow)),
117 JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
118 JUMP_INIT(pbackfail, INTUSE(_IO_default_pbackfail)),
119 JUMP_INIT(xsputn, INTUSE(_IO_file_xsputn)),
120 JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
121 JUMP_INIT(seekoff, _IO_cookie_seekoff),
122 JUMP_INIT(seekpos, _IO_default_seekpos),
123 JUMP_INIT(setbuf, INTUSE(_IO_file_setbuf)),
124 JUMP_INIT(sync, INTUSE(_IO_file_sync)),
125 JUMP_INIT(doallocate, INTUSE(_IO_file_doallocate)),
126 JUMP_INIT(read, _IO_cookie_read),
127 JUMP_INIT(write, _IO_cookie_write),
128 JUMP_INIT(seek, _IO_cookie_seek),
129 JUMP_INIT(close, _IO_cookie_close),
130 JUMP_INIT(stat, _IO_default_stat),
131 JUMP_INIT(showmanyc, _IO_default_showmanyc),
132 JUMP_INIT(imbue, _IO_default_imbue),
136 void
137 _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
138 void *cookie, _IO_cookie_io_functions_t io_functions)
140 INTUSE(_IO_init) (&cfile->__fp.file, 0);
141 _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
143 cfile->__cookie = cookie;
144 cfile->__io_functions = io_functions;
146 INTUSE(_IO_file_init) (&cfile->__fp);
148 cfile->__fp.file._IO_file_flags =
149 _IO_mask_flags (&cfile->__fp.file, read_write,
150 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
152 /* We use a negative number different from -1 for _fileno to mark that
153 this special stream is not associated with a real file, but still has
154 to be treated as such. */
155 cfile->__fp.file._fileno = -2;
159 _IO_FILE *
160 _IO_fopencookie (cookie, mode, io_functions)
161 void *cookie;
162 const char *mode;
163 _IO_cookie_io_functions_t io_functions;
165 int read_write;
166 struct locked_FILE
168 struct _IO_cookie_file cfile;
169 #ifdef _IO_MTSAFE_IO
170 _IO_lock_t lock;
171 #endif
172 } *new_f;
174 switch (*mode++)
176 case 'r':
177 read_write = _IO_NO_WRITES;
178 break;
179 case 'w':
180 read_write = _IO_NO_READS;
181 break;
182 case 'a':
183 read_write = _IO_NO_READS|_IO_IS_APPENDING;
184 break;
185 default:
186 return NULL;
188 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
189 read_write &= _IO_IS_APPENDING;
191 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
192 if (new_f == NULL)
193 return NULL;
194 #ifdef _IO_MTSAFE_IO
195 new_f->cfile.__fp.file._lock = &new_f->lock;
196 #endif
198 _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions);
200 return (_IO_FILE *) &new_f->cfile.__fp;
203 versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2);
205 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
207 static _IO_off64_t _IO_old_cookie_seek (_IO_FILE *fp, _IO_off64_t offset,
208 int dir);
209 _IO_FILE * _IO_old_fopencookie (void *cookie, const char *mode,
210 _IO_cookie_io_functions_t io_functions);
212 static _IO_off64_t
213 attribute_compat_text_section
214 _IO_old_cookie_seek (fp, offset, dir)
215 _IO_FILE *fp;
216 _IO_off64_t offset;
217 int dir;
219 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
220 int (*seek) (_IO_FILE *, _IO_off_t, int);
221 int ret;
223 seek = (int (*)(_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;
224 if (seek == NULL)
225 return _IO_pos_BAD;
227 ret = seek (cfile->__cookie, offset, dir);
229 return (ret == -1) ? _IO_pos_BAD : ret;
232 static const struct _IO_jump_t _IO_old_cookie_jumps = {
233 JUMP_INIT_DUMMY,
234 JUMP_INIT(finish, INTUSE(_IO_file_finish)),
235 JUMP_INIT(overflow, INTUSE(_IO_file_overflow)),
236 JUMP_INIT(underflow, INTUSE(_IO_file_underflow)),
237 JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
238 JUMP_INIT(pbackfail, INTUSE(_IO_default_pbackfail)),
239 JUMP_INIT(xsputn, INTUSE(_IO_file_xsputn)),
240 JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
241 JUMP_INIT(seekoff, _IO_cookie_seekoff),
242 JUMP_INIT(seekpos, _IO_default_seekpos),
243 JUMP_INIT(setbuf, INTUSE(_IO_file_setbuf)),
244 JUMP_INIT(sync, INTUSE(_IO_file_sync)),
245 JUMP_INIT(doallocate, INTUSE(_IO_file_doallocate)),
246 JUMP_INIT(read, _IO_cookie_read),
247 JUMP_INIT(write, _IO_cookie_write),
248 JUMP_INIT(seek, _IO_old_cookie_seek),
249 JUMP_INIT(close, _IO_cookie_close),
250 JUMP_INIT(stat, _IO_default_stat),
251 JUMP_INIT(showmanyc, _IO_default_showmanyc),
252 JUMP_INIT(imbue, _IO_default_imbue),
255 _IO_FILE *
256 attribute_compat_text_section
257 _IO_old_fopencookie (cookie, mode, io_functions)
258 void *cookie;
259 const char *mode;
260 _IO_cookie_io_functions_t io_functions;
262 _IO_FILE *ret;
264 ret = _IO_fopencookie (cookie, mode, io_functions);
265 if (ret != NULL)
266 _IO_JUMPS ((struct _IO_FILE_plus *) ret) = &_IO_old_cookie_jumps;
268 return ret;
271 compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0);
273 #endif