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