]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - libio/wfileops.c
libio: Remove codecvt vtable [BZ #24588]
[thirdparty/glibc.git] / libio / wfileops.c
index 9cebe771263e228f8c69e2f734730503f2c3fe99..f1863db6383d4110108eae10e47b2bb57a6cd15c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993-2014 Free Software Foundation, Inc.
+/* Copyright (C) 1993-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Written by Ulrich Drepper <drepper@cygnus.com>.
    Based on the single byte version by Per Bothner <bothner@cygnus.com>.
 #include <stdlib.h>
 #include <string.h>
 
-
-#ifndef _LIBC
-# define _IO_new_do_write _IO_do_write
-# define _IO_new_file_attach _IO_file_attach
-# define _IO_new_file_close_it _IO_file_close_it
-# define _IO_new_file_finish _IO_file_finish
-# define _IO_new_file_fopen _IO_file_fopen
-# define _IO_new_file_init _IO_file_init
-# define _IO_new_file_setbuf _IO_file_setbuf
-# define _IO_new_file_sync _IO_file_sync
-# define _IO_new_file_overflow _IO_file_overflow
-# define _IO_new_file_seekoff _IO_file_seekoff
-# define _IO_new_file_underflow _IO_file_underflow
-# define _IO_new_file_write _IO_file_write
-# define _IO_new_file_xsputn _IO_file_xsputn
-#endif
-
-
 /* Convert TO_DO wide character from DATA to FP.
    Then mark FP as having empty buffers. */
 int
-_IO_wdo_write (fp, data, to_do)
-     _IO_FILE *fp;
-     const wchar_t *data;
-     _IO_size_t to_do;
+_IO_wdo_write (FILE *fp, const wchar_t *data, size_t to_do)
 {
   struct _IO_codecvt *cc = fp->_codecvt;
 
@@ -75,17 +54,32 @@ _IO_wdo_write (fp, data, to_do)
        {
          enum __codecvt_result result;
          const wchar_t *new_data;
+         char mb_buf[MB_LEN_MAX];
+         char *write_base, *write_ptr, *buf_end;
+
+         if (fp->_IO_write_ptr - fp->_IO_write_base < sizeof (mb_buf))
+           {
+             /* Make sure we have room for at least one multibyte
+                character.  */
+             write_ptr = write_base = mb_buf;
+             buf_end = mb_buf + sizeof (mb_buf);
+           }
+         else
+           {
+             write_ptr = fp->_IO_write_ptr;
+             write_base = fp->_IO_write_base;
+             buf_end = fp->_IO_buf_end;
+           }
 
          /* Now convert from the internal format into the external buffer.  */
-         result = (*cc->__codecvt_do_out) (cc, &fp->_wide_data->_IO_state,
-                                           data, data + to_do, &new_data,
-                                           fp->_IO_write_ptr,
-                                           fp->_IO_buf_end,
-                                           &fp->_IO_write_ptr);
+         result = __libio_codecvt_out (cc, &fp->_wide_data->_IO_state,
+                                       data, data + to_do, &new_data,
+                                       write_ptr,
+                                       buf_end,
+                                       &write_ptr);
 
          /* Write out what we produced so far.  */
-         if (_IO_new_do_write (fp, fp->_IO_write_base,
-                               fp->_IO_write_ptr - fp->_IO_write_base) == EOF)
+         if (_IO_new_do_write (fp, write_base, write_ptr - write_base) == EOF)
            /* Something went wrong.  */
            return WEOF;
 
@@ -106,7 +100,7 @@ _IO_wdo_write (fp, data, to_do)
             fp->_wide_data->_IO_buf_base);
   fp->_wide_data->_IO_write_base = fp->_wide_data->_IO_write_ptr
     = fp->_wide_data->_IO_buf_base;
-  fp->_wide_data->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
+  fp->_wide_data->_IO_write_end = ((fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
                                   ? fp->_wide_data->_IO_buf_base
                                   : fp->_wide_data->_IO_buf_end);
 
@@ -116,12 +110,15 @@ libc_hidden_def (_IO_wdo_write)
 
 
 wint_t
-_IO_wfile_underflow (fp)
-     _IO_FILE *fp;
+_IO_wfile_underflow (FILE *fp)
 {
   struct _IO_codecvt *cd;
   enum __codecvt_result status;
-  _IO_ssize_t count;
+  ssize_t count;
+
+  /* C99 requires EOF to be "sticky".  */
+  if (fp->_flags & _IO_EOF_SEEN)
+    return WEOF;
 
   if (__glibc_unlikely (fp->_flags & _IO_NO_READS))
     {
@@ -143,12 +140,12 @@ _IO_wfile_underflow (fp)
       fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
       fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
        fp->_wide_data->_IO_buf_base;
-      status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
-                                      fp->_IO_read_ptr, fp->_IO_read_end,
-                                      &read_stop,
-                                      fp->_wide_data->_IO_read_ptr,
-                                      fp->_wide_data->_IO_buf_end,
-                                      &fp->_wide_data->_IO_read_end);
+      status = __libio_codecvt_in (cd, &fp->_wide_data->_IO_state,
+                                  fp->_IO_read_ptr, fp->_IO_read_end,
+                                  &read_stop,
+                                  fp->_wide_data->_IO_read_ptr,
+                                  fp->_wide_data->_IO_buf_end,
+                                  &fp->_wide_data->_IO_read_end);
 
       fp->_IO_read_base = fp->_IO_read_ptr;
       fp->_IO_read_ptr = (char *) read_stop;
@@ -203,26 +200,21 @@ _IO_wfile_underflow (fp)
       _IO_wdoallocbuf (fp);
     }
 
-  /* Flush all line buffered files before reading. */
   /* FIXME This can/should be moved to genops ?? */
-  if (fp->_flags & (_IO_LINE_BUF|_IO_UNBUFFERED))
+  if (fp->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
     {
-#if 0
-      _IO_flush_all_linebuffered ();
-#else
       /* We used to flush all line-buffered stream.  This really isn't
         required by any standard.  My recollection is that
         traditional Unix systems did this for stdout.  stderr better
         not be line buffered.  So we do just that here
         explicitly.  --drepper */
-      _IO_acquire_lock (_IO_stdout);
+      _IO_acquire_lock (stdout);
 
-      if ((_IO_stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
+      if ((stdout->_flags & (_IO_LINKED | _IO_NO_WRITES | _IO_LINE_BUF))
          == (_IO_LINKED | _IO_LINE_BUF))
-       _IO_OVERFLOW (_IO_stdout, EOF);
+       _IO_OVERFLOW (stdout, EOF);
 
-      _IO_release_lock (_IO_stdout);
-#endif
+      _IO_release_lock (stdout);
     }
 
   _IO_switch_to_get_mode (fp);
@@ -242,7 +234,10 @@ _IO_wfile_underflow (fp)
   if (count <= 0)
     {
       if (count == 0 && naccbuf == 0)
-       fp->_flags |= _IO_EOF_SEEN;
+       {
+         fp->_flags |= _IO_EOF_SEEN;
+         fp->_offset = _IO_pos_BAD;
+       }
       else
        fp->_flags |= _IO_ERR_SEEN, count = 0;
     }
@@ -271,11 +266,11 @@ _IO_wfile_underflow (fp)
       naccbuf += to_copy;
       from = accbuf;
     }
-  status = (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
-                                  from, to, &read_ptr_copy,
-                                  fp->_wide_data->_IO_read_end,
-                                  fp->_wide_data->_IO_buf_end,
-                                  &fp->_wide_data->_IO_read_end);
+  status = __libio_codecvt_in (cd, &fp->_wide_data->_IO_state,
+                              from, to, &read_ptr_copy,
+                              fp->_wide_data->_IO_read_end,
+                              fp->_wide_data->_IO_buf_end,
+                              &fp->_wide_data->_IO_read_end);
 
   if (__glibc_unlikely (naccbuf != 0))
     fp->_IO_read_ptr += MAX (0, read_ptr_copy - &accbuf[naccbuf - to_copy]);
@@ -336,7 +331,7 @@ libc_hidden_def (_IO_wfile_underflow)
 
 
 static wint_t
-_IO_wfile_underflow_mmap (_IO_FILE *fp)
+_IO_wfile_underflow_mmap (FILE *fp)
 {
   struct _IO_codecvt *cd;
   const char *read_stop;
@@ -377,12 +372,12 @@ _IO_wfile_underflow_mmap (_IO_FILE *fp)
   fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
   fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_read_ptr =
     fp->_wide_data->_IO_buf_base;
-  (*cd->__codecvt_do_in) (cd, &fp->_wide_data->_IO_state,
-                         fp->_IO_read_ptr, fp->_IO_read_end,
-                         &read_stop,
-                         fp->_wide_data->_IO_read_ptr,
-                         fp->_wide_data->_IO_buf_end,
-                         &fp->_wide_data->_IO_read_end);
+  __libio_codecvt_in (cd, &fp->_wide_data->_IO_state,
+                     fp->_IO_read_ptr, fp->_IO_read_end,
+                     &read_stop,
+                     fp->_wide_data->_IO_read_ptr,
+                     fp->_wide_data->_IO_buf_end,
+                     &fp->_wide_data->_IO_read_end);
 
   fp->_IO_read_ptr = (char *) read_stop;
 
@@ -397,7 +392,7 @@ _IO_wfile_underflow_mmap (_IO_FILE *fp)
 }
 
 static wint_t
-_IO_wfile_underflow_maybe_mmap (_IO_FILE *fp)
+_IO_wfile_underflow_maybe_mmap (FILE *fp)
 {
   /* This is the first read attempt.  Doing the underflow will choose mmap
      or vanilla operations and then punt to the chosen underflow routine.
@@ -410,9 +405,7 @@ _IO_wfile_underflow_maybe_mmap (_IO_FILE *fp)
 
 
 wint_t
-_IO_wfile_overflow (f, wch)
-     _IO_FILE *f;
-     wint_t wch;
+_IO_wfile_overflow (FILE *f, wint_t wch)
 {
   if (f->_flags & _IO_NO_WRITES) /* SET ERROR */
     {
@@ -427,6 +420,7 @@ _IO_wfile_overflow (f, wch)
       if (f->_wide_data->_IO_write_base == 0)
        {
          _IO_wdoallocbuf (f);
+         _IO_free_wbackup_area (f);
          _IO_wsetg (f, f->_wide_data->_IO_buf_base,
                     f->_wide_data->_IO_buf_base, f->_wide_data->_IO_buf_base);
 
@@ -465,7 +459,7 @@ _IO_wfile_overflow (f, wch)
       f->_IO_read_base = f->_IO_read_ptr = f->_IO_read_end;
 
       f->_flags |= _IO_CURRENTLY_PUTTING;
-      if (f->_flags & (_IO_LINE_BUF+_IO_UNBUFFERED))
+      if (f->_flags & (_IO_LINE_BUF | _IO_UNBUFFERED))
        f->_wide_data->_IO_write_end = f->_wide_data->_IO_write_ptr;
     }
   if (wch == WEOF)
@@ -484,10 +478,9 @@ _IO_wfile_overflow (f, wch)
 libc_hidden_def (_IO_wfile_overflow)
 
 wint_t
-_IO_wfile_sync (fp)
-     _IO_FILE *fp;
+_IO_wfile_sync (FILE *fp)
 {
-  _IO_ssize_t delta;
+  ssize_t delta;
   wint_t retval = 0;
 
   /*    char* ptr = cur_ptr(); */
@@ -500,9 +493,9 @@ _IO_wfile_sync (fp)
       /* We have to find out how many bytes we have to go back in the
         external buffer.  */
       struct _IO_codecvt *cv = fp->_codecvt;
-      _IO_off64_t new_pos;
+      off64_t new_pos;
 
-      int clen = (*cv->__codecvt_do_encoding) (cv);
+      int clen = __libio_codecvt_encoding (cv);
 
       if (clen > 0)
        /* It is easy, a fixed number of input bytes are used for each
@@ -515,25 +508,24 @@ _IO_wfile_sync (fp)
             generate the wide characters up to the current reading
             position.  */
          int nread;
-
+         size_t wnread = (fp->_wide_data->_IO_read_ptr
+                          - fp->_wide_data->_IO_read_base);
          fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
-         nread = (*cv->__codecvt_do_length) (cv, &fp->_wide_data->_IO_state,
-                                             fp->_IO_read_base,
-                                             fp->_IO_read_end, delta);
+         nread = __libio_codecvt_length (cv, &fp->_wide_data->_IO_state,
+                                         fp->_IO_read_base,
+                                         fp->_IO_read_end, wnread);
          fp->_IO_read_ptr = fp->_IO_read_base + nread;
          delta = -(fp->_IO_read_end - fp->_IO_read_base - nread);
        }
 
       new_pos = _IO_SYSSEEK (fp, delta, 1);
-      if (new_pos != (_IO_off64_t) EOF)
+      if (new_pos != (off64_t) EOF)
        {
          fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
          fp->_IO_read_end = fp->_IO_read_ptr;
        }
-#ifdef ESPIPE
       else if (errno == ESPIPE)
        ; /* Ignore error from unseekable devices. */
-#endif
       else
        retval = WEOF;
     }
@@ -552,11 +544,11 @@ libc_hidden_def (_IO_wfile_sync)
 
    Returns 0 on success and -1 on error with the _IO_ERR_SEEN flag set.  */
 static int
-adjust_wide_data (_IO_FILE *fp, bool do_convert)
+adjust_wide_data (FILE *fp, bool do_convert)
 {
   struct _IO_codecvt *cv = fp->_codecvt;
 
-  int clen = (*cv->__codecvt_do_encoding) (cv);
+  int clen = __libio_codecvt_encoding (cv);
 
   /* Take the easy way out for constant length encodings if we don't need to
      convert.  */
@@ -573,12 +565,12 @@ adjust_wide_data (_IO_FILE *fp, bool do_convert)
     {
 
       fp->_wide_data->_IO_last_state = fp->_wide_data->_IO_state;
-      status = (*cv->__codecvt_do_in) (cv, &fp->_wide_data->_IO_state,
-                                      fp->_IO_read_base, fp->_IO_read_ptr,
-                                      &read_stop,
-                                      fp->_wide_data->_IO_read_base,
-                                      fp->_wide_data->_IO_buf_end,
-                                      &fp->_wide_data->_IO_read_end);
+      status = __libio_codecvt_in (cv, &fp->_wide_data->_IO_state,
+                                  fp->_IO_read_base, fp->_IO_read_ptr,
+                                  &read_stop,
+                                  fp->_wide_data->_IO_read_base,
+                                  fp->_wide_data->_IO_buf_end,
+                                  &fp->_wide_data->_IO_read_end);
 
       /* Should we return EILSEQ?  */
       if (__glibc_unlikely (status == __codecvt_error))
@@ -596,29 +588,38 @@ done:
   return 0;
 }
 
-_IO_off64_t
-_IO_wfile_seekoff (fp, offset, dir, mode)
-     _IO_FILE *fp;
-     _IO_off64_t offset;
-     int dir;
-     int mode;
+/* ftell{,o} implementation for wide mode.  Don't modify any state of the file
+   pointer while we try to get the current state of the stream except in one
+   case, which is when we have unflushed writes in append mode.  */
+static off64_t
+do_ftell_wide (FILE *fp)
 {
-  _IO_off64_t result;
-  _IO_off64_t delta, new_offset;
-  long int count;
-  /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
-     offset of the underlying file must be exact.  */
-  int must_be_exact = ((fp->_wide_data->_IO_read_base
-                       == fp->_wide_data->_IO_read_end)
-                      && (fp->_wide_data->_IO_write_base
-                          == fp->_wide_data->_IO_write_ptr));
+  off64_t result, offset = 0;
 
-  bool was_writing = ((fp->_wide_data->_IO_write_ptr
-                      > fp->_wide_data->_IO_write_base)
-                     || _IO_in_put_mode (fp));
-
-  if (mode == 0)
+  /* No point looking for offsets in the buffer if it hasn't even been
+     allocated.  */
+  if (fp->_wide_data->_IO_buf_base != NULL)
     {
+      const wchar_t *wide_read_base;
+      const wchar_t *wide_read_ptr;
+      const wchar_t *wide_read_end;
+      bool unflushed_writes = (fp->_wide_data->_IO_write_ptr
+                              > fp->_wide_data->_IO_write_base);
+
+      bool append_mode = (fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING;
+
+      /* When we have unflushed writes in append mode, seek to the end of the
+        file and record that offset.  This is the only time we change the file
+        stream state and it is safe since the file handle is active.  */
+      if (unflushed_writes && append_mode)
+       {
+         result = _IO_SYSSEEK (fp, 0, _IO_seek_end);
+         if (result == _IO_pos_BAD)
+           return EOF;
+         else
+           fp->_offset = result;
+       }
+
       /* XXX For wide stream with backup store it is not very
         reasonable to determine the offset.  The pushed-back
         character might require a state change and we need not be
@@ -633,14 +634,135 @@ _IO_wfile_seekoff (fp, offset, dir, mode)
              return -1;
            }
 
-         /* There is no more data in the backup buffer.  We can
-            switch back.  */
-         _IO_switch_to_main_wget_area (fp);
+         /* Nothing in the backup store, so note the backed up pointers
+            without changing the state.  */
+         wide_read_base = fp->_wide_data->_IO_save_base;
+         wide_read_ptr = wide_read_base;
+         wide_read_end = fp->_wide_data->_IO_save_end;
+       }
+      else
+       {
+         wide_read_base = fp->_wide_data->_IO_read_base;
+         wide_read_ptr = fp->_wide_data->_IO_read_ptr;
+         wide_read_end = fp->_wide_data->_IO_read_end;
+       }
+
+      struct _IO_codecvt *cv = fp->_codecvt;
+      int clen = __libio_codecvt_encoding (cv);
+
+      if (!unflushed_writes)
+       {
+         if (clen > 0)
+           {
+             offset -= (wide_read_end - wide_read_ptr) * clen;
+             offset -= fp->_IO_read_end - fp->_IO_read_ptr;
+           }
+         else
+           {
+             int nread;
+
+             size_t delta = wide_read_ptr - wide_read_base;
+             __mbstate_t state = fp->_wide_data->_IO_last_state;
+             nread = __libio_codecvt_length (cv, &state,
+                                             fp->_IO_read_base,
+                                             fp->_IO_read_end, delta);
+             offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
+           }
+       }
+      else
+       {
+         if (clen > 0)
+           offset += (fp->_wide_data->_IO_write_ptr
+                      - fp->_wide_data->_IO_write_base) * clen;
+         else
+           {
+             size_t delta = (fp->_wide_data->_IO_write_ptr
+                             - fp->_wide_data->_IO_write_base);
+
+             /* Allocate enough space for the conversion.  */
+             size_t outsize = delta * sizeof (wchar_t);
+             char *out = malloc (outsize);
+             char *outstop = out;
+             const wchar_t *in = fp->_wide_data->_IO_write_base;
+
+             enum __codecvt_result status;
+
+             __mbstate_t state = fp->_wide_data->_IO_last_state;
+             status = __libio_codecvt_out (cv, &state, in, in + delta, &in,
+                                           out, out + outsize, &outstop);
+
+             /* We don't check for __codecvt_partial because it can be
+                returned on one of two conditions: either the output
+                buffer is full or the input sequence is incomplete.  We
+                take care to allocate enough buffer and our input
+                sequences must be complete since they are accepted as
+                wchar_t; if not, then that is an error.  */
+             if (__glibc_unlikely (status != __codecvt_ok))
+               {
+                 free (out);
+                 return WEOF;
+               }
+
+             offset += outstop - out;
+             free (out);
+           }
+
+         /* We don't trust _IO_read_end to represent the current file offset
+            when writing in append mode because the value would have to be
+            shifted to the end of the file during a flush.  Use the write base
+            instead, along with the new offset we got above when we did a seek
+            to the end of the file.  */
+         if (append_mode)
+           offset += fp->_IO_write_ptr - fp->_IO_write_base;
+         /* For all other modes, _IO_read_end represents the file offset.  */
+         else
+           offset += fp->_IO_write_ptr - fp->_IO_read_end;
        }
+    }
+
+  if (fp->_offset != _IO_pos_BAD)
+    result = fp->_offset;
+  else
+    result = _IO_SYSSEEK (fp, 0, _IO_seek_cur);
+
+  if (result == EOF)
+    return result;
 
-      dir = _IO_seek_cur, offset = 0; /* Don't move any pointers. */
+  result += offset;
+
+  if (result < 0)
+    {
+      __set_errno (EINVAL);
+      return EOF;
     }
 
+  return result;
+}
+
+off64_t
+_IO_wfile_seekoff (FILE *fp, off64_t offset, int dir, int mode)
+{
+  off64_t result;
+  off64_t delta, new_offset;
+  long int count;
+
+  /* Short-circuit into a separate function.  We don't want to mix any
+     functionality and we don't want to touch anything inside the FILE
+     object. */
+  if (mode == 0)
+    return do_ftell_wide (fp);
+
+  /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
+     offset of the underlying file must be exact.  */
+  int must_be_exact = ((fp->_wide_data->_IO_read_base
+                       == fp->_wide_data->_IO_read_end)
+                      && (fp->_wide_data->_IO_write_base
+                          == fp->_wide_data->_IO_write_ptr));
+
+  bool was_writing = ((fp->_wide_data->_IO_write_ptr
+                      > fp->_wide_data->_IO_write_base)
+                     || _IO_in_put_mode (fp));
+
   /* Flush unwritten characters.
      (This may do an unneeded write if we seek within the buffer.
      But to be able to switch to reading, we would need to set
@@ -648,7 +770,7 @@ _IO_wfile_seekoff (fp, offset, dir, mode)
      which assumes file_ptr() is eGptr.  Anyway, since we probably
      end up flushing when we close(), it doesn't make much difference.)
      FIXME: simulate mem-mapped files. */
-  else if (was_writing && _IO_switch_to_wget_mode (fp))
+  if (was_writing && _IO_switch_to_wget_mode (fp))
     return WEOF;
 
   if (fp->_wide_data->_IO_buf_base == NULL)
@@ -678,7 +800,7 @@ _IO_wfile_seekoff (fp, offset, dir, mode)
         find out which position in the external buffer corresponds to
         the current position in the internal buffer.  */
       cv = fp->_codecvt;
-      clen = (*cv->__codecvt_do_encoding) (cv);
+      clen = __libio_codecvt_encoding (cv);
 
       if (mode != 0 || !was_writing)
        {
@@ -693,93 +815,21 @@ _IO_wfile_seekoff (fp, offset, dir, mode)
            {
              int nread;
 
-           flushed:
              delta = (fp->_wide_data->_IO_read_ptr
                       - fp->_wide_data->_IO_read_base);
              fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
-             nread = (*cv->__codecvt_do_length) (cv,
-                                                 &fp->_wide_data->_IO_state,
-                                                 fp->_IO_read_base,
-                                                 fp->_IO_read_end, delta);
+             nread = __libio_codecvt_length (cv,
+                                             &fp->_wide_data->_IO_state,
+                                             fp->_IO_read_base,
+                                             fp->_IO_read_end, delta);
              fp->_IO_read_ptr = fp->_IO_read_base + nread;
              fp->_wide_data->_IO_read_end = fp->_wide_data->_IO_read_ptr;
              offset -= fp->_IO_read_end - fp->_IO_read_base - nread;
            }
        }
-      else
-       {
-         char *new_write_ptr = fp->_IO_write_ptr;
-
-         if (clen > 0)
-           offset += (fp->_wide_data->_IO_write_ptr
-                      - fp->_wide_data->_IO_write_base) / clen;
-         else
-           {
-             enum __codecvt_result status = __codecvt_ok;
-             delta = (fp->_wide_data->_IO_write_ptr
-                      - fp->_wide_data->_IO_write_base);
-             const wchar_t *write_base = fp->_wide_data->_IO_write_base;
-
-             /* FIXME: This actually ends up in two iterations of conversion,
-                one here and the next when the buffer actually gets flushed.
-                It may be possible to optimize this in future so that
-                wdo_write identifies already converted content and does not
-                redo it.  In any case, this is much better than having to
-                flush buffers for every ftell.  */
-             do
-               {
-                 /* There is not enough space in the buffer to do the entire
-                    conversion, so there is no point trying to avoid the
-                    buffer flush.  Just do it and go back to how it was with
-                    the read mode.  */
-                 if (status == __codecvt_partial
-                     || (delta > 0 && new_write_ptr == fp->_IO_buf_end))
-                   {
-                     if (_IO_switch_to_wget_mode (fp))
-                       return WEOF;
-                     goto flushed;
-                   }
-
-                 const wchar_t *new_wbase = fp->_wide_data->_IO_write_base;
-                 fp->_wide_data->_IO_state = fp->_wide_data->_IO_last_state;
-                 status = (*cv->__codecvt_do_out) (cv,
-                                                   &fp->_wide_data->_IO_state,
-                                                   write_base,
-                                                   write_base + delta,
-                                                   &new_wbase,
-                                                   new_write_ptr,
-                                                   fp->_IO_buf_end,
-                                                   &new_write_ptr);
-
-                 delta -= new_wbase - write_base;
-
-                 /* If there was an error, then return WEOF.
-                    TODO: set buffer state.  */
-                 if (__glibc_unlikely (status == __codecvt_error))
-                     return WEOF;
-               }
-             while (delta > 0);
-           }
-
-         /* _IO_read_end coincides with fp._offset, so the actual file position
-            is fp._offset - (_IO_read_end - new_write_ptr).  This is fine
-            even if fp._offset is not set, since fp->_IO_read_end is then at
-            _IO_buf_base and this adjustment is for unbuffered output.  */
-         offset -= fp->_IO_read_end - new_write_ptr;
-       }
 
       if (fp->_offset == _IO_pos_BAD)
-       {
-         if (mode != 0)
-           goto dumb;
-         else
-           {
-             result = _IO_SYSSEEK (fp, 0, dir);
-             if (result == EOF)
-               return result;
-             fp->_offset = result;
-           }
-       }
+       goto dumb;
 
       /* Make offset absolute, assuming current pointer is file_ptr(). */
       offset += fp->_offset;
@@ -800,18 +850,17 @@ _IO_wfile_seekoff (fp, offset, dir, mode)
          goto dumb;
       }
     }
-  /* At this point, dir==_IO_seek_set. */
 
-  /* If we are only interested in the current position we've found it now.  */
-  if (mode == 0)
-    return offset;
+  _IO_free_wbackup_area (fp);
+
+  /* At this point, dir==_IO_seek_set. */
 
   /* If destination is within current buffer, optimize: */
   if (fp->_offset != _IO_pos_BAD && fp->_IO_read_base != NULL
       && !_IO_in_backup (fp))
     {
-      _IO_off64_t start_offset = (fp->_offset
-                                 - (fp->_IO_read_end - fp->_IO_buf_base));
+      off64_t start_offset = (fp->_offset
+                              - (fp->_IO_read_end - fp->_IO_buf_base));
       if (offset >= start_offset && offset < fp->_offset)
        {
          _IO_setg (fp, fp->_IO_buf_base,
@@ -904,16 +953,13 @@ resync:
 libc_hidden_def (_IO_wfile_seekoff)
 
 
-_IO_size_t
-_IO_wfile_xsputn (f, data, n)
-     _IO_FILE *f;
-     const void *data;
-     _IO_size_t n;
+size_t
+_IO_wfile_xsputn (FILE *f, const void *data, size_t n)
 {
   const wchar_t *s = (const wchar_t *) data;
-  _IO_size_t to_do = n;
+  size_t to_do = n;
   int must_flush = 0;
-  _IO_size_t count;
+  size_t count;
 
   if (n <= 0)
     return 0;
@@ -947,13 +993,8 @@ _IO_wfile_xsputn (f, data, n)
        count = to_do;
       if (count > 20)
        {
-#ifdef _LIBC
          f->_wide_data->_IO_write_ptr =
            __wmempcpy (f->_wide_data->_IO_write_ptr, s, count);
-#else
-         wmemcpy (f->_wide_data->_IO_write_ptr, s, count);
-         f->_wide_data->_IO_write_ptr += count;
-#endif
          s += count;
        }
       else
@@ -979,7 +1020,7 @@ _IO_wfile_xsputn (f, data, n)
 libc_hidden_def (_IO_wfile_xsputn)
 
 
-const struct _IO_jump_t _IO_wfile_jumps =
+const struct _IO_jump_t _IO_wfile_jumps libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT(finish, _IO_new_file_finish),
@@ -1005,7 +1046,7 @@ const struct _IO_jump_t _IO_wfile_jumps =
 libc_hidden_data_def (_IO_wfile_jumps)
 
 
-const struct _IO_jump_t _IO_wfile_jumps_mmap =
+const struct _IO_jump_t _IO_wfile_jumps_mmap libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT(finish, _IO_new_file_finish),
@@ -1029,7 +1070,7 @@ const struct _IO_jump_t _IO_wfile_jumps_mmap =
   JUMP_INIT(imbue, _IO_default_imbue)
 };
 
-const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap =
+const struct _IO_jump_t _IO_wfile_jumps_maybe_mmap libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT(finish, _IO_new_file_finish),