* hurd/sigunwind.c (_hurdsig_longjmp_from_handler): Fix a typo
[platform/upstream/glibc.git] / libio / iofopncook.c
1 /* Copyright (C) 1993,95,97,99,2000,2002,2004, 2005
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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.
19
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.  */
28
29 #include <libioP.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <shlib-compat.h>
33
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);
43
44 static _IO_ssize_t
45 _IO_cookie_read (fp, buf, size)
46      _IO_FILE *fp;
47      void *buf;
48      _IO_ssize_t size;
49 {
50   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
51
52   if (cfile->__io_functions.read == NULL)
53     return -1;
54
55   return cfile->__io_functions.read (cfile->__cookie, buf, size);
56 }
57
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;
63 {
64   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
65
66   if (cfile->__io_functions.write == NULL)
67     return -1;
68
69   return cfile->__io_functions.write (cfile->__cookie, buf, size);
70 }
71
72 static _IO_off64_t
73 _IO_cookie_seek (fp, offset, dir)
74      _IO_FILE *fp;
75      _IO_off64_t offset;
76      int dir;
77 {
78   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
79
80   return ((cfile->__io_functions.seek == NULL
81            || (cfile->__io_functions.seek (cfile->__cookie, &offset, dir)
82                == -1)
83            || offset == (_IO_off64_t) -1)
84           ? _IO_pos_BAD : offset);
85 }
86
87 static int
88 _IO_cookie_close (fp)
89      _IO_FILE *fp;
90 {
91   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
92
93   if (cfile->__io_functions.close == NULL)
94     return 0;
95
96   return cfile->__io_functions.close (cfile->__cookie);
97 }
98
99
100 static _IO_off64_t
101 _IO_cookie_seekoff (fp, offset, dir, mode)
102      _IO_FILE *fp;
103      _IO_off64_t offset;
104      int dir;
105      int mode;
106 {
107   /* We must force the fileops code to always use seek to determine
108      the position.  */
109   fp->_offset = _IO_pos_BAD;
110   return INTUSE(_IO_file_seekoff) (fp, offset, dir, mode);
111 }
112
113
114 static const struct _IO_jump_t _IO_cookie_jumps = {
115   JUMP_INIT_DUMMY,
116   JUMP_INIT(finish, INTUSE(_IO_file_finish)),
117   JUMP_INIT(overflow, INTUSE(_IO_file_overflow)),
118   JUMP_INIT(underflow, INTUSE(_IO_file_underflow)),
119   JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
120   JUMP_INIT(pbackfail, INTUSE(_IO_default_pbackfail)),
121   JUMP_INIT(xsputn, INTUSE(_IO_file_xsputn)),
122   JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
123   JUMP_INIT(seekoff, _IO_cookie_seekoff),
124   JUMP_INIT(seekpos, _IO_default_seekpos),
125   JUMP_INIT(setbuf, INTUSE(_IO_file_setbuf)),
126   JUMP_INIT(sync, INTUSE(_IO_file_sync)),
127   JUMP_INIT(doallocate, INTUSE(_IO_file_doallocate)),
128   JUMP_INIT(read, _IO_cookie_read),
129   JUMP_INIT(write, _IO_cookie_write),
130   JUMP_INIT(seek, _IO_cookie_seek),
131   JUMP_INIT(close, _IO_cookie_close),
132   JUMP_INIT(stat, _IO_default_stat),
133   JUMP_INIT(showmanyc, _IO_default_showmanyc),
134   JUMP_INIT(imbue, _IO_default_imbue),
135 };
136
137
138 void
139 _IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
140                  void *cookie, _IO_cookie_io_functions_t io_functions)
141 {
142   INTUSE(_IO_init) (&cfile->__fp.file, 0);
143   _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
144
145   cfile->__cookie = cookie;
146   cfile->__io_functions = io_functions;
147
148   INTUSE(_IO_file_init) (&cfile->__fp);
149
150   cfile->__fp.file._IO_file_flags =
151     _IO_mask_flags (&cfile->__fp.file, read_write,
152                     _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
153
154   /* We use a negative number different from -1 for _fileno to mark that
155      this special stream is not associated with a real file, but still has
156      to be treated as such.  */
157   cfile->__fp.file._fileno = -2;
158 }
159
160
161 _IO_FILE *
162 _IO_fopencookie (cookie, mode, io_functions)
163      void *cookie;
164      const char *mode;
165      _IO_cookie_io_functions_t io_functions;
166 {
167   int read_write;
168   struct locked_FILE
169   {
170     struct _IO_cookie_file cfile;
171 #ifdef _IO_MTSAFE_IO
172     _IO_lock_t lock;
173 #endif
174   } *new_f;
175
176   switch (*mode++)
177     {
178     case 'r':
179       read_write = _IO_NO_WRITES;
180       break;
181     case 'w':
182       read_write = _IO_NO_READS;
183       break;
184     case 'a':
185       read_write = _IO_NO_READS|_IO_IS_APPENDING;
186       break;
187     default:
188       return NULL;
189   }
190   if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
191     read_write &= _IO_IS_APPENDING;
192
193   new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
194   if (new_f == NULL)
195     return NULL;
196 #ifdef _IO_MTSAFE_IO
197   new_f->cfile.__fp.file._lock = &new_f->lock;
198 #endif
199
200   _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions);
201
202   return (_IO_FILE *) &new_f->cfile.__fp;
203 }
204
205 versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2);
206
207 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
208
209 static _IO_off64_t _IO_old_cookie_seek (_IO_FILE *fp, _IO_off64_t offset,
210                                         int dir);
211 _IO_FILE * _IO_old_fopencookie (void *cookie, const char *mode,
212                                 _IO_cookie_io_functions_t io_functions);
213
214 static _IO_off64_t
215 attribute_compat_text_section
216 _IO_old_cookie_seek (fp, offset, dir)
217      _IO_FILE *fp;
218      _IO_off64_t offset;
219      int dir;
220 {
221   struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
222   int (*seek) (_IO_FILE *, _IO_off_t, int);
223   int ret;
224
225   seek = (int (*)(_IO_FILE *, _IO_off_t, int)) cfile->__io_functions.seek;
226   if (seek == NULL)
227     return _IO_pos_BAD;
228
229   ret = seek (cfile->__cookie, offset, dir);
230
231   return (ret == -1) ? _IO_pos_BAD : ret;
232 }
233
234 static const struct _IO_jump_t _IO_old_cookie_jumps = {
235   JUMP_INIT_DUMMY,
236   JUMP_INIT(finish, INTUSE(_IO_file_finish)),
237   JUMP_INIT(overflow, INTUSE(_IO_file_overflow)),
238   JUMP_INIT(underflow, INTUSE(_IO_file_underflow)),
239   JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
240   JUMP_INIT(pbackfail, INTUSE(_IO_default_pbackfail)),
241   JUMP_INIT(xsputn, INTUSE(_IO_file_xsputn)),
242   JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
243   JUMP_INIT(seekoff, _IO_cookie_seekoff),
244   JUMP_INIT(seekpos, _IO_default_seekpos),
245   JUMP_INIT(setbuf, INTUSE(_IO_file_setbuf)),
246   JUMP_INIT(sync, INTUSE(_IO_file_sync)),
247   JUMP_INIT(doallocate, INTUSE(_IO_file_doallocate)),
248   JUMP_INIT(read, _IO_cookie_read),
249   JUMP_INIT(write, _IO_cookie_write),
250   JUMP_INIT(seek, _IO_old_cookie_seek),
251   JUMP_INIT(close, _IO_cookie_close),
252   JUMP_INIT(stat, _IO_default_stat),
253   JUMP_INIT(showmanyc, _IO_default_showmanyc),
254   JUMP_INIT(imbue, _IO_default_imbue),
255 };
256
257 _IO_FILE *
258 attribute_compat_text_section
259 _IO_old_fopencookie (cookie, mode, io_functions)
260      void *cookie;
261      const char *mode;
262      _IO_cookie_io_functions_t io_functions;
263 {
264   _IO_FILE *ret;
265
266   ret = _IO_fopencookie (cookie, mode, io_functions);
267   if (ret != NULL)
268     _IO_JUMPS ((struct _IO_FILE_plus *) ret) = &_IO_old_cookie_jumps;
269
270   return ret;
271 }
272
273 compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0);
274
275 #endif