XCU5: Add fseeko() and ftello() functions which provide the functionality of

fseek() and ftell(), respectively, but operate on file offsets of type off_t.
This commit is contained in:
kleink 2000-07-08 13:46:33 +00:00
parent 2f244ea9d9
commit dae360611f
9 changed files with 512 additions and 11 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: stdio.h,v 1.33 2000/06/26 15:52:36 kleink Exp $ */
/* $NetBSD: stdio.h,v 1.34 2000/07/08 13:46:33 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -328,6 +328,20 @@ char *tempnam __P((const char *, const char *));
__END_DECLS
#endif
/*
* X/Open CAE Specification Issue 5 Version 2
*/
#if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || \
(_XOPEN_SOURCE - 0) >= 500 || defined(_LARGEFILE_SOURCE)
#ifndef off_t
typedef __off_t off_t;
#define off_t off_t
#endif 7* off_t */
int fseeko __P((FILE *, off_t, int));
off_t ftello __P((FILE *));
#endif /* (!_POSIX_SOURCE && !_XOPEN_SOURCE) || ... */
/*
* Routines that are purely local.
*/

View File

@ -1,4 +1,4 @@
/* $NetBSD: namespace.h,v 1.56 2000/07/05 12:03:50 kleink Exp $ */
/* $NetBSD: namespace.h,v 1.57 2000/07/08 13:46:34 kleink Exp $ */
/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@ -45,6 +45,8 @@
#define err _err
#define errx _errx
#define fork _fork
#define fseeko _fseeko
#define ftello _ftello
#define inet_aton _inet_aton
#define inet_pton _inet_pton
#define sbrk _sbrk

View File

@ -1,4 +1,4 @@
# $NetBSD: shlib_version,v 1.92 2000/07/05 16:08:28 veego Exp $
# $NetBSD: shlib_version,v 1.93 2000/07/08 13:46:34 kleink Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing
#
# things we wish to do on next major version bump:
@ -6,4 +6,4 @@
# - libc/net: resolver update to BIND8/9?
#
major=12
minor=63
minor=64

View File

@ -1,5 +1,5 @@
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
# $NetBSD: Makefile.inc,v 1.18 1998/08/30 23:29:18 perry Exp $
# $NetBSD: Makefile.inc,v 1.19 2000/07/08 13:46:35 kleink Exp $
# stdio sources
.PATH: ${.CURDIR}/stdio
@ -9,10 +9,10 @@ CPPFLAGS+=-DFLOATING_POINT
SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \
fgetc.c fgetln.c fgetpos.c fgets.c fileno.c findfp.c flags.c \
fopen.c fprintf.c fpurge.c fputc.c fputs.c fread.c freopen.c \
fscanf.c fseek.c fsetpos.c ftell.c funopen.c fvwrite.c \
fwalk.c fwrite.c getc.c getchar.c gettemp.c getw.c makebuf.c \
mkdtemp.c mkstemp.c perror.c printf.c putc.c putchar.c puts.c \
putw.c refill.c remove.c rewind.c rget.c scanf.c setbuf.c \
fscanf.c fseek.c fseeko.c fsetpos.c ftell.c ftello.c funopen.c \
fvwrite.c fwalk.c fwrite.c getc.c getchar.c gettemp.c getw.c \
makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c putchar.c \
puts.c putw.c refill.c remove.c rewind.c rget.c scanf.c setbuf.c \
setbuffer.c setvbuf.c snprintf.c sscanf.c stdio.c tmpfile.c \
ungetc.c vasprintf.c vfprintf.c vfscanf.c vprintf.c vscanf.c \
vsnprintf.c vsscanf.c wbuf.c wsetup.c
@ -21,6 +21,9 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \
SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
.endif
# namespace purity wrappers
SRCS+= _fseeko.c _ftello.c
MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fopen.3 fputs.3 \
fread.3 fseek.3 funopen.3 getc.3 mktemp.3 perror.3 printf.3 putc.3 \
remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 ungetc.3
@ -31,7 +34,8 @@ MLINKS+=fgets.3 gets.3
MLINKS+=fopen.3 fdopen.3 fopen.3 freopen.3
MLINKS+=fputs.3 puts.3
MLINKS+=fread.3 fwrite.3
MLINKS+=fseek.3 fgetpos.3 fseek.3 fsetpos.3 fseek.3 ftell.3 fseek.3 rewind.3
MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.3 \
fseek.3 ftello.3 fseek.3 rewind.3
MLINKS+=funopen.3 fropen.3 funopen.3 fwopen.3
MLINKS+=getc.3 fgetc.3 getc.3 getchar.3 getc.3 getw.3
MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3

49
lib/libc/stdio/_fseeko.c Normal file
View File

@ -0,0 +1,49 @@
/* $NetBSD: _fseeko.c,v 1.1 2000/07/08 13:46:35 kleink Exp $ */
/*
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christos Zoulas.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifdef __indr_reference
__indr_reference(_fseeko, fseeko)
#else
#include <stdio.h>
int _fseeko __P((FILE *, off_t, int)); /* XXX */
int
fseeko(stream, offset, whence)
FILE *stream;
off_t offset;
int whence;
{
return _fseeko(stream, offset, whence);
}
#endif

47
lib/libc/stdio/_ftello.c Normal file
View File

@ -0,0 +1,47 @@
/* $NetBSD: _ftello.c,v 1.1 2000/07/08 13:46:35 kleink Exp $ */
/*
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christos Zoulas.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifdef __indr_reference
__indr_reference(_ftello, ftello)
#else
#include <stdio.h>
off_t _ftello __P((FILE *)); /* XXX */
off_t
ftello(stream)
FILE *stream;
{
return _ftello(stream);
}
#endif

View File

@ -1,4 +1,4 @@
.\" $NetBSD: fseek.3,v 1.12 1999/03/08 10:27:34 kleink Exp $
.\" $NetBSD: fseek.3,v 1.13 2000/07/08 13:46:35 kleink Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -43,8 +43,10 @@
.Sh NAME
.Nm fgetpos ,
.Nm fseek ,
.Nm fseeko ,
.Nm fsetpos ,
.Nm ftell ,
.Nm ftello ,
.Nm rewind
.Nd reposition a stream
.Sh LIBRARY
@ -53,8 +55,12 @@
.Fd #include <stdio.h>
.Ft int
.Fn fseek "FILE *stream" "long offset" "int whence"
.Ft int
.Fn fseeko "FILE *stream" "off_t offset" "int whence"
.Ft long
.Fn ftell "FILE *stream"
.Ft off_t
.Fn ftell "FILE *stream"
.Ft void
.Fn rewind "FILE *stream"
.Ft int
@ -197,3 +203,9 @@ and
functions
conform to
.St -ansiC .
The
.Fn fseeko
and
.Fn ftello
functions conform to
.St -xcu5 .

271
lib/libc/stdio/fseeko.c Normal file
View File

@ -0,0 +1,271 @@
/* $NetBSD: fseeko.c,v 1.1 2000/07/08 13:46:35 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: fseeko.c,v 1.1 2000/07/08 13:46:35 kleink Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "local.h"
#include "reentrant.h"
#define POS_ERR (-(fpos_t)1)
/*
* Seek the given file to the given offset.
* `Whence' must be one of the three SEEK_* macros.
*/
int
fseeko(fp, offset, whence)
FILE *fp;
off_t offset;
int whence;
{
fpos_t (*seekfn) __P((void *, fpos_t, int));
fpos_t target, curoff;
size_t n;
struct stat st;
int havepos;
_DIAGASSERT(fp != NULL);
#ifdef __GNUC__
/* This outrageous construct just to shut up a GCC warning. */
(void) &curoff;
#endif
/* make sure stdio is set up */
if (!__sdidinit)
__sinit();
FLOCKFILE(fp);
/*
* Have to be able to seek.
*/
if ((seekfn = fp->_seek) == NULL) {
errno = ESPIPE; /* historic practice */
FUNLOCKFILE(fp);
return (-1);
}
/*
* Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
* After this, whence is either SEEK_SET or SEEK_END.
*/
switch (whence) {
case SEEK_CUR:
/*
* In order to seek relative to the current stream offset,
* we have to first find the current stream offset a la
* ftell (see ftell for details).
*/
__sflush(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);
if (curoff == POS_ERR) {
FUNLOCKFILE(fp);
return (-1);
}
}
if (fp->_flags & __SRD) {
curoff -= fp->_r;
if (HASUB(fp))
curoff -= fp->_ur;
} else if (fp->_flags & __SWR && fp->_p != NULL)
curoff += fp->_p - fp->_bf._base;
offset += curoff;
whence = SEEK_SET;
havepos = 1;
break;
case SEEK_SET:
case SEEK_END:
curoff = 0; /* XXX just to keep gcc quiet */
havepos = 0;
break;
default:
errno = EINVAL;
FUNLOCKFILE(fp);
return (-1);
}
/*
* Can only optimise if:
* reading (and not reading-and-writing);
* not unbuffered; and
* this is a `regular' Unix file (and hence seekfn==__sseek).
* We must check __NBF first, because it is possible to have __NBF
* and __SOPT both set.
*/
if (fp->_bf._base == NULL)
__smakebuf(fp);
if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
goto dumb;
if ((fp->_flags & __SOPT) == 0) {
if (seekfn != __sseek ||
fp->_file < 0 || fstat(fp->_file, &st) ||
!S_ISREG(st.st_mode)) {
fp->_flags |= __SNPT;
goto dumb;
}
fp->_blksize = st.st_blksize;
fp->_flags |= __SOPT;
}
/*
* We are reading; we can try to optimise.
* Figure out where we are going and where we are now.
*/
if (whence == SEEK_SET)
target = offset;
else {
if (fstat(fp->_file, &st))
goto dumb;
target = st.st_size + offset;
}
if (!havepos) {
if (fp->_flags & __SOFF)
curoff = fp->_offset;
else {
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
if (curoff == POS_ERR)
goto dumb;
}
curoff -= fp->_r;
if (HASUB(fp))
curoff -= fp->_ur;
}
/*
* Compute the number of bytes in the input buffer (pretending
* that any ungetc() input has been discarded). Adjust current
* offset backwards by this count so that it represents the
* file offset for the first byte in the current input buffer.
*/
if (HASUB(fp)) {
curoff += fp->_r; /* kill off ungetc */
n = fp->_up - fp->_bf._base;
curoff -= n;
n += fp->_ur;
} else {
n = fp->_p - fp->_bf._base;
curoff -= n;
n += fp->_r;
}
/*
* If the target offset is within the current buffer,
* simply adjust the pointers, clear EOF, undo ungetc(),
* and return. (If the buffer was modified, we have to
* skip this; see fgetln.c.)
*/
if ((fp->_flags & __SMOD) == 0 &&
target >= curoff && target < curoff + n) {
int o = (int)(target - curoff);
fp->_p = fp->_bf._base + o;
fp->_r = n - o;
if (HASUB(fp))
FREEUB(fp);
fp->_flags &= ~__SEOF;
FUNLOCKFILE(fp);
return (0);
}
/*
* The place we want to get to is not within the current buffer,
* but we can still be kind to the kernel copyout mechanism.
* By aligning the file offset to a block boundary, we can let
* the kernel use the VM hardware to map pages instead of
* copying bytes laboriously. Using a block boundary also
* ensures that we only read one block, rather than two.
*/
curoff = target & ~(fp->_blksize - 1);
if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
goto dumb;
fp->_r = 0;
fp->_p = fp->_bf._base;
if (HASUB(fp))
FREEUB(fp);
fp->_flags &= ~__SEOF;
n = (int)(target - curoff);
if (n) {
if (__srefill(fp) || fp->_r < n)
goto dumb;
fp->_p += n;
fp->_r -= n;
}
FUNLOCKFILE(fp);
return (0);
/*
* We get here if we cannot optimise the seek ... just
* do it. Allow the seek function to change fp->_bf._base.
*/
dumb:
if (__sflush(fp) ||
(*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
FUNLOCKFILE(fp);
return (-1);
}
/* success: clear EOF indicator and discard ungetc() data */
if (HASUB(fp))
FREEUB(fp);
fp->_p = fp->_bf._base;
fp->_r = 0;
/* fp->_w = 0; */ /* unnecessary (I think...) */
fp->_flags &= ~__SEOF;
FUNLOCKFILE(fp);
return (0);
}

102
lib/libc/stdio/ftello.c Normal file
View File

@ -0,0 +1,102 @@
/* $NetBSD: ftello.c,v 1.1 2000/07/08 13:46:35 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: ftello.c,v 1.1 2000/07/08 13:46:35 kleink Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include "local.h"
#include "reentrant.h"
/*
* ftell: return current offset.
*/
off_t
ftello(fp)
FILE *fp;
{
fpos_t pos;
FLOCKFILE(fp);
if (fp->_seek == NULL) {
FUNLOCKFILE(fp);
errno = ESPIPE; /* historic practice */
return ((off_t)-1);
}
/*
* Find offset of underlying I/O object, then
* adjust for buffered bytes.
*/
__sflush(fp); /* may adjust seek offset on append stream */
if (fp->_flags & __SOFF)
pos = fp->_offset;
else {
pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR);
if (pos == (fpos_t)-1) {
FUNLOCKFILE(fp);
return (pos);
}
}
if (fp->_flags & __SRD) {
/*
* Reading. Any unread characters (including
* those from ungetc) cause the position to be
* smaller than that in the underlying object.
*/
pos -= fp->_r;
if (HASUB(fp))
pos -= fp->_ur;
} else if (fp->_flags & __SWR && fp->_p != NULL) {
/*
* Writing. Any buffered characters cause the
* position to be greater than that in the
* underlying object.
*/
pos += fp->_p - fp->_bf._base;
}
FUNLOCKFILE(fp);
return (pos);
}