Support Intel processor model 6 and model 0x2.
[glibc.git] / libio / iofopncook.c
blobf36e4c741d3525fd54702fb3a27a35e57b92db79
1 /* Copyright (C) 1993,95,97,99,2000,2002,2004, 2005, 2010
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 _IO_off64_t _IO_cookie_seekoff (_IO_FILE *fp, _IO_off64_t offset,
41 int dir, int mode);
42 static int _IO_cookie_close (_IO_FILE* fp);
44 static _IO_ssize_t
45 _IO_cookie_read (fp, buf, size)
46 _IO_FILE *fp;
47 void *buf;
48 _IO_ssize_t size;
50 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
52 if (cfile->__io_functions.read == NULL)
53 return -1;
55 return cfile->__io_functions.read (cfile->__cookie, buf, size);
58 static _IO_ssize_t
59 _IO_cookie_write (fp, buf, size)
60 _IO_FILE *fp;
61 const void *buf;
62 _IO_ssize_t size;
64 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
66 if (cfile->__io_functions.write == NULL)
68 fp->_flags |= _IO_ERR_SEEN;
69 return 0;
72 _IO_ssize_t n = cfile->__io_functions.write (cfile->__cookie, buf, size);
73 if (n < size)
74 fp->_flags |= _IO_ERR_SEEN;
76 return n;
79 static _IO_off64_t
80 _IO_cookie_seek (fp, offset, dir)
81 _IO_FILE *fp;
82 _IO_off64_t offset;
83 int dir;
85 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
87 return ((cfile->__io_functions.seek == NULL
88 || (cfile->__io_functions.seek (cfile->__cookie, &offset, dir)
89 == -1)
90 || offset == (_IO_off64_t) -1)
91 ? _IO_pos_BAD : offset);
94 static int
95 _IO_cookie_close (fp)
96 _IO_FILE *fp;
98 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
100 if (cfile->__io_functions.close == NULL)
101 return 0;
103 return cfile->__io_functions.close (cfile->__cookie);
107 static _IO_off64_t
108 _IO_cookie_seekoff (fp, offset, dir, mode)
109 _IO_FILE *fp;
110 _IO_off64_t offset;
111 int dir;
112 int mode;
114 /* We must force the fileops code to always use seek to determine
115 the position. */
116 fp->_offset = _IO_pos_BAD;
117 return INTUSE(_IO_file_seekoff) (fp, offset, dir, mode);
121 static const struct _IO_jump_t _IO_cookie_jumps = {
122 JUMP_INIT_DUMMY,
123 JUMP_INIT(finish, INTUSE(_IO_file_finish)),
124 JUMP_INIT(overflow, INTUSE(_IO_file_overflow)),
125 JUMP_INIT(underflow, INTUSE(_IO_file_underflow)),
126 JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
127 JUMP_INIT(pbackfail, INTUSE(_IO_default_pbackfail)),
128 JUMP_INIT(xsputn, INTUSE(_IO_file_xsputn)),
129 JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
130 JUMP_INIT(seekoff, _IO_cookie_seekoff),
131 JUMP_INIT(seekpos, _IO_default_seekpos),
132 JUMP_INIT(setbuf, INTUSE(_IO_file_setbuf)),
133 JUMP_INIT(sync, INTUSE(_IO_file_sync)),
134 JUMP_INIT(doallocate, INTUSE(_IO_file_doallocate)),
135 JUMP_INIT(read, _IO_cookie_read),
136 JUMP_INIT(write, _IO_cookie_write),
137 JUMP_INIT(seek, _IO_cookie_seek),
138 JUMP_INIT(close, _IO_cookie_close),
139 JUMP_INIT(stat, _IO_default_stat),
140 JUMP_INIT(showmanyc, _IO_default_showmanyc),
141 JUMP_INIT(imbue, _IO_default_imbue),
145 void
146 _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
147 void *cookie, _IO_cookie_io_functions_t io_functions)
149 INTUSE(_IO_init) (&cfile->__fp.file, 0);
150 _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
152 cfile->__cookie = cookie;
153 cfile->__io_functions = io_functions;
155 INTUSE(_IO_file_init) (&cfile->__fp);
157 _IO_mask_flags (&cfile->__fp.file, read_write,
158 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
160 /* We use a negative number different from -1 for _fileno to mark that
161 this special stream is not associated with a real file, but still has
162 to be treated as such. */
163 cfile->__fp.file._fileno = -2;
167 _IO_FILE *
168 _IO_fopencookie (cookie, mode, io_functions)
169 void *cookie;
170 const char *mode;
171 _IO_cookie_io_functions_t io_functions;
173 int read_write;
174 struct locked_FILE
176 struct _IO_cookie_file cfile;
177 #ifdef _IO_MTSAFE_IO
178 _IO_lock_t lock;
179 #endif
180 } *new_f;
182 switch (*mode++)
184 case 'r':
185 read_write = _IO_NO_WRITES;
186 break;
187 case 'w':
188 read_write = _IO_NO_READS;
189 break;
190 case 'a':
191 read_write = _IO_NO_READS|_IO_IS_APPENDING;
192 break;
193 default:
194 return NULL;
196 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
197 read_write &= _IO_IS_APPENDING;
199 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
200 if (new_f == NULL)
201 return NULL;
202 #ifdef _IO_MTSAFE_IO
203 new_f->cfile.__fp.file._lock = &new_f->lock;
204 #endif
206 _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions);
208 return (_IO_FILE *) &new_f->cfile.__fp;
211 versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2);
213 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
215 static _IO_off64_t _IO_old_cookie_seek (_IO_FILE *fp, _IO_off64_t offset,
216 int dir);
217 _IO_FILE * _IO_old_fopencookie (void *cookie, const char *mode,
218 _IO_cookie_io_functions_t io_functions);
220 static _IO_off64_t
221 attribute_compat_text_section
222 _IO_old_cookie_seek (fp, offset, dir)
223 _IO_FILE *fp;
224 _IO_off64_t offset;
225 int dir;
227 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
228 int (*seek) (_IO_FILE *, _IO_off_t, int);
229 int ret;
231 seek = (int (*)(_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;
232 if (seek == NULL)
233 return _IO_pos_BAD;
235 ret = seek (cfile->__cookie, offset, dir);
237 return (ret == -1) ? _IO_pos_BAD : ret;
240 static const struct _IO_jump_t _IO_old_cookie_jumps = {
241 JUMP_INIT_DUMMY,
242 JUMP_INIT(finish, INTUSE(_IO_file_finish)),
243 JUMP_INIT(overflow, INTUSE(_IO_file_overflow)),
244 JUMP_INIT(underflow, INTUSE(_IO_file_underflow)),
245 JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
246 JUMP_INIT(pbackfail, INTUSE(_IO_default_pbackfail)),
247 JUMP_INIT(xsputn, INTUSE(_IO_file_xsputn)),
248 JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
249 JUMP_INIT(seekoff, _IO_cookie_seekoff),
250 JUMP_INIT(seekpos, _IO_default_seekpos),
251 JUMP_INIT(setbuf, INTUSE(_IO_file_setbuf)),
252 JUMP_INIT(sync, INTUSE(_IO_file_sync)),
253 JUMP_INIT(doallocate, INTUSE(_IO_file_doallocate)),
254 JUMP_INIT(read, _IO_cookie_read),
255 JUMP_INIT(write, _IO_cookie_write),
256 JUMP_INIT(seek, _IO_old_cookie_seek),
257 JUMP_INIT(close, _IO_cookie_close),
258 JUMP_INIT(stat, _IO_default_stat),
259 JUMP_INIT(showmanyc, _IO_default_showmanyc),
260 JUMP_INIT(imbue, _IO_default_imbue),
263 _IO_FILE *
264 attribute_compat_text_section
265 _IO_old_fopencookie (cookie, mode, io_functions)
266 void *cookie;
267 const char *mode;
268 _IO_cookie_io_functions_t io_functions;
270 _IO_FILE *ret;
272 ret = _IO_fopencookie (cookie, mode, io_functions);
273 if (ret != NULL)
274 _IO_JUMPS ((struct _IO_FILE_plus *) ret) = &_IO_old_cookie_jumps;
276 return ret;
279 compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0);
281 #endif