diff options
Diffstat (limited to 'newlib/libc/stdio/fseek.c')
-rw-r--r-- | newlib/libc/stdio/fseek.c | 109 |
1 files changed, 18 insertions, 91 deletions
diff --git a/newlib/libc/stdio/fseek.c b/newlib/libc/stdio/fseek.c index 8b189ba17..1b7298914 100644 --- a/newlib/libc/stdio/fseek.c +++ b/newlib/libc/stdio/fseek.c @@ -17,25 +17,14 @@ /* FUNCTION -<<fseek>>, <<fseeko>>---set file position +<<fseek>>---set file position INDEX fseek -INDEX - fseeko -INDEX - _fseek_r -INDEX - _fseeko_r ANSI_SYNOPSIS #include <stdio.h> int fseek(FILE *<[fp]>, long <[offset]>, int <[whence]>) - int fseeko(FILE *<[fp]>, off_t <[offset]>, int <[whence]>) - int _fseek_r(struct _reent *<[ptr]>, FILE *<[fp]>, - long <[offset]>, int <[whence]>) - int _fseeko_r(struct _reent *<[ptr]>, FILE *<[fp]>, - off_t <[offset]>, int <[whence]>) TRAD_SYNOPSIS #include <stdio.h> @@ -44,29 +33,12 @@ TRAD_SYNOPSIS long <[offset]>; int <[whence]>; - int fseeko(<[fp]>, <[offset]>, <[whence]>) - FILE *<[fp]>; - off_t <[offset]>; - int <[whence]>; - - int _fseek_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>) - struct _reent *<[ptr]>; - FILE *<[fp]>; - long <[offset]>; - int <[whence]>; - - int _fseeko_r(<[ptr]>, <[fp]>, <[offset]>, <[whence]>) - struct _reent *<[ptr]>; - FILE *<[fp]>; - off_t <[offset]>; - int <[whence]>; - DESCRIPTION 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 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>> to set the position for the file identified by <[fp]>. The value of <[offset]> determines the new position, in one of three ways selected by the value of <[whence]> (defined as macros in `<<stdio.h>>'): @@ -81,10 +53,10 @@ from the beginning of the file) desired. <[offset]> must be positive. <[offset]> can meaningfully be either positive (to increase the size of the file) or negative. -See <<ftell>>/<<ftello>> to determine the current file position. +See <<ftell>> to determine the current file position. RETURNS -<<fseek>>/<<fseeko>> return <<0>> when successful. On failure, the +<<fseek>> returns <<0>> when successful. If <<fseek>> fails, the result is <<EOF>>. The reason for failure is indicated in <<errno>>: either <<ESPIPE>> (the stream identified by <[fp]> doesn't support repositioning) or <<EINVAL>> (invalid file position). @@ -92,14 +64,10 @@ repositioning) or <<EINVAL>> (invalid file position). PORTABILITY ANSI C requires <<fseek>>. -<<fseeko>> is defined by the Single Unix specification. - Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, <<lseek>>, <<read>>, <<sbrk>>, <<write>>. */ -#include <_ansi.h> -#include <reent.h> #include <stdio.h> #include <time.h> #include <fcntl.h> @@ -108,7 +76,7 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, #include <sys/stat.h> #include "local.h" -#define POS_ERR (-(_fpos_t)1) +#define POS_ERR (-(fpos_t)1) /* * Seek the given file to the given offset. @@ -116,24 +84,22 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, */ int -_DEFUN(_fseek_r, (ptr, fp, offset, whence), - struct _reent *ptr _AND - register FILE *fp _AND - long offset _AND - int whence) +fseek (fp, offset, whence) + register FILE *fp; + long offset; + int whence; { - _fpos_t _EXFUN((*seekfn), (_PTR, _fpos_t, int)); - _fpos_t target; - _fpos_t curoff = 0; + struct _reent *ptr; + fpos_t _EXFUN ((*seekfn), (void *, fpos_t, int)); + fpos_t target, curoff; size_t n; struct stat st; int havepos; /* Make sure stdio is set up. */ - CHECK_INIT (ptr); - - _flockfile (fp); + CHECK_INIT (fp); + ptr = fp->_data; /* If we've been doing some writing, and we're in append mode then we don't really know where the filepos is. */ @@ -149,7 +115,6 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), if ((seekfn = fp->_seek) == NULL) { ptr->_errno = ESPIPE; /* ??? */ - _funlockfile (fp); return EOF; } @@ -166,17 +131,14 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), * we have to first find the current stream offset a la * ftell (see ftell for details). */ - fflush (fp); /* may adjust seek offset on append stream */ + fflush(fp); /* may adjust seek offset on append stream */ if (fp->_flags & __SOFF) curoff = fp->_offset; else { - curoff = (*seekfn) (fp->_cookie, (_fpos_t) 0, SEEK_CUR); + curoff = (*seekfn) (fp->_cookie, (fpos_t) 0, SEEK_CUR); if (curoff == -1L) - { - _funlockfile (fp); - return EOF; - } + return EOF; } if (fp->_flags & __SRD) { @@ -199,7 +161,6 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), default: ptr->_errno = EINVAL; - _funlockfile (fp); return (EOF); } @@ -220,11 +181,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), { if (seekfn != __sseek || fp->_file < 0 -#ifdef __USE_INTERNAL_STAT64 - || _fstat64_r (ptr, fp->_file, &st) -#else || _fstat_r (ptr, fp->_file, &st) -#endif || (st.st_mode & S_IFMT) != S_IFREG) { fp->_flags |= __SNPT; @@ -247,11 +204,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), target = offset; else { -#ifdef __USE_INTERNAL_STAT64 - if (_fstat64_r (ptr, fp->_file, &st)) -#else if (_fstat_r (ptr, fp->_file, &st)) -#endif goto dumb; target = st.st_size + offset; } @@ -309,7 +262,6 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), if (HASUB (fp)) FREEUB (fp); fp->_flags &= ~__SEOF; - _funlockfile (fp); return 0; } @@ -338,7 +290,6 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), fp->_p += n; fp->_r -= n; } - _funlockfile (fp); return 0; /* @@ -348,10 +299,7 @@ _DEFUN(_fseek_r, (ptr, fp, offset, whence), dumb: if (fflush (fp) || (*seekfn) (fp->_cookie, offset, whence) == POS_ERR) - { - _funlockfile (fp); - return EOF; - } + return EOF; /* success: clear EOF indicator and discard ungetc() data */ if (HASUB (fp)) FREEUB (fp); @@ -359,26 +307,5 @@ dumb: fp->_r = 0; /* fp->_w = 0; *//* unnecessary (I think...) */ fp->_flags &= ~__SEOF; - /* Reset no-optimization flag after successful seek. The - no-optimization flag may be set in the case of a read - stream that is flushed which by POSIX/SUSv3 standards, - means that a corresponding seek must not optimize. The - optimization is then allowed if no subsequent flush - is performed. */ - fp->_flags &= ~__SNPT; - _funlockfile (fp); return 0; } - -#ifndef _REENT_ONLY - -int -_DEFUN(fseek, (fp, offset, whence), - register FILE *fp _AND - long offset _AND - int whence) -{ - return _fseek_r (_REENT, fp, offset, whence); -} - -#endif /* !_REENT_ONLY */ |