| 1 | /* Copyright (C) 1993-2020 Free Software Foundation, Inc. | 
|---|
| 2 | This file is part of the GNU C Library. | 
|---|
| 3 | Written by Per Bothner <bothner@cygnus.com>. | 
|---|
| 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 | <https://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 | /* This is a compatibility file.  If we don't build the libc with | 
|---|
| 29 | versioning don't compile this file.  */ | 
|---|
| 30 | #include <shlib-compat.h> | 
|---|
| 31 | #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1) | 
|---|
| 32 |  | 
|---|
| 33 | #define _IO_USE_OLD_IO_FILE | 
|---|
| 34 | #include "libioP.h" | 
|---|
| 35 | #include <fcntl.h> | 
|---|
| 36 | #include <sys/types.h> | 
|---|
| 37 | #include <sys/stat.h> | 
|---|
| 38 | #include <string.h> | 
|---|
| 39 | #include <errno.h> | 
|---|
| 40 | #include <stdlib.h> | 
|---|
| 41 | #include <unistd.h> | 
|---|
| 42 |  | 
|---|
| 43 | /* An fstream can be in at most one of put mode, get mode, or putback mode. | 
|---|
| 44 | Putback mode is a variant of get mode. | 
|---|
| 45 |  | 
|---|
| 46 | In a filebuf, there is only one current position, instead of two | 
|---|
| 47 | separate get and put pointers.  In get mode, the current position | 
|---|
| 48 | is that of gptr(); in put mode that of pptr(). | 
|---|
| 49 |  | 
|---|
| 50 | The position in the buffer that corresponds to the position | 
|---|
| 51 | in external file system is normally _IO_read_end, except in putback | 
|---|
| 52 | mode, when it is _IO_save_end. | 
|---|
| 53 | If the field _fb._offset is >= 0, it gives the offset in | 
|---|
| 54 | the file as a whole corresponding to eGptr(). (?) | 
|---|
| 55 |  | 
|---|
| 56 | PUT MODE: | 
|---|
| 57 | If a filebuf is in put mode, then all of _IO_read_ptr, _IO_read_end, | 
|---|
| 58 | and _IO_read_base are equal to each other.  These are usually equal | 
|---|
| 59 | to _IO_buf_base, though not necessarily if we have switched from | 
|---|
| 60 | get mode to put mode.  (The reason is to maintain the invariant | 
|---|
| 61 | that _IO_read_end corresponds to the external file position.) | 
|---|
| 62 | _IO_write_base is non-NULL and usually equal to _IO_buf_base. | 
|---|
| 63 | We also have _IO_write_end == _IO_buf_end, but only in fully buffered mode. | 
|---|
| 64 | The un-flushed character are those between _IO_write_base and _IO_write_ptr. | 
|---|
| 65 |  | 
|---|
| 66 | GET MODE: | 
|---|
| 67 | If a filebuf is in get or putback mode, eback() != egptr(). | 
|---|
| 68 | In get mode, the unread characters are between gptr() and egptr(). | 
|---|
| 69 | The OS file position corresponds to that of egptr(). | 
|---|
| 70 |  | 
|---|
| 71 | PUTBACK MODE: | 
|---|
| 72 | Putback mode is used to remember "excess" characters that have | 
|---|
| 73 | been sputbackc'd in a separate putback buffer. | 
|---|
| 74 | In putback mode, the get buffer points to the special putback buffer. | 
|---|
| 75 | The unread characters are the characters between gptr() and egptr() | 
|---|
| 76 | in the putback buffer, as well as the area between save_gptr() | 
|---|
| 77 | and save_egptr(), which point into the original reserve buffer. | 
|---|
| 78 | (The pointers save_gptr() and save_egptr() are the values | 
|---|
| 79 | of gptr() and egptr() at the time putback mode was entered.) | 
|---|
| 80 | The OS position corresponds to that of save_egptr(). | 
|---|
| 81 |  | 
|---|
| 82 | LINE BUFFERED OUTPUT: | 
|---|
| 83 | During line buffered output, _IO_write_base==base() && epptr()==base(). | 
|---|
| 84 | However, ptr() may be anywhere between base() and ebuf(). | 
|---|
| 85 | This forces a call to filebuf::overflow(int C) on every put. | 
|---|
| 86 | If there is more space in the buffer, and C is not a '\n', | 
|---|
| 87 | then C is inserted, and pptr() incremented. | 
|---|
| 88 |  | 
|---|
| 89 | UNBUFFERED STREAMS: | 
|---|
| 90 | If a filebuf is unbuffered(), the _shortbuf[1] is used as the buffer. | 
|---|
| 91 | */ | 
|---|
| 92 |  | 
|---|
| 93 | #define CLOSED_FILEBUF_FLAGS \ | 
|---|
| 94 | (_IO_IS_FILEBUF+_IO_NO_READS+_IO_NO_WRITES+_IO_TIED_PUT_GET) | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | void | 
|---|
| 98 | attribute_compat_text_section | 
|---|
| 99 | _IO_old_file_init_internal (struct _IO_FILE_plus *fp) | 
|---|
| 100 | { | 
|---|
| 101 | /* POSIX.1 allows another file handle to be used to change the position | 
|---|
| 102 | of our file descriptor.  Hence we actually don't know the actual | 
|---|
| 103 | position before we do the first fseek (and until a following fflush). */ | 
|---|
| 104 | fp->file._old_offset = _IO_pos_BAD; | 
|---|
| 105 | fp->file._flags |= CLOSED_FILEBUF_FLAGS; | 
|---|
| 106 |  | 
|---|
| 107 | _IO_link_in (fp); | 
|---|
| 108 | fp->file._vtable_offset = ((int) sizeof (struct _IO_FILE) | 
|---|
| 109 | - (int) sizeof (struct _IO_FILE_complete)); | 
|---|
| 110 | fp->file._fileno = -1; | 
|---|
| 111 |  | 
|---|
| 112 | if (&_IO_stdin_used != NULL || !_IO_legacy_file ((FILE *) fp)) | 
|---|
| 113 | /* The object is dynamically allocated and large enough.  Initialize | 
|---|
| 114 | the _mode element as well.  */ | 
|---|
| 115 | ((struct _IO_FILE_complete *) fp)->_mode = -1; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | void | 
|---|
| 119 | attribute_compat_text_section | 
|---|
| 120 | _IO_old_file_init (struct _IO_FILE_plus *fp) | 
|---|
| 121 | { | 
|---|
| 122 | IO_set_accept_foreign_vtables (&_IO_vtable_check); | 
|---|
| 123 | _IO_old_file_init_internal (fp); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | int | 
|---|
| 127 | attribute_compat_text_section | 
|---|
| 128 | _IO_old_file_close_it (FILE *fp) | 
|---|
| 129 | { | 
|---|
| 130 | int write_status, close_status; | 
|---|
| 131 | if (!_IO_file_is_open (fp)) | 
|---|
| 132 | return EOF; | 
|---|
| 133 |  | 
|---|
| 134 | write_status = _IO_old_do_flush (fp); | 
|---|
| 135 |  | 
|---|
| 136 | _IO_unsave_markers (fp); | 
|---|
| 137 |  | 
|---|
| 138 | close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0 | 
|---|
| 139 | ? _IO_SYSCLOSE (fp) : 0); | 
|---|
| 140 |  | 
|---|
| 141 | /* Free buffer. */ | 
|---|
| 142 | _IO_setb (fp, NULL, NULL, 0); | 
|---|
| 143 | _IO_setg (fp, NULL, NULL, NULL); | 
|---|
| 144 | _IO_setp (fp, NULL, NULL); | 
|---|
| 145 |  | 
|---|
| 146 | _IO_un_link ((struct _IO_FILE_plus *) fp); | 
|---|
| 147 | fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS; | 
|---|
| 148 | fp->_fileno = -1; | 
|---|
| 149 | fp->_old_offset = _IO_pos_BAD; | 
|---|
| 150 |  | 
|---|
| 151 | return close_status ? close_status : write_status; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | void | 
|---|
| 155 | attribute_compat_text_section | 
|---|
| 156 | _IO_old_file_finish (FILE *fp, int dummy) | 
|---|
| 157 | { | 
|---|
| 158 | if (_IO_file_is_open (fp)) | 
|---|
| 159 | { | 
|---|
| 160 | _IO_old_do_flush (fp); | 
|---|
| 161 | if (!(fp->_flags & _IO_DELETE_DONT_CLOSE)) | 
|---|
| 162 | _IO_SYSCLOSE (fp); | 
|---|
| 163 | } | 
|---|
| 164 | _IO_default_finish (fp, 0); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | FILE * | 
|---|
| 168 | attribute_compat_text_section | 
|---|
| 169 | _IO_old_file_fopen (FILE *fp, const char *filename, const char *mode) | 
|---|
| 170 | { | 
|---|
| 171 | int oflags = 0, omode; | 
|---|
| 172 | int read_write, fdesc; | 
|---|
| 173 | int oprot = 0666; | 
|---|
| 174 | if (_IO_file_is_open (fp)) | 
|---|
| 175 | return 0; | 
|---|
| 176 | switch (*mode++) | 
|---|
| 177 | { | 
|---|
| 178 | case 'r': | 
|---|
| 179 | omode = O_RDONLY; | 
|---|
| 180 | read_write = _IO_NO_WRITES; | 
|---|
| 181 | break; | 
|---|
| 182 | case 'w': | 
|---|
| 183 | omode = O_WRONLY; | 
|---|
| 184 | oflags = O_CREAT|O_TRUNC; | 
|---|
| 185 | read_write = _IO_NO_READS; | 
|---|
| 186 | break; | 
|---|
| 187 | case 'a': | 
|---|
| 188 | omode = O_WRONLY; | 
|---|
| 189 | oflags = O_CREAT|O_APPEND; | 
|---|
| 190 | read_write = _IO_NO_READS|_IO_IS_APPENDING; | 
|---|
| 191 | break; | 
|---|
| 192 | default: | 
|---|
| 193 | __set_errno (EINVAL); | 
|---|
| 194 | return NULL; | 
|---|
| 195 | } | 
|---|
| 196 | if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) | 
|---|
| 197 | { | 
|---|
| 198 | omode = O_RDWR; | 
|---|
| 199 | read_write &= _IO_IS_APPENDING; | 
|---|
| 200 | } | 
|---|
| 201 | fdesc = __open (filename, omode|oflags, oprot); | 
|---|
| 202 | if (fdesc < 0) | 
|---|
| 203 | return NULL; | 
|---|
| 204 | fp->_fileno = fdesc; | 
|---|
| 205 | _IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); | 
|---|
| 206 | if (read_write & _IO_IS_APPENDING) | 
|---|
| 207 | if (_IO_SEEKOFF (fp, (off_t)0, _IO_seek_end, _IOS_INPUT|_IOS_OUTPUT) | 
|---|
| 208 | == _IO_pos_BAD && errno != ESPIPE) | 
|---|
| 209 | return NULL; | 
|---|
| 210 | _IO_link_in ((struct _IO_FILE_plus *) fp); | 
|---|
| 211 | return fp; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | FILE * | 
|---|
| 215 | attribute_compat_text_section | 
|---|
| 216 | _IO_old_file_attach (FILE *fp, int fd) | 
|---|
| 217 | { | 
|---|
| 218 | if (_IO_file_is_open (fp)) | 
|---|
| 219 | return NULL; | 
|---|
| 220 | fp->_fileno = fd; | 
|---|
| 221 | fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES); | 
|---|
| 222 | fp->_flags |= _IO_DELETE_DONT_CLOSE; | 
|---|
| 223 | /* Get the current position of the file. */ | 
|---|
| 224 | /* We have to do that since that may be junk. */ | 
|---|
| 225 | fp->_old_offset = _IO_pos_BAD; | 
|---|
| 226 | if (_IO_SEEKOFF (fp, (off_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT) | 
|---|
| 227 | == _IO_pos_BAD && errno != ESPIPE) | 
|---|
| 228 | return NULL; | 
|---|
| 229 | return fp; | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | FILE * | 
|---|
| 233 | attribute_compat_text_section | 
|---|
| 234 | _IO_old_file_setbuf (FILE *fp, char *p, ssize_t len) | 
|---|
| 235 | { | 
|---|
| 236 | if (_IO_default_setbuf (fp, p, len) == NULL) | 
|---|
| 237 | return NULL; | 
|---|
| 238 |  | 
|---|
| 239 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end | 
|---|
| 240 | = fp->_IO_buf_base; | 
|---|
| 241 | _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 242 |  | 
|---|
| 243 | return fp; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | static int old_do_write (FILE *, const char *, size_t); | 
|---|
| 247 |  | 
|---|
| 248 | /* Write TO_DO bytes from DATA to FP. | 
|---|
| 249 | Then mark FP as having empty buffers. */ | 
|---|
| 250 |  | 
|---|
| 251 | int | 
|---|
| 252 | attribute_compat_text_section | 
|---|
| 253 | _IO_old_do_write (FILE *fp, const char *data, size_t to_do) | 
|---|
| 254 | { | 
|---|
| 255 | return (to_do == 0 || (size_t) old_do_write (fp, data, to_do) == to_do) | 
|---|
| 256 | ? 0 : EOF; | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | static int | 
|---|
| 260 | attribute_compat_text_section | 
|---|
| 261 | old_do_write (FILE *fp, const char *data, size_t to_do) | 
|---|
| 262 | { | 
|---|
| 263 | size_t count; | 
|---|
| 264 | if (fp->_flags & _IO_IS_APPENDING) | 
|---|
| 265 | /* On a system without a proper O_APPEND implementation, | 
|---|
| 266 | you would need to sys_seek(0, SEEK_END) here, but is | 
|---|
| 267 | not needed nor desirable for Unix- or Posix-like systems. | 
|---|
| 268 | Instead, just indicate that offset (before and after) is | 
|---|
| 269 | unpredictable. */ | 
|---|
| 270 | fp->_old_offset = _IO_pos_BAD; | 
|---|
| 271 | else if (fp->_IO_read_end != fp->_IO_write_base) | 
|---|
| 272 | { | 
|---|
| 273 | off_t new_pos | 
|---|
| 274 | = _IO_SYSSEEK (fp, fp->_IO_write_base - fp->_IO_read_end, 1); | 
|---|
| 275 | if (new_pos == _IO_pos_BAD) | 
|---|
| 276 | return 0; | 
|---|
| 277 | fp->_old_offset = new_pos; | 
|---|
| 278 | } | 
|---|
| 279 | count = _IO_SYSWRITE (fp, data, to_do); | 
|---|
| 280 | if (fp->_cur_column && count) | 
|---|
| 281 | fp->_cur_column = _IO_adjust_column (fp->_cur_column - 1, data, count) + 1; | 
|---|
| 282 | _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 283 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_buf_base; | 
|---|
| 284 | fp->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED)) | 
|---|
| 285 | ? fp->_IO_buf_base : fp->_IO_buf_end); | 
|---|
| 286 | return count; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | int | 
|---|
| 290 | attribute_compat_text_section | 
|---|
| 291 | _IO_old_file_underflow (FILE *fp) | 
|---|
| 292 | { | 
|---|
| 293 | ssize_t count; | 
|---|
| 294 |  | 
|---|
| 295 | /* C99 requires EOF to be "sticky".  */ | 
|---|
| 296 | if (fp->_flags & _IO_EOF_SEEN) | 
|---|
| 297 | return EOF; | 
|---|
| 298 |  | 
|---|
| 299 | if (fp->_flags & _IO_NO_READS) | 
|---|
| 300 | { | 
|---|
| 301 | fp->_flags |= _IO_ERR_SEEN; | 
|---|
| 302 | __set_errno (EBADF); | 
|---|
| 303 | return EOF; | 
|---|
| 304 | } | 
|---|
| 305 | if (fp->_IO_read_ptr < fp->_IO_read_end) | 
|---|
| 306 | return *(unsigned char *) fp->_IO_read_ptr; | 
|---|
| 307 |  | 
|---|
| 308 | if (fp->_IO_buf_base == NULL) | 
|---|
| 309 | { | 
|---|
| 310 | /* Maybe we already have a push back pointer.  */ | 
|---|
| 311 | if (fp->_IO_save_base != NULL) | 
|---|
| 312 | { | 
|---|
| 313 | free (fp->_IO_save_base); | 
|---|
| 314 | fp->_flags &= ~_IO_IN_BACKUP; | 
|---|
| 315 | } | 
|---|
| 316 | _IO_doallocbuf (fp); | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | /* Flush all line buffered files before reading. */ | 
|---|
| 320 | /* FIXME This can/should be moved to genops ?? */ | 
|---|
| 321 | if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED)) | 
|---|
| 322 | _IO_flush_all_linebuffered (); | 
|---|
| 323 |  | 
|---|
| 324 | _IO_switch_to_get_mode (fp); | 
|---|
| 325 |  | 
|---|
| 326 | /* This is very tricky. We have to adjust those | 
|---|
| 327 | pointers before we call _IO_SYSREAD () since | 
|---|
| 328 | we may longjump () out while waiting for | 
|---|
| 329 | input. Those pointers may be screwed up. H.J. */ | 
|---|
| 330 | fp->_IO_read_base = fp->_IO_read_ptr = fp->_IO_buf_base; | 
|---|
| 331 | fp->_IO_read_end = fp->_IO_buf_base; | 
|---|
| 332 | fp->_IO_write_base = fp->_IO_write_ptr = fp->_IO_write_end | 
|---|
| 333 | = fp->_IO_buf_base; | 
|---|
| 334 |  | 
|---|
| 335 | count = _IO_SYSREAD (fp, fp->_IO_buf_base, | 
|---|
| 336 | fp->_IO_buf_end - fp->_IO_buf_base); | 
|---|
| 337 | if (count <= 0) | 
|---|
| 338 | { | 
|---|
| 339 | if (count == 0) | 
|---|
| 340 | fp->_flags |= _IO_EOF_SEEN; | 
|---|
| 341 | else | 
|---|
| 342 | fp->_flags |= _IO_ERR_SEEN, count = 0; | 
|---|
| 343 | } | 
|---|
| 344 | fp->_IO_read_end += count; | 
|---|
| 345 | if (count == 0) | 
|---|
| 346 | return EOF; | 
|---|
| 347 | if (fp->_old_offset != _IO_pos_BAD) | 
|---|
| 348 | _IO_pos_adjust (fp->_old_offset, count); | 
|---|
| 349 | return *(unsigned char *) fp->_IO_read_ptr; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | int | 
|---|
| 353 | attribute_compat_text_section | 
|---|
| 354 | _IO_old_file_overflow (FILE *f, int ch) | 
|---|
| 355 | { | 
|---|
| 356 | if (f->_flags & _IO_NO_WRITES) /* SET ERROR */ | 
|---|
| 357 | { | 
|---|
| 358 | f->_flags |= _IO_ERR_SEEN; | 
|---|
| 359 | __set_errno (EBADF); | 
|---|
| 360 | return EOF; | 
|---|
| 361 | } | 
|---|
| 362 | /* If currently reading or no buffer allocated. */ | 
|---|
| 363 | if ((f->_flags & _IO_CURRENTLY_PUTTING) == 0) | 
|---|
| 364 | { | 
|---|
| 365 | /* Allocate a buffer if needed. */ | 
|---|
| 366 | if (f->_IO_write_base == 0) | 
|---|
| 367 | { | 
|---|
| 368 | _IO_doallocbuf (f); | 
|---|
| 369 | _IO_setg (f, f->_IO_buf_base, f->_IO_buf_base, f->_IO_buf_base); | 
|---|
| 370 | } | 
|---|
| 371 | /* Otherwise must be currently reading. | 
|---|
| 372 | If _IO_read_ptr (and hence also _IO_read_end) is at the buffer end, | 
|---|
| 373 | logically slide the buffer forwards one block (by setting the | 
|---|
| 374 | read pointers to all point at the beginning of the block).  This | 
|---|
| 375 | makes room for subsequent output. | 
|---|
| 376 | Otherwise, set the read pointers to _IO_read_end (leaving that | 
|---|
| 377 | alone, so it can continue to correspond to the external position). */ | 
|---|
| 378 | if (f->_IO_read_ptr == f->_IO_buf_end) | 
|---|
| 379 | f->_IO_read_end = f->_IO_read_ptr = f->_IO_buf_base; | 
|---|
| 380 | f->_IO_write_ptr = f->_IO_read_ptr; | 
|---|
| 381 | f->_IO_write_base = f->_IO_write_ptr; | 
|---|
| 382 | f->_IO_write_end = f->_IO_buf_end; | 
|---|
| 383 | f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end; | 
|---|
| 384 |  | 
|---|
| 385 | if (f->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED)) | 
|---|
| 386 | f->_IO_write_end = f->_IO_write_ptr; | 
|---|
| 387 | f->_flags |= _IO_CURRENTLY_PUTTING; | 
|---|
| 388 | } | 
|---|
| 389 | if (ch == EOF) | 
|---|
| 390 | return _IO_old_do_flush (f); | 
|---|
| 391 | if (f->_IO_write_ptr == f->_IO_buf_end ) /* Buffer is really full */ | 
|---|
| 392 | if (_IO_old_do_flush (f) == EOF) | 
|---|
| 393 | return EOF; | 
|---|
| 394 | *f->_IO_write_ptr++ = ch; | 
|---|
| 395 | if ((f->_flags & _IO_UNBUFFERED) | 
|---|
| 396 | || ((f->_flags & _IO_LINE_BUF) && ch == '\n')) | 
|---|
| 397 | if (_IO_old_do_flush (f) == EOF) | 
|---|
| 398 | return EOF; | 
|---|
| 399 | return (unsigned char) ch; | 
|---|
| 400 | } | 
|---|
| 401 |  | 
|---|
| 402 | int | 
|---|
| 403 | attribute_compat_text_section | 
|---|
| 404 | _IO_old_file_sync (FILE *fp) | 
|---|
| 405 | { | 
|---|
| 406 | ssize_t delta; | 
|---|
| 407 | int retval = 0; | 
|---|
| 408 |  | 
|---|
| 409 | /*    char* ptr = cur_ptr(); */ | 
|---|
| 410 | if (fp->_IO_write_ptr > fp->_IO_write_base) | 
|---|
| 411 | if (_IO_old_do_flush(fp)) return EOF; | 
|---|
| 412 | delta = fp->_IO_read_ptr - fp->_IO_read_end; | 
|---|
| 413 | if (delta != 0) | 
|---|
| 414 | { | 
|---|
| 415 | #ifdef TODO | 
|---|
| 416 | if (_IO_in_backup (fp)) | 
|---|
| 417 | delta -= eGptr () - Gbase (); | 
|---|
| 418 | #endif | 
|---|
| 419 | off_t new_pos = _IO_SYSSEEK (fp, delta, 1); | 
|---|
| 420 | if (new_pos != (off_t) EOF) | 
|---|
| 421 | fp->_IO_read_end = fp->_IO_read_ptr; | 
|---|
| 422 | else if (errno == ESPIPE) | 
|---|
| 423 | ; /* Ignore error from unseekable devices. */ | 
|---|
| 424 | else | 
|---|
| 425 | retval = EOF; | 
|---|
| 426 | } | 
|---|
| 427 | if (retval != EOF) | 
|---|
| 428 | fp->_old_offset = _IO_pos_BAD; | 
|---|
| 429 | /* FIXME: Cleanup - can this be shared? */ | 
|---|
| 430 | /*    setg(base(), ptr, ptr); */ | 
|---|
| 431 | return retval; | 
|---|
| 432 | } | 
|---|
| 433 |  | 
|---|
| 434 | off64_t | 
|---|
| 435 | attribute_compat_text_section | 
|---|
| 436 | _IO_old_file_seekoff (FILE *fp, off64_t offset, int dir, int mode) | 
|---|
| 437 | { | 
|---|
| 438 | off_t result; | 
|---|
| 439 | off64_t delta, new_offset; | 
|---|
| 440 | long count; | 
|---|
| 441 | /* POSIX.1 8.2.3.7 says that after a call the fflush() the file | 
|---|
| 442 | offset of the underlying file must be exact.  */ | 
|---|
| 443 | int must_be_exact = (fp->_IO_read_base == fp->_IO_read_end | 
|---|
| 444 | && fp->_IO_write_base == fp->_IO_write_ptr); | 
|---|
| 445 |  | 
|---|
| 446 | if (mode == 0) | 
|---|
| 447 | dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */ | 
|---|
| 448 |  | 
|---|
| 449 | /* Flush unwritten characters. | 
|---|
| 450 | (This may do an unneeded write if we seek within the buffer. | 
|---|
| 451 | But to be able to switch to reading, we would need to set | 
|---|
| 452 | egptr to pptr.  That can't be done in the current design, | 
|---|
| 453 | which assumes file_ptr() is eGptr.  Anyway, since we probably | 
|---|
| 454 | end up flushing when we close(), it doesn't make much difference.) | 
|---|
| 455 | FIXME: simulate mem-mapped files. */ | 
|---|
| 456 |  | 
|---|
| 457 | if (fp->_IO_write_ptr > fp->_IO_write_base || _IO_in_put_mode (fp)) | 
|---|
| 458 | if (_IO_switch_to_get_mode (fp)) | 
|---|
| 459 | return EOF; | 
|---|
| 460 |  | 
|---|
| 461 | if (fp->_IO_buf_base == NULL) | 
|---|
| 462 | { | 
|---|
| 463 | /* It could be that we already have a pushback buffer.  */ | 
|---|
| 464 | if (fp->_IO_read_base != NULL) | 
|---|
| 465 | { | 
|---|
| 466 | free (fp->_IO_read_base); | 
|---|
| 467 | fp->_flags &= ~_IO_IN_BACKUP; | 
|---|
| 468 | } | 
|---|
| 469 | _IO_doallocbuf (fp); | 
|---|
| 470 | _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 471 | _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | switch (dir) | 
|---|
| 475 | { | 
|---|
| 476 | case _IO_seek_cur: | 
|---|
| 477 | /* Adjust for read-ahead (bytes is buffer). */ | 
|---|
| 478 | offset -= fp->_IO_read_end - fp->_IO_read_ptr; | 
|---|
| 479 | if (fp->_old_offset == _IO_pos_BAD) | 
|---|
| 480 | goto dumb; | 
|---|
| 481 | /* Make offset absolute, assuming current pointer is file_ptr(). */ | 
|---|
| 482 | offset += fp->_old_offset; | 
|---|
| 483 |  | 
|---|
| 484 | dir = _IO_seek_set; | 
|---|
| 485 | break; | 
|---|
| 486 | case _IO_seek_set: | 
|---|
| 487 | break; | 
|---|
| 488 | case _IO_seek_end: | 
|---|
| 489 | { | 
|---|
| 490 | struct stat64 st; | 
|---|
| 491 | if (_IO_SYSSTAT (fp, &st) == 0 && S_ISREG (st.st_mode)) | 
|---|
| 492 | { | 
|---|
| 493 | offset += st.st_size; | 
|---|
| 494 | dir = _IO_seek_set; | 
|---|
| 495 | } | 
|---|
| 496 | else | 
|---|
| 497 | goto dumb; | 
|---|
| 498 | } | 
|---|
| 499 | } | 
|---|
| 500 | /* At this point, dir==_IO_seek_set. */ | 
|---|
| 501 |  | 
|---|
| 502 | /* If we are only interested in the current position we've found it now.  */ | 
|---|
| 503 | if (mode == 0) | 
|---|
| 504 | return offset; | 
|---|
| 505 |  | 
|---|
| 506 | /* If destination is within current buffer, optimize: */ | 
|---|
| 507 | if (fp->_old_offset != _IO_pos_BAD && fp->_IO_read_base != NULL | 
|---|
| 508 | && !_IO_in_backup (fp)) | 
|---|
| 509 | { | 
|---|
| 510 | /* Offset relative to start of main get area. */ | 
|---|
| 511 | off_t rel_offset = (offset - fp->_old_offset | 
|---|
| 512 | + (fp->_IO_read_end - fp->_IO_read_base)); | 
|---|
| 513 | if (rel_offset >= 0) | 
|---|
| 514 | { | 
|---|
| 515 | if (rel_offset <= fp->_IO_read_end - fp->_IO_read_base) | 
|---|
| 516 | { | 
|---|
| 517 | _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + rel_offset, | 
|---|
| 518 | fp->_IO_read_end); | 
|---|
| 519 | _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 520 | { | 
|---|
| 521 | _IO_mask_flags (fp, 0, _IO_EOF_SEEN); | 
|---|
| 522 | goto resync; | 
|---|
| 523 | } | 
|---|
| 524 | } | 
|---|
| 525 | #ifdef TODO | 
|---|
| 526 | /* If we have streammarkers, seek forward by reading ahead. */ | 
|---|
| 527 | if (_IO_have_markers (fp)) | 
|---|
| 528 | { | 
|---|
| 529 | int to_skip = rel_offset | 
|---|
| 530 | - (fp->_IO_read_ptr - fp->_IO_read_base); | 
|---|
| 531 | if (ignore (to_skip) != to_skip) | 
|---|
| 532 | goto dumb; | 
|---|
| 533 | _IO_mask_flags (fp, 0, _IO_EOF_SEEN); | 
|---|
| 534 | goto resync; | 
|---|
| 535 | } | 
|---|
| 536 | #endif | 
|---|
| 537 | } | 
|---|
| 538 | #ifdef TODO | 
|---|
| 539 | if (rel_offset < 0 && rel_offset >= Bbase () - Bptr ()) | 
|---|
| 540 | { | 
|---|
| 541 | if (!_IO_in_backup (fp)) | 
|---|
| 542 | _IO_switch_to_backup_area (fp); | 
|---|
| 543 | gbump (fp->_IO_read_end + rel_offset - fp->_IO_read_ptr); | 
|---|
| 544 | _IO_mask_flags (fp, 0, _IO_EOF_SEEN); | 
|---|
| 545 | goto resync; | 
|---|
| 546 | } | 
|---|
| 547 | #endif | 
|---|
| 548 | } | 
|---|
| 549 |  | 
|---|
| 550 | #ifdef TODO | 
|---|
| 551 | _IO_unsave_markers (fp); | 
|---|
| 552 | #endif | 
|---|
| 553 |  | 
|---|
| 554 | if (fp->_flags & _IO_NO_READS) | 
|---|
| 555 | goto dumb; | 
|---|
| 556 |  | 
|---|
| 557 | /* Try to seek to a block boundary, to improve kernel page management. */ | 
|---|
| 558 | new_offset = offset & ~(fp->_IO_buf_end - fp->_IO_buf_base - 1); | 
|---|
| 559 | delta = offset - new_offset; | 
|---|
| 560 | if (delta > fp->_IO_buf_end - fp->_IO_buf_base) | 
|---|
| 561 | { | 
|---|
| 562 | new_offset = offset; | 
|---|
| 563 | delta = 0; | 
|---|
| 564 | } | 
|---|
| 565 | result = _IO_SYSSEEK (fp, new_offset, 0); | 
|---|
| 566 | if (result < 0) | 
|---|
| 567 | return EOF; | 
|---|
| 568 | if (delta == 0) | 
|---|
| 569 | count = 0; | 
|---|
| 570 | else | 
|---|
| 571 | { | 
|---|
| 572 | count = _IO_SYSREAD (fp, fp->_IO_buf_base, | 
|---|
| 573 | (must_be_exact | 
|---|
| 574 | ? delta : fp->_IO_buf_end - fp->_IO_buf_base)); | 
|---|
| 575 | if (count < delta) | 
|---|
| 576 | { | 
|---|
| 577 | /* We weren't allowed to read, but try to seek the remainder. */ | 
|---|
| 578 | offset = count == EOF ? delta : delta-count; | 
|---|
| 579 | dir = _IO_seek_cur; | 
|---|
| 580 | goto dumb; | 
|---|
| 581 | } | 
|---|
| 582 | } | 
|---|
| 583 | _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base + delta, | 
|---|
| 584 | fp->_IO_buf_base + count); | 
|---|
| 585 | _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 586 | fp->_old_offset = result + count; | 
|---|
| 587 | _IO_mask_flags (fp, 0, _IO_EOF_SEEN); | 
|---|
| 588 | return offset; | 
|---|
| 589 | dumb: | 
|---|
| 590 |  | 
|---|
| 591 | _IO_unsave_markers (fp); | 
|---|
| 592 | result = _IO_SYSSEEK (fp, offset, dir); | 
|---|
| 593 | if (result != EOF) | 
|---|
| 594 | { | 
|---|
| 595 | _IO_mask_flags (fp, 0, _IO_EOF_SEEN); | 
|---|
| 596 | fp->_old_offset = result; | 
|---|
| 597 | _IO_setg (fp, fp->_IO_buf_base, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 598 | _IO_setp (fp, fp->_IO_buf_base, fp->_IO_buf_base); | 
|---|
| 599 | } | 
|---|
| 600 | return result; | 
|---|
| 601 |  | 
|---|
| 602 | resync: | 
|---|
| 603 | /* We need to do it since it is possible that the file offset in | 
|---|
| 604 | the kernel may be changed behind our back. It may happen when | 
|---|
| 605 | we fopen a file and then do a fork. One process may access the | 
|---|
| 606 | file and the kernel file offset will be changed. */ | 
|---|
| 607 | if (fp->_old_offset >= 0) | 
|---|
| 608 | _IO_SYSSEEK (fp, fp->_old_offset, 0); | 
|---|
| 609 |  | 
|---|
| 610 | return offset; | 
|---|
| 611 | } | 
|---|
| 612 |  | 
|---|
| 613 | ssize_t | 
|---|
| 614 | attribute_compat_text_section | 
|---|
| 615 | _IO_old_file_write (FILE *f, const void *data, ssize_t n) | 
|---|
| 616 | { | 
|---|
| 617 | ssize_t to_do = n; | 
|---|
| 618 | while (to_do > 0) | 
|---|
| 619 | { | 
|---|
| 620 | ssize_t count = __write (f->_fileno, data, to_do); | 
|---|
| 621 | if (count == EOF) | 
|---|
| 622 | { | 
|---|
| 623 | f->_flags |= _IO_ERR_SEEN; | 
|---|
| 624 | break; | 
|---|
| 625 | } | 
|---|
| 626 | to_do -= count; | 
|---|
| 627 | data = (void *) ((char *) data + count); | 
|---|
| 628 | } | 
|---|
| 629 | n -= to_do; | 
|---|
| 630 | if (f->_old_offset >= 0) | 
|---|
| 631 | f->_old_offset += n; | 
|---|
| 632 | return n; | 
|---|
| 633 | } | 
|---|
| 634 |  | 
|---|
| 635 | size_t | 
|---|
| 636 | attribute_compat_text_section | 
|---|
| 637 | _IO_old_file_xsputn (FILE *f, const void *data, size_t n) | 
|---|
| 638 | { | 
|---|
| 639 | const char *s = (char *) data; | 
|---|
| 640 | size_t to_do = n; | 
|---|
| 641 | int must_flush = 0; | 
|---|
| 642 | size_t count = 0; | 
|---|
| 643 |  | 
|---|
| 644 | if (n <= 0) | 
|---|
| 645 | return 0; | 
|---|
| 646 | /* This is an optimized implementation. | 
|---|
| 647 | If the amount to be written straddles a block boundary | 
|---|
| 648 | (or the filebuf is unbuffered), use sys_write directly. */ | 
|---|
| 649 |  | 
|---|
| 650 | /* First figure out how much space is available in the buffer. */ | 
|---|
| 651 | if ((f->_flags & _IO_LINE_BUF) && (f->_flags & _IO_CURRENTLY_PUTTING)) | 
|---|
| 652 | { | 
|---|
| 653 | count = f->_IO_buf_end - f->_IO_write_ptr; | 
|---|
| 654 | if (count >= n) | 
|---|
| 655 | { | 
|---|
| 656 | const char *p; | 
|---|
| 657 | for (p = s + n; p > s; ) | 
|---|
| 658 | { | 
|---|
| 659 | if (*--p == '\n') | 
|---|
| 660 | { | 
|---|
| 661 | count = p - s + 1; | 
|---|
| 662 | must_flush = 1; | 
|---|
| 663 | break; | 
|---|
| 664 | } | 
|---|
| 665 | } | 
|---|
| 666 | } | 
|---|
| 667 | } | 
|---|
| 668 | else if (f->_IO_write_end > f->_IO_write_ptr) | 
|---|
| 669 | count = f->_IO_write_end - f->_IO_write_ptr; /* Space available. */ | 
|---|
| 670 |  | 
|---|
| 671 | /* Then fill the buffer. */ | 
|---|
| 672 | if (count > 0) | 
|---|
| 673 | { | 
|---|
| 674 | if (count > to_do) | 
|---|
| 675 | count = to_do; | 
|---|
| 676 | if (count > 20) | 
|---|
| 677 | { | 
|---|
| 678 | f->_IO_write_ptr = __mempcpy (f->_IO_write_ptr, s, count); | 
|---|
| 679 | s += count; | 
|---|
| 680 | } | 
|---|
| 681 | else | 
|---|
| 682 | { | 
|---|
| 683 | char *p = f->_IO_write_ptr; | 
|---|
| 684 | int i = (int) count; | 
|---|
| 685 | while (--i >= 0) | 
|---|
| 686 | *p++ = *s++; | 
|---|
| 687 | f->_IO_write_ptr = p; | 
|---|
| 688 | } | 
|---|
| 689 | to_do -= count; | 
|---|
| 690 | } | 
|---|
| 691 | if (to_do + must_flush > 0) | 
|---|
| 692 | { | 
|---|
| 693 | size_t block_size, do_write; | 
|---|
| 694 | /* Next flush the (full) buffer. */ | 
|---|
| 695 | if (__overflow (f, EOF) == EOF) | 
|---|
| 696 | return to_do == 0 ? EOF : n - to_do; | 
|---|
| 697 |  | 
|---|
| 698 | /* Try to maintain alignment: write a whole number of blocks. | 
|---|
| 699 | dont_write is what gets left over. */ | 
|---|
| 700 | block_size = f->_IO_buf_end - f->_IO_buf_base; | 
|---|
| 701 | do_write = to_do - (block_size >= 128 ? to_do % block_size : 0); | 
|---|
| 702 |  | 
|---|
| 703 | if (do_write) | 
|---|
| 704 | { | 
|---|
| 705 | count = old_do_write (f, s, do_write); | 
|---|
| 706 | to_do -= count; | 
|---|
| 707 | if (count < do_write) | 
|---|
| 708 | return n - to_do; | 
|---|
| 709 | } | 
|---|
| 710 |  | 
|---|
| 711 | /* Now write out the remainder.  Normally, this will fit in the | 
|---|
| 712 | buffer, but it's somewhat messier for line-buffered files, | 
|---|
| 713 | so we let _IO_default_xsputn handle the general case. */ | 
|---|
| 714 | if (to_do) | 
|---|
| 715 | to_do -= _IO_default_xsputn (f, s+do_write, to_do); | 
|---|
| 716 | } | 
|---|
| 717 | return n - to_do; | 
|---|
| 718 | } | 
|---|
| 719 |  | 
|---|
| 720 |  | 
|---|
| 721 | const struct _IO_jump_t _IO_old_file_jumps libio_vtable = | 
|---|
| 722 | { | 
|---|
| 723 | JUMP_INIT_DUMMY, | 
|---|
| 724 | JUMP_INIT(finish, _IO_old_file_finish), | 
|---|
| 725 | JUMP_INIT(overflow, _IO_old_file_overflow), | 
|---|
| 726 | JUMP_INIT(underflow, _IO_old_file_underflow), | 
|---|
| 727 | JUMP_INIT(uflow, _IO_default_uflow), | 
|---|
| 728 | JUMP_INIT(pbackfail, _IO_default_pbackfail), | 
|---|
| 729 | JUMP_INIT(xsputn, _IO_old_file_xsputn), | 
|---|
| 730 | JUMP_INIT(xsgetn, _IO_default_xsgetn), | 
|---|
| 731 | JUMP_INIT(seekoff, _IO_old_file_seekoff), | 
|---|
| 732 | JUMP_INIT(seekpos, _IO_default_seekpos), | 
|---|
| 733 | JUMP_INIT(setbuf, _IO_old_file_setbuf), | 
|---|
| 734 | JUMP_INIT(sync, _IO_old_file_sync), | 
|---|
| 735 | JUMP_INIT(doallocate, _IO_file_doallocate), | 
|---|
| 736 | JUMP_INIT(read, _IO_file_read), | 
|---|
| 737 | JUMP_INIT(write, _IO_old_file_write), | 
|---|
| 738 | JUMP_INIT(seek, _IO_file_seek), | 
|---|
| 739 | JUMP_INIT(close, _IO_file_close), | 
|---|
| 740 | JUMP_INIT(stat, _IO_file_stat) | 
|---|
| 741 | }; | 
|---|
| 742 |  | 
|---|
| 743 | compat_symbol (libc, _IO_old_do_write, _IO_do_write, GLIBC_2_0); | 
|---|
| 744 | compat_symbol (libc, _IO_old_file_attach, _IO_file_attach, GLIBC_2_0); | 
|---|
| 745 | compat_symbol (libc, _IO_old_file_close_it, _IO_file_close_it, GLIBC_2_0); | 
|---|
| 746 | compat_symbol (libc, _IO_old_file_finish, _IO_file_finish, GLIBC_2_0); | 
|---|
| 747 | compat_symbol (libc, _IO_old_file_fopen, _IO_file_fopen, GLIBC_2_0); | 
|---|
| 748 | compat_symbol (libc, _IO_old_file_init, _IO_file_init, GLIBC_2_0); | 
|---|
| 749 | compat_symbol (libc, _IO_old_file_setbuf, _IO_file_setbuf, GLIBC_2_0); | 
|---|
| 750 | compat_symbol (libc, _IO_old_file_sync, _IO_file_sync, GLIBC_2_0); | 
|---|
| 751 | compat_symbol (libc, _IO_old_file_overflow, _IO_file_overflow, GLIBC_2_0); | 
|---|
| 752 | compat_symbol (libc, _IO_old_file_seekoff, _IO_file_seekoff, GLIBC_2_0); | 
|---|
| 753 | compat_symbol (libc, _IO_old_file_underflow, _IO_file_underflow, GLIBC_2_0); | 
|---|
| 754 | compat_symbol (libc, _IO_old_file_write, _IO_file_write, GLIBC_2_0); | 
|---|
| 755 | compat_symbol (libc, _IO_old_file_xsputn, _IO_file_xsputn, GLIBC_2_0); | 
|---|
| 756 |  | 
|---|
| 757 | #endif | 
|---|
| 758 |  | 
|---|