1 /* Copyright (C) 1993,95,97,99,2000 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does
21 not cause the resulting executable to be covered by the GNU General
22 Public License. This exception does not however invalidate any
23 other reasons why the executable file might be covered by the GNU
24 General Public License. */
29 #include <shlib-compat.h>
31 /* Prototyped for local functions. */
32 static _IO_ssize_t
_IO_cookie_read (register _IO_FILE
* fp
, void* buf
,
34 static _IO_ssize_t
_IO_cookie_write (register _IO_FILE
* fp
,
35 const void* buf
, _IO_ssize_t size
);
36 static _IO_off64_t
_IO_cookie_seek (_IO_FILE
*fp
, _IO_off64_t offset
, int dir
);
37 static int _IO_cookie_close (_IO_FILE
* fp
);
38 _IO_FILE
* _IO_fopencookie (void *cookie
, const char *mode
,
39 _IO_cookie_io_functions_t io_functions
);
42 _IO_cookie_read (fp
, buf
, size
)
47 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
49 if (cfile
->__io_functions
.read
== NULL
)
52 return cfile
->__io_functions
.read (cfile
->__cookie
, buf
, size
);
56 _IO_cookie_write (fp
, buf
, size
)
61 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
63 if (cfile
->__io_functions
.write
== NULL
)
66 return cfile
->__io_functions
.write (cfile
->__cookie
, buf
, size
);
70 _IO_cookie_seek (fp
, offset
, dir
)
75 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
77 return ((cfile
->__io_functions
.seek
== NULL
78 || (cfile
->__io_functions
.seek (cfile
->__cookie
, &offset
, dir
)
80 || offset
== (_IO_off64_t
) -1)
81 ? _IO_pos_BAD
: offset
);
88 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
90 if (cfile
->__io_functions
.close
== NULL
)
93 return cfile
->__io_functions
.close (cfile
->__cookie
);
97 static struct _IO_jump_t _IO_cookie_jumps
= {
99 JUMP_INIT(finish
, _IO_file_finish
),
100 JUMP_INIT(overflow
, _IO_file_overflow
),
101 JUMP_INIT(underflow
, _IO_file_underflow
),
102 JUMP_INIT(uflow
, _IO_default_uflow
),
103 JUMP_INIT(pbackfail
, _IO_default_pbackfail
),
104 JUMP_INIT(xsputn
, _IO_file_xsputn
),
105 JUMP_INIT(xsgetn
, _IO_default_xsgetn
),
106 JUMP_INIT(seekoff
, _IO_file_seekoff
),
107 JUMP_INIT(seekpos
, _IO_default_seekpos
),
108 JUMP_INIT(setbuf
, _IO_file_setbuf
),
109 JUMP_INIT(sync
, _IO_file_sync
),
110 JUMP_INIT(doallocate
, _IO_file_doallocate
),
111 JUMP_INIT(read
, _IO_cookie_read
),
112 JUMP_INIT(write
, _IO_cookie_write
),
113 JUMP_INIT(seek
, _IO_cookie_seek
),
114 JUMP_INIT(close
, _IO_cookie_close
),
115 JUMP_INIT(stat
, _IO_default_stat
),
116 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
117 JUMP_INIT(imbue
, _IO_default_imbue
),
122 _IO_cookie_init (struct _IO_cookie_file
*cfile
, int read_write
,
123 void *cookie
, _IO_cookie_io_functions_t io_functions
)
125 _IO_init (&cfile
->__fp
.file
, 0);
126 _IO_JUMPS (&cfile
->__fp
) = &_IO_cookie_jumps
;
128 cfile
->__cookie
= cookie
;
129 cfile
->__io_functions
= io_functions
;
131 _IO_file_init(&cfile
->__fp
);
133 cfile
->__fp
.file
._IO_file_flags
=
134 _IO_mask_flags (&cfile
->__fp
.file
, read_write
,
135 _IO_NO_READS
+_IO_NO_WRITES
+_IO_IS_APPENDING
);
137 /* We use a negative number different from -1 for _fileno to mark that
138 this special stream is not associated with a real file, but still has
139 to be treated as such. */
140 cfile
->__fp
.file
._fileno
= -2;
145 _IO_fopencookie (cookie
, mode
, io_functions
)
148 _IO_cookie_io_functions_t io_functions
;
153 struct _IO_cookie_file cfile
;
162 read_write
= _IO_NO_WRITES
;
165 read_write
= _IO_NO_READS
;
168 read_write
= _IO_NO_READS
|_IO_IS_APPENDING
;
173 if (mode
[0] == '+' || (mode
[0] == 'b' && mode
[1] == '+'))
174 read_write
&= _IO_IS_APPENDING
;
176 new_f
= (struct locked_FILE
*) malloc (sizeof (struct locked_FILE
));
180 new_f
->cfile
.__fp
.file
._lock
= &new_f
->lock
;
183 _IO_cookie_init (&new_f
->cfile
, read_write
, cookie
, io_functions
);
185 return (_IO_FILE
*) &new_f
->cfile
.__fp
;
188 versioned_symbol (libc
, _IO_fopencookie
, fopencookie
, GLIBC_2_2
);
190 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
192 static _IO_off64_t
_IO_old_cookie_seek (_IO_FILE
*fp
, _IO_off64_t offset
,
194 _IO_FILE
* _IO_old_fopencookie (void *cookie
, const char *mode
,
195 _IO_cookie_io_functions_t io_functions
);
198 _IO_old_cookie_seek (fp
, offset
, dir
)
203 struct _IO_cookie_file
*cfile
= (struct _IO_cookie_file
*) fp
;
204 int (*seek
) (_IO_FILE
*, _IO_off_t
, int);
207 seek
= (int (*)(_IO_FILE
*, _IO_off_t
, int)) cfile
->__io_functions
.seek
;
211 ret
= seek (cfile
->__cookie
, offset
, dir
);
213 return (ret
== -1) ? _IO_pos_BAD
: ret
;
216 static struct _IO_jump_t _IO_old_cookie_jumps
= {
218 JUMP_INIT(finish
, _IO_file_finish
),
219 JUMP_INIT(overflow
, _IO_file_overflow
),
220 JUMP_INIT(underflow
, _IO_file_underflow
),
221 JUMP_INIT(uflow
, _IO_default_uflow
),
222 JUMP_INIT(pbackfail
, _IO_default_pbackfail
),
223 JUMP_INIT(xsputn
, _IO_file_xsputn
),
224 JUMP_INIT(xsgetn
, _IO_default_xsgetn
),
225 JUMP_INIT(seekoff
, _IO_file_seekoff
),
226 JUMP_INIT(seekpos
, _IO_default_seekpos
),
227 JUMP_INIT(setbuf
, _IO_file_setbuf
),
228 JUMP_INIT(sync
, _IO_file_sync
),
229 JUMP_INIT(doallocate
, _IO_file_doallocate
),
230 JUMP_INIT(read
, _IO_cookie_read
),
231 JUMP_INIT(write
, _IO_cookie_write
),
232 JUMP_INIT(seek
, _IO_old_cookie_seek
),
233 JUMP_INIT(close
, _IO_cookie_close
),
234 JUMP_INIT(stat
, _IO_default_stat
),
235 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
236 JUMP_INIT(imbue
, _IO_default_imbue
),
240 _IO_old_fopencookie (cookie
, mode
, io_functions
)
243 _IO_cookie_io_functions_t io_functions
;
247 ret
= _IO_fopencookie (cookie
, mode
, io_functions
);
249 _IO_JUMPS ((struct _IO_FILE_plus
*) ret
) = &_IO_old_cookie_jumps
;
254 compat_symbol (libc
, _IO_old_fopencookie
, fopencookie
, GLIBC_2_0
);