Update.
[glibc.git] / libio / iofopncook.c
blob73346e31dbe52713481788f52b520e57b61271b1
1 /* Copyright (C) 1993,95,97,99,2000 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
17 02111-1307 USA.
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. */
28 #include <libioP.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <shlib-compat.h>
33 /* Prototyped for local functions. */
34 static _IO_ssize_t _IO_cookie_read (register _IO_FILE* fp, void* buf,
35 _IO_ssize_t size);
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 int _IO_cookie_close (_IO_FILE* fp);
40 _IO_FILE * _IO_fopencookie (void *cookie, const char *mode,
41 _IO_cookie_io_functions_t io_functions);
43 static _IO_ssize_t
44 _IO_cookie_read (fp, buf, size)
45 _IO_FILE *fp;
46 void *buf;
47 _IO_ssize_t size;
49 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
51 if (cfile->__io_functions.read == NULL)
52 return -1;
54 return cfile->__io_functions.read (cfile->__cookie, buf, size);
57 static _IO_ssize_t
58 _IO_cookie_write (fp, buf, size)
59 _IO_FILE *fp;
60 const void *buf;
61 _IO_ssize_t size;
63 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
65 if (cfile->__io_functions.write == NULL)
66 return -1;
68 return cfile->__io_functions.write (cfile->__cookie, buf, size);
71 static _IO_off64_t
72 _IO_cookie_seek (fp, offset, dir)
73 _IO_FILE *fp;
74 _IO_off64_t offset;
75 int 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)
81 == -1)
82 || offset == (_IO_off64_t) -1)
83 ? _IO_pos_BAD : offset);
86 static int
87 _IO_cookie_close (fp)
88 _IO_FILE *fp;
90 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
92 if (cfile->__io_functions.close == NULL)
93 return 0;
95 return cfile->__io_functions.close (cfile->__cookie);
99 static struct _IO_jump_t _IO_cookie_jumps = {
100 JUMP_INIT_DUMMY,
101 JUMP_INIT(finish, _IO_file_finish),
102 JUMP_INIT(overflow, _IO_file_overflow),
103 JUMP_INIT(underflow, _IO_file_underflow),
104 JUMP_INIT(uflow, _IO_default_uflow),
105 JUMP_INIT(pbackfail, _IO_default_pbackfail),
106 JUMP_INIT(xsputn, _IO_file_xsputn),
107 JUMP_INIT(xsgetn, _IO_default_xsgetn),
108 JUMP_INIT(seekoff, _IO_file_seekoff),
109 JUMP_INIT(seekpos, _IO_default_seekpos),
110 JUMP_INIT(setbuf, _IO_file_setbuf),
111 JUMP_INIT(sync, _IO_file_sync),
112 JUMP_INIT(doallocate, _IO_file_doallocate),
113 JUMP_INIT(read, _IO_cookie_read),
114 JUMP_INIT(write, _IO_cookie_write),
115 JUMP_INIT(seek, _IO_cookie_seek),
116 JUMP_INIT(close, _IO_cookie_close),
117 JUMP_INIT(stat, _IO_default_stat),
118 JUMP_INIT(showmanyc, _IO_default_showmanyc),
119 JUMP_INIT(imbue, _IO_default_imbue),
123 void
124 _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
125 void *cookie, _IO_cookie_io_functions_t io_functions)
127 _IO_init (&cfile->__fp.file, 0);
128 _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
130 cfile->__cookie = cookie;
131 cfile->__io_functions = io_functions;
133 _IO_file_init(&cfile->__fp);
135 cfile->__fp.file._IO_file_flags =
136 _IO_mask_flags (&cfile->__fp.file, read_write,
137 _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
139 /* We use a negative number different from -1 for _fileno to mark that
140 this special stream is not associated with a real file, but still has
141 to be treated as such. */
142 cfile->__fp.file._fileno = -2;
146 _IO_FILE *
147 _IO_fopencookie (cookie, mode, io_functions)
148 void *cookie;
149 const char *mode;
150 _IO_cookie_io_functions_t io_functions;
152 int read_write;
153 struct locked_FILE
155 struct _IO_cookie_file cfile;
156 #ifdef _IO_MTSAFE_IO
157 _IO_lock_t lock;
158 #endif
159 } *new_f;
161 switch (*mode++)
163 case 'r':
164 read_write = _IO_NO_WRITES;
165 break;
166 case 'w':
167 read_write = _IO_NO_READS;
168 break;
169 case 'a':
170 read_write = _IO_NO_READS|_IO_IS_APPENDING;
171 break;
172 default:
173 return NULL;
175 if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
176 read_write &= _IO_IS_APPENDING;
178 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
179 if (new_f == NULL)
180 return NULL;
181 #ifdef _IO_MTSAFE_IO
182 new_f->cfile.__fp.file._lock = &new_f->lock;
183 #endif
185 _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions);
187 return (_IO_FILE *) &new_f->cfile.__fp;
190 versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2);
192 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
194 static _IO_off64_t _IO_old_cookie_seek (_IO_FILE *fp, _IO_off64_t offset,
195 int dir);
196 _IO_FILE * _IO_old_fopencookie (void *cookie, const char *mode,
197 _IO_cookie_io_functions_t io_functions);
199 static _IO_off64_t
200 _IO_old_cookie_seek (fp, offset, dir)
201 _IO_FILE *fp;
202 _IO_off64_t offset;
203 int dir;
205 struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
206 int (*seek) (_IO_FILE *, _IO_off_t, int);
207 int ret;
209 seek = (int (*)(_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;
210 if (seek == NULL)
211 return _IO_pos_BAD;
213 ret = seek (cfile->__cookie, offset, dir);
215 return (ret == -1) ? _IO_pos_BAD : ret;
218 static struct _IO_jump_t _IO_old_cookie_jumps = {
219 JUMP_INIT_DUMMY,
220 JUMP_INIT(finish, _IO_file_finish),
221 JUMP_INIT(overflow, _IO_file_overflow),
222 JUMP_INIT(underflow, _IO_file_underflow),
223 JUMP_INIT(uflow, _IO_default_uflow),
224 JUMP_INIT(pbackfail, _IO_default_pbackfail),
225 JUMP_INIT(xsputn, _IO_file_xsputn),
226 JUMP_INIT(xsgetn, _IO_default_xsgetn),
227 JUMP_INIT(seekoff, _IO_file_seekoff),
228 JUMP_INIT(seekpos, _IO_default_seekpos),
229 JUMP_INIT(setbuf, _IO_file_setbuf),
230 JUMP_INIT(sync, _IO_file_sync),
231 JUMP_INIT(doallocate, _IO_file_doallocate),
232 JUMP_INIT(read, _IO_cookie_read),
233 JUMP_INIT(write, _IO_cookie_write),
234 JUMP_INIT(seek, _IO_old_cookie_seek),
235 JUMP_INIT(close, _IO_cookie_close),
236 JUMP_INIT(stat, _IO_default_stat),
237 JUMP_INIT(showmanyc, _IO_default_showmanyc),
238 JUMP_INIT(imbue, _IO_default_imbue),
241 _IO_FILE *
242 _IO_old_fopencookie (cookie, mode, io_functions)
243 void *cookie;
244 const char *mode;
245 _IO_cookie_io_functions_t io_functions;
247 _IO_FILE *ret;
249 ret = _IO_fopencookie (cookie, mode, io_functions);
250 if (ret != NULL)
251 _IO_JUMPS ((struct _IO_FILE_plus *) ret) = &_IO_old_cookie_jumps;
253 return ret;
256 compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0);
258 #endif