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