1 /* Copyright (C) 1993,95,97,99,2000,2002,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
31 #include <shlib-compat.h>
33 /* Prototyped for local functions. */
34 static _IO_ssize_t
_IO_cookie_read (register _IO_FILE
* fp
, void* buf
,
36 static _IO_ssize_t
_IO_cookie_write (register _IO_FILE
* fp
,
37 const void* buf
, _IO_ssize_t size
);
38 static _IO_off64_t
_IO_cookie_seek (_IO_FILE
*fp
, _IO_off64_t offset
, int dir
);
39 static _IO_off64_t
_IO_cookie_seekoff (_IO_FILE
*fp
, _IO_off64_t offset
,
41 static int _IO_cookie_close (_IO_FILE
* fp
);
44 _IO_cookie_read (fp
, buf
, size
)
49 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
51 if (cfile
->__io_functions
.read
== NULL
)
54 return cfile
->__io_functions
.read (cfile
->__cookie
, buf
, size
);
58 _IO_cookie_write (fp
, buf
, size
)
63 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
65 if (cfile
->__io_functions
.write
== NULL
)
68 return cfile
->__io_functions
.write (cfile
->__cookie
, buf
, size
);
72 _IO_cookie_seek (fp
, offset
, dir
)
77 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
79 return ((cfile
->__io_functions
.seek
== NULL
80 || (cfile
->__io_functions
.seek (cfile
->__cookie
, &offset
, dir
)
82 || offset
== (_IO_off64_t
) -1)
83 ? _IO_pos_BAD
: offset
);
90 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
92 if (cfile
->__io_functions
.close
== NULL
)
95 return cfile
->__io_functions
.close (cfile
->__cookie
);
100 _IO_cookie_seekoff (fp
, offset
, dir
, mode
)
106 /* We must force the fileops code to always use seek to determine
108 fp
->_offset
= _IO_pos_BAD
;
109 return INTUSE(_IO_file_seekoff
) (fp
, offset
, dir
, mode
);
113 static const struct _IO_jump_t _IO_cookie_jumps
= {
115 JUMP_INIT(finish
, INTUSE(_IO_file_finish
)),
116 JUMP_INIT(overflow
, INTUSE(_IO_file_overflow
)),
117 JUMP_INIT(underflow
, INTUSE(_IO_file_underflow
)),
118 JUMP_INIT(uflow
, INTUSE(_IO_default_uflow
)),
119 JUMP_INIT(pbackfail
, INTUSE(_IO_default_pbackfail
)),
120 JUMP_INIT(xsputn
, INTUSE(_IO_file_xsputn
)),
121 JUMP_INIT(xsgetn
, INTUSE(_IO_default_xsgetn
)),
122 JUMP_INIT(seekoff
, _IO_cookie_seekoff
),
123 JUMP_INIT(seekpos
, _IO_default_seekpos
),
124 JUMP_INIT(setbuf
, INTUSE(_IO_file_setbuf
)),
125 JUMP_INIT(sync
, INTUSE(_IO_file_sync
)),
126 JUMP_INIT(doallocate
, INTUSE(_IO_file_doallocate
)),
127 JUMP_INIT(read
, _IO_cookie_read
),
128 JUMP_INIT(write
, _IO_cookie_write
),
129 JUMP_INIT(seek
, _IO_cookie_seek
),
130 JUMP_INIT(close
, _IO_cookie_close
),
131 JUMP_INIT(stat
, _IO_default_stat
),
132 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
133 JUMP_INIT(imbue
, _IO_default_imbue
),
138 _IO_cookie_init (struct _IO_cookie_file
*cfile
, int read_write
,
139 void *cookie
, _IO_cookie_io_functions_t io_functions
)
141 INTUSE(_IO_init
) (&cfile
->__fp
.file
, 0);
142 _IO_JUMPS (&cfile
->__fp
) = &_IO_cookie_jumps
;
144 cfile
->__cookie
= cookie
;
145 cfile
->__io_functions
= io_functions
;
147 INTUSE(_IO_file_init
) (&cfile
->__fp
);
149 cfile
->__fp
.file
._IO_file_flags
=
150 _IO_mask_flags (&cfile
->__fp
.file
, read_write
,
151 _IO_NO_READS
+_IO_NO_WRITES
+_IO_IS_APPENDING
);
153 /* We use a negative number different from -1 for _fileno to mark that
154 this special stream is not associated with a real file, but still has
155 to be treated as such. */
156 cfile
->__fp
.file
._fileno
= -2;
161 _IO_fopencookie (cookie
, mode
, io_functions
)
164 _IO_cookie_io_functions_t io_functions
;
169 struct _IO_cookie_file cfile
;
178 read_write
= _IO_NO_WRITES
;
181 read_write
= _IO_NO_READS
;
184 read_write
= _IO_NO_READS
|_IO_IS_APPENDING
;
189 if (mode
[0] == '+' || (mode
[0] == 'b' && mode
[1] == '+'))
190 read_write
&= _IO_IS_APPENDING
;
192 new_f
= (struct locked_FILE
*) malloc (sizeof (struct locked_FILE
));
196 new_f
->cfile
.__fp
.file
._lock
= &new_f
->lock
;
199 _IO_cookie_init (&new_f
->cfile
, read_write
, cookie
, io_functions
);
201 return (_IO_FILE
*) &new_f
->cfile
.__fp
;
204 versioned_symbol (libc
, _IO_fopencookie
, fopencookie
, GLIBC_2_2
);
206 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
208 static _IO_off64_t
_IO_old_cookie_seek (_IO_FILE
*fp
, _IO_off64_t offset
,
210 _IO_FILE
* _IO_old_fopencookie (void *cookie
, const char *mode
,
211 _IO_cookie_io_functions_t io_functions
);
214 attribute_compat_text_section
215 _IO_old_cookie_seek (fp
, offset
, dir
)
220 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
221 int (*seek
) (_IO_FILE
*, _IO_off_t
, int);
224 seek
= (int (*)(_IO_FILE
*, _IO_off_t
, int)) cfile
->__io_functions
.seek
;
228 ret
= seek (cfile
->__cookie
, offset
, dir
);
230 return (ret
== -1) ? _IO_pos_BAD
: ret
;
233 static const struct _IO_jump_t _IO_old_cookie_jumps
= {
235 JUMP_INIT(finish
, INTUSE(_IO_file_finish
)),
236 JUMP_INIT(overflow
, INTUSE(_IO_file_overflow
)),
237 JUMP_INIT(underflow
, INTUSE(_IO_file_underflow
)),
238 JUMP_INIT(uflow
, INTUSE(_IO_default_uflow
)),
239 JUMP_INIT(pbackfail
, INTUSE(_IO_default_pbackfail
)),
240 JUMP_INIT(xsputn
, INTUSE(_IO_file_xsputn
)),
241 JUMP_INIT(xsgetn
, INTUSE(_IO_default_xsgetn
)),
242 JUMP_INIT(seekoff
, _IO_cookie_seekoff
),
243 JUMP_INIT(seekpos
, _IO_default_seekpos
),
244 JUMP_INIT(setbuf
, INTUSE(_IO_file_setbuf
)),
245 JUMP_INIT(sync
, INTUSE(_IO_file_sync
)),
246 JUMP_INIT(doallocate
, INTUSE(_IO_file_doallocate
)),
247 JUMP_INIT(read
, _IO_cookie_read
),
248 JUMP_INIT(write
, _IO_cookie_write
),
249 JUMP_INIT(seek
, _IO_old_cookie_seek
),
250 JUMP_INIT(close
, _IO_cookie_close
),
251 JUMP_INIT(stat
, _IO_default_stat
),
252 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
253 JUMP_INIT(imbue
, _IO_default_imbue
),
257 attribute_compat_text_section
258 _IO_old_fopencookie (cookie
, mode
, io_functions
)
261 _IO_cookie_io_functions_t io_functions
;
265 ret
= _IO_fopencookie (cookie
, mode
, io_functions
);
267 _IO_JUMPS ((struct _IO_FILE_plus
*) ret
) = &_IO_old_cookie_jumps
;
272 compat_symbol (libc
, _IO_old_fopencookie
, fopencookie
, GLIBC_2_0
);