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