OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [fseek.c] - Diff between revs 148 and 158

Only display areas with differences | Details | Blame | View Log

Rev 148 Rev 158
/*
/*
 * Copyright (c) 1990 The Regents of the University of California.
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms are permitted
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
 */
 
 
/*
/*
FUNCTION
FUNCTION
<<fseek>>, <<fseeko>>---set file position
<<fseek>>, <<fseeko>>---set file position
 
 
INDEX
INDEX
        fseek
        fseek
INDEX
INDEX
        fseeko
        fseeko
INDEX
INDEX
        _fseek_r
        _fseek_r
INDEX
INDEX
        _fseeko_r
        _fseeko_r
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        int fseek(FILE *<[fp]>, long <[offset]>, int <[whence]>)
        int fseek(FILE *<[fp]>, long <[offset]>, int <[whence]>)
        int fseeko(FILE *<[fp]>, off_t <[offset]>, int <[whence]>)
        int fseeko(FILE *<[fp]>, off_t <[offset]>, int <[whence]>)
        int _fseek_r(struct _reent *<[ptr]>, FILE *<[fp]>,
        int _fseek_r(struct _reent *<[ptr]>, FILE *<[fp]>,
                     long <[offset]>, int <[whence]>)
                     long <[offset]>, int <[whence]>)
        int _fseeko_r(struct _reent *<[ptr]>, FILE *<[fp]>,
        int _fseeko_r(struct _reent *<[ptr]>, FILE *<[fp]>,
                     off_t <[offset]>, int <[whence]>)
                     off_t <[offset]>, int <[whence]>)
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        int fseek(<[fp]>, <[offset]>, <[whence]>)
        int fseek(<[fp]>, <[offset]>, <[whence]>)
        FILE *<[fp]>;
        FILE *<[fp]>;
        long <[offset]>;
        long <[offset]>;
        int <[whence]>;
        int <[whence]>;
 
 
        int fseeko(<[fp]>, <[offset]>, <[whence]>)
        int fseeko(<[fp]>, <[offset]>, <[whence]>)
        FILE *<[fp]>;
        FILE *<[fp]>;
        off_t <[offset]>;
        off_t <[offset]>;
        int <[whence]>;
        int <[whence]>;
 
 
        int _fseek_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>)
        int _fseek_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>)
        struct _reent *<[ptr]>;
        struct _reent *<[ptr]>;
        FILE *<[fp]>;
        FILE *<[fp]>;
        long <[offset]>;
        long <[offset]>;
        int <[whence]>;
        int <[whence]>;
 
 
        int _fseeko_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>)
        int _fseeko_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>)
        struct _reent *<[ptr]>;
        struct _reent *<[ptr]>;
        FILE *<[fp]>;
        FILE *<[fp]>;
        off_t <[offset]>;
        off_t <[offset]>;
        int <[whence]>;
        int <[whence]>;
 
 
DESCRIPTION
DESCRIPTION
Objects of type <<FILE>> can have a ``position'' that records how much
Objects of type <<FILE>> can have a ``position'' that records how much
of the file your program has already read.  Many of the <<stdio>> functions
of the file your program has already read.  Many of the <<stdio>> functions
depend on this position, and many change it as a side effect.
depend on this position, and many change it as a side effect.
 
 
You can use <<fseek>>/<<fseeko>> to set the position for the file identified by
You can use <<fseek>>/<<fseeko>> to set the position for the file identified by
<[fp]>.  The value of <[offset]> determines the new position, in one
<[fp]>.  The value of <[offset]> determines the new position, in one
of three ways selected by the value of <[whence]> (defined as macros
of three ways selected by the value of <[whence]> (defined as macros
in `<<stdio.h>>'):
in `<<stdio.h>>'):
 
 
<<SEEK_SET>>---<[offset]> is the absolute file position (an offset
<<SEEK_SET>>---<[offset]> is the absolute file position (an offset
from the beginning of the file) desired.  <[offset]> must be positive.
from the beginning of the file) desired.  <[offset]> must be positive.
 
 
<<SEEK_CUR>>---<[offset]> is relative to the current file position.
<<SEEK_CUR>>---<[offset]> is relative to the current file position.
<[offset]> can meaningfully be either positive or negative.
<[offset]> can meaningfully be either positive or negative.
 
 
<<SEEK_END>>---<[offset]> is relative to the current end of file.
<<SEEK_END>>---<[offset]> is relative to the current end of file.
<[offset]> can meaningfully be either positive (to increase the size
<[offset]> can meaningfully be either positive (to increase the size
of the file) or negative.
of the file) or negative.
 
 
See <<ftell>>/<<ftello>> to determine the current file position.
See <<ftell>>/<<ftello>> to determine the current file position.
 
 
RETURNS
RETURNS
<<fseek>>/<<fseeko>> return <<0>> when successful.  On failure, the
<<fseek>>/<<fseeko>> return <<0>> when successful.  On failure, the
result is <<EOF>>.  The reason for failure is indicated in <<errno>>:
result is <<EOF>>.  The reason for failure is indicated in <<errno>>:
either <<ESPIPE>> (the stream identified by <[fp]> doesn't support
either <<ESPIPE>> (the stream identified by <[fp]> doesn't support
repositioning) or <<EINVAL>> (invalid file position).
repositioning) or <<EINVAL>> (invalid file position).
 
 
PORTABILITY
PORTABILITY
ANSI C requires <<fseek>>.
ANSI C requires <<fseek>>.
 
 
<<fseeko>> is defined by the Single Unix specification.
<<fseeko>> is defined by the Single Unix specification.
 
 
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/
*/
 
 
#include <_ansi.h>
#include <_ansi.h>
#include <reent.h>
#include <reent.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <fcntl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdlib.h>
#include <errno.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include "local.h"
#include "local.h"
 
 
#define POS_ERR (-(_fpos_t)1)
#define POS_ERR (-(_fpos_t)1)
 
 
/*
/*
 * Seek the given file to the given offset.
 * Seek the given file to the given offset.
 * `Whence' must be one of the three SEEK_* macros.
 * `Whence' must be one of the three SEEK_* macros.
 */
 */
 
 
int
int
_DEFUN(_fseek_r, (ptr, fp, offset, whence),
_DEFUN(_fseek_r, (ptr, fp, offset, whence),
       struct _reent *ptr _AND
       struct _reent *ptr _AND
       register FILE *fp  _AND
       register FILE *fp  _AND
       long offset        _AND
       long offset        _AND
       int whence)
       int whence)
{
{
  _fpos_t _EXFUN((*seekfn), (struct _reent *, _PTR, _fpos_t, int));
  _fpos_t _EXFUN((*seekfn), (struct _reent *, _PTR, _fpos_t, int));
  _fpos_t target;
  _fpos_t target;
  _fpos_t curoff = 0;
  _fpos_t curoff = 0;
  size_t n;
  size_t n;
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
  struct stat64 st;
  struct stat64 st;
#else
#else
  struct stat st;
  struct stat st;
#endif
#endif
  int havepos;
  int havepos;
 
 
  /* Make sure stdio is set up.  */
  /* Make sure stdio is set up.  */
 
 
  CHECK_INIT (ptr, fp);
  CHECK_INIT (ptr, fp);
 
 
  _flockfile (fp);
  _flockfile (fp);
 
 
  /* If we've been doing some writing, and we're in append mode
  /* If we've been doing some writing, and we're in append mode
     then we don't really know where the filepos is.  */
     then we don't really know where the filepos is.  */
 
 
  if (fp->_flags & __SAPP && fp->_flags & __SWR)
  if (fp->_flags & __SAPP && fp->_flags & __SWR)
    {
    {
      /* So flush the buffer and seek to the end.  */
      /* So flush the buffer and seek to the end.  */
      _fflush_r (ptr, fp);
      _fflush_r (ptr, fp);
    }
    }
 
 
  /* Have to be able to seek.  */
  /* Have to be able to seek.  */
 
 
  if ((seekfn = fp->_seek) == NULL)
  if ((seekfn = fp->_seek) == NULL)
    {
    {
      ptr->_errno = ESPIPE;     /* ??? */
      ptr->_errno = ESPIPE;     /* ??? */
      _funlockfile (fp);
      _funlockfile (fp);
      return EOF;
      return EOF;
    }
    }
 
 
  /*
  /*
   * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
   * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
   * After this, whence is either SEEK_SET or SEEK_END.
   * After this, whence is either SEEK_SET or SEEK_END.
   */
   */
 
 
  switch (whence)
  switch (whence)
    {
    {
    case SEEK_CUR:
    case SEEK_CUR:
      /*
      /*
       * In order to seek relative to the current stream offset,
       * In order to seek relative to the current stream offset,
       * we have to first find the current stream offset a la
       * we have to first find the current stream offset a la
       * ftell (see ftell for details).
       * ftell (see ftell for details).
       */
       */
      _fflush_r (ptr, fp);   /* may adjust seek offset on append stream */
      _fflush_r (ptr, fp);   /* may adjust seek offset on append stream */
      if (fp->_flags & __SOFF)
      if (fp->_flags & __SOFF)
        curoff = fp->_offset;
        curoff = fp->_offset;
      else
      else
        {
        {
          curoff = seekfn (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
          curoff = seekfn (ptr, fp->_cookie, (_fpos_t) 0, SEEK_CUR);
          if (curoff == -1L)
          if (curoff == -1L)
            {
            {
              _funlockfile (fp);
              _funlockfile (fp);
              return EOF;
              return EOF;
            }
            }
        }
        }
      if (fp->_flags & __SRD)
      if (fp->_flags & __SRD)
        {
        {
          curoff -= fp->_r;
          curoff -= fp->_r;
          if (HASUB (fp))
          if (HASUB (fp))
            curoff -= fp->_ur;
            curoff -= fp->_ur;
        }
        }
      else if (fp->_flags & __SWR && fp->_p != NULL)
      else if (fp->_flags & __SWR && fp->_p != NULL)
        curoff += fp->_p - fp->_bf._base;
        curoff += fp->_p - fp->_bf._base;
 
 
      offset += curoff;
      offset += curoff;
      whence = SEEK_SET;
      whence = SEEK_SET;
      havepos = 1;
      havepos = 1;
      break;
      break;
 
 
    case SEEK_SET:
    case SEEK_SET:
    case SEEK_END:
    case SEEK_END:
      havepos = 0;
      havepos = 0;
      break;
      break;
 
 
    default:
    default:
      ptr->_errno = EINVAL;
      ptr->_errno = EINVAL;
      _funlockfile (fp);
      _funlockfile (fp);
      return (EOF);
      return (EOF);
    }
    }
 
 
  /*
  /*
   * Can only optimise if:
   * Can only optimise if:
   *    reading (and not reading-and-writing);
   *    reading (and not reading-and-writing);
   *    not unbuffered; and
   *    not unbuffered; and
   *    this is a `regular' Unix file (and hence seekfn==__sseek).
   *    this is a `regular' Unix file (and hence seekfn==__sseek).
   * We must check __NBF first, because it is possible to have __NBF
   * We must check __NBF first, because it is possible to have __NBF
   * and __SOPT both set.
   * and __SOPT both set.
   */
   */
 
 
  if (fp->_bf._base == NULL)
  if (fp->_bf._base == NULL)
    __smakebuf_r (ptr, fp);
    __smakebuf_r (ptr, fp);
  if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
  if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
    goto dumb;
    goto dumb;
  if ((fp->_flags & __SOPT) == 0)
  if ((fp->_flags & __SOPT) == 0)
    {
    {
      if (seekfn != __sseek
      if (seekfn != __sseek
          || fp->_file < 0
          || fp->_file < 0
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
          || _fstat64_r (ptr, fp->_file, &st)
          || _fstat64_r (ptr, fp->_file, &st)
#else
#else
          || _fstat_r (ptr, fp->_file, &st)
          || _fstat_r (ptr, fp->_file, &st)
#endif
#endif
          || (st.st_mode & S_IFMT) != S_IFREG)
          || (st.st_mode & S_IFMT) != S_IFREG)
        {
        {
          fp->_flags |= __SNPT;
          fp->_flags |= __SNPT;
          goto dumb;
          goto dumb;
        }
        }
#ifdef  HAVE_BLKSIZE
#ifdef  HAVE_BLKSIZE
      fp->_blksize = st.st_blksize;
      fp->_blksize = st.st_blksize;
#else
#else
      fp->_blksize = 1024;
      fp->_blksize = 1024;
#endif
#endif
      fp->_flags |= __SOPT;
      fp->_flags |= __SOPT;
    }
    }
 
 
  /*
  /*
   * We are reading; we can try to optimise.
   * We are reading; we can try to optimise.
   * Figure out where we are going and where we are now.
   * Figure out where we are going and where we are now.
   */
   */
 
 
  if (whence == SEEK_SET)
  if (whence == SEEK_SET)
    target = offset;
    target = offset;
  else
  else
    {
    {
#ifdef __USE_INTERNAL_STAT64
#ifdef __USE_INTERNAL_STAT64
      if (_fstat64_r (ptr, fp->_file, &st))
      if (_fstat64_r (ptr, fp->_file, &st))
#else
#else
      if (_fstat_r (ptr, fp->_file, &st))
      if (_fstat_r (ptr, fp->_file, &st))
#endif
#endif
        goto dumb;
        goto dumb;
      target = st.st_size + offset;
      target = st.st_size + offset;
    }
    }
  if ((long)target != target)
  if ((long)target != target)
    {
    {
      ptr->_errno = EOVERFLOW;
      ptr->_errno = EOVERFLOW;
      return EOF;
      return EOF;
    }
    }
 
 
  if (!havepos)
  if (!havepos)
    {
    {
      if (fp->_flags & __SOFF)
      if (fp->_flags & __SOFF)
        curoff = fp->_offset;
        curoff = fp->_offset;
      else
      else
        {
        {
          curoff = seekfn (ptr, fp->_cookie, 0L, SEEK_CUR);
          curoff = seekfn (ptr, fp->_cookie, 0L, SEEK_CUR);
          if (curoff == POS_ERR)
          if (curoff == POS_ERR)
            goto dumb;
            goto dumb;
        }
        }
      curoff -= fp->_r;
      curoff -= fp->_r;
      if (HASUB (fp))
      if (HASUB (fp))
        curoff -= fp->_ur;
        curoff -= fp->_ur;
    }
    }
 
 
  /*
  /*
   * Compute the number of bytes in the input buffer (pretending
   * Compute the number of bytes in the input buffer (pretending
   * that any ungetc() input has been discarded).  Adjust current
   * that any ungetc() input has been discarded).  Adjust current
   * offset backwards by this count so that it represents the
   * offset backwards by this count so that it represents the
   * file offset for the first byte in the current input buffer.
   * file offset for the first byte in the current input buffer.
   */
   */
 
 
  if (HASUB (fp))
  if (HASUB (fp))
    {
    {
      curoff += fp->_r;       /* kill off ungetc */
      curoff += fp->_r;       /* kill off ungetc */
      n = fp->_up - fp->_bf._base;
      n = fp->_up - fp->_bf._base;
      curoff -= n;
      curoff -= n;
      n += fp->_ur;
      n += fp->_ur;
    }
    }
  else
  else
    {
    {
      n = fp->_p - fp->_bf._base;
      n = fp->_p - fp->_bf._base;
      curoff -= n;
      curoff -= n;
      n += fp->_r;
      n += fp->_r;
    }
    }
 
 
  /*
  /*
   * If the target offset is within the current buffer,
   * If the target offset is within the current buffer,
   * simply adjust the pointers, clear EOF, undo ungetc(),
   * simply adjust the pointers, clear EOF, undo ungetc(),
   * and return.
   * and return.
   */
   */
 
 
  if (target >= curoff && target < curoff + n)
  if (target >= curoff && target < curoff + n)
    {
    {
      register int o = target - curoff;
      register int o = target - curoff;
 
 
      fp->_p = fp->_bf._base + o;
      fp->_p = fp->_bf._base + o;
      fp->_r = n - o;
      fp->_r = n - o;
      if (HASUB (fp))
      if (HASUB (fp))
        FREEUB (ptr, fp);
        FREEUB (ptr, fp);
      fp->_flags &= ~__SEOF;
      fp->_flags &= ~__SEOF;
      memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
      memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
      _funlockfile (fp);
      _funlockfile (fp);
      return 0;
      return 0;
    }
    }
 
 
  /*
  /*
   * The place we want to get to is not within the current buffer,
   * The place we want to get to is not within the current buffer,
   * but we can still be kind to the kernel copyout mechanism.
   * but we can still be kind to the kernel copyout mechanism.
   * By aligning the file offset to a block boundary, we can let
   * By aligning the file offset to a block boundary, we can let
   * the kernel use the VM hardware to map pages instead of
   * the kernel use the VM hardware to map pages instead of
   * copying bytes laboriously.  Using a block boundary also
   * copying bytes laboriously.  Using a block boundary also
   * ensures that we only read one block, rather than two.
   * ensures that we only read one block, rather than two.
   */
   */
 
 
  curoff = target & ~(fp->_blksize - 1);
  curoff = target & ~(fp->_blksize - 1);
  if (seekfn (ptr, fp->_cookie, curoff, SEEK_SET) == POS_ERR)
  if (seekfn (ptr, fp->_cookie, curoff, SEEK_SET) == POS_ERR)
    goto dumb;
    goto dumb;
  fp->_r = 0;
  fp->_r = 0;
  fp->_p = fp->_bf._base;
  fp->_p = fp->_bf._base;
  if (HASUB (fp))
  if (HASUB (fp))
    FREEUB (ptr, fp);
    FREEUB (ptr, fp);
  fp->_flags &= ~__SEOF;
  fp->_flags &= ~__SEOF;
  n = target - curoff;
  n = target - curoff;
  if (n)
  if (n)
    {
    {
      if (__srefill_r (ptr, fp) || fp->_r < n)
      if (__srefill_r (ptr, fp) || fp->_r < n)
        goto dumb;
        goto dumb;
      fp->_p += n;
      fp->_p += n;
      fp->_r -= n;
      fp->_r -= n;
    }
    }
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
  _funlockfile (fp);
  _funlockfile (fp);
  return 0;
  return 0;
 
 
  /*
  /*
   * We get here if we cannot optimise the seek ... just
   * We get here if we cannot optimise the seek ... just
   * do it.  Allow the seek function to change fp->_bf._base.
   * do it.  Allow the seek function to change fp->_bf._base.
   */
   */
 
 
dumb:
dumb:
  if (_fflush_r (ptr, fp)
  if (_fflush_r (ptr, fp)
      || seekfn (ptr, fp->_cookie, offset, whence) == POS_ERR)
      || seekfn (ptr, fp->_cookie, offset, whence) == POS_ERR)
    {
    {
      _funlockfile (fp);
      _funlockfile (fp);
      return EOF;
      return EOF;
    }
    }
  /* success: clear EOF indicator and discard ungetc() data */
  /* success: clear EOF indicator and discard ungetc() data */
  if (HASUB (fp))
  if (HASUB (fp))
    FREEUB (ptr, fp);
    FREEUB (ptr, fp);
  fp->_p = fp->_bf._base;
  fp->_p = fp->_bf._base;
  fp->_r = 0;
  fp->_r = 0;
  /* fp->_w = 0; *//* unnecessary (I think...) */
  /* fp->_w = 0; *//* unnecessary (I think...) */
  fp->_flags &= ~__SEOF;
  fp->_flags &= ~__SEOF;
  /* Reset no-optimization flag after successful seek.  The
  /* Reset no-optimization flag after successful seek.  The
     no-optimization flag may be set in the case of a read
     no-optimization flag may be set in the case of a read
     stream that is flushed which by POSIX/SUSv3 standards,
     stream that is flushed which by POSIX/SUSv3 standards,
     means that a corresponding seek must not optimize.  The
     means that a corresponding seek must not optimize.  The
     optimization is then allowed if no subsequent flush
     optimization is then allowed if no subsequent flush
     is performed.  */
     is performed.  */
  fp->_flags &= ~__SNPT;
  fp->_flags &= ~__SNPT;
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
  _funlockfile (fp);
  _funlockfile (fp);
  return 0;
  return 0;
}
}
 
 
#ifndef _REENT_ONLY
#ifndef _REENT_ONLY
 
 
int
int
_DEFUN(fseek, (fp, offset, whence),
_DEFUN(fseek, (fp, offset, whence),
       register FILE *fp _AND
       register FILE *fp _AND
       long offset       _AND
       long offset       _AND
       int whence)
       int whence)
{
{
  return _fseek_r (_REENT, fp, offset, whence);
  return _fseek_r (_REENT, fp, offset, whence);
}
}
 
 
#endif /* !_REENT_ONLY */
#endif /* !_REENT_ONLY */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.