2002-10-23 22:56:24 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* fseeko.c
|
|
|
|
* 64-bit versions of fseeko/ftello()
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2002-10-23 22:56:24 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-03-05 16:59:11 +01:00
|
|
|
* $PostgreSQL: pgsql/src/port/fseeko.c,v 1.20 2006/03/05 15:59:10 momjian Exp $
|
2002-10-23 22:56:24 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2002-10-28 01:00:25 +01:00
|
|
|
/*
|
|
|
|
* We have to use the native defines here because configure hasn't
|
|
|
|
* completed yet.
|
|
|
|
*/
|
|
|
|
#if defined(__bsdi__) || defined(__NetBSD__)
|
2002-10-23 22:56:24 +02:00
|
|
|
|
2002-10-24 06:33:46 +02:00
|
|
|
#include "c.h"
|
|
|
|
|
2003-01-11 20:38:23 +01:00
|
|
|
#ifdef bsdi
|
2002-10-23 23:16:17 +02:00
|
|
|
#include <pthread.h>
|
2003-01-11 20:38:23 +01:00
|
|
|
#endif
|
2002-10-23 22:56:24 +02:00
|
|
|
#include <sys/stat.h>
|
2005-07-28 06:03:14 +02:00
|
|
|
|
2002-10-23 22:56:24 +02:00
|
|
|
|
|
|
|
/*
|
2002-10-24 05:11:05 +02:00
|
|
|
* On BSD/OS and NetBSD, off_t and fpos_t are the same. Standards
|
|
|
|
* say off_t is an arithmetic type, but not necessarily integral,
|
2002-10-23 22:56:24 +02:00
|
|
|
* while fpos_t might be neither.
|
|
|
|
*
|
2002-10-24 05:11:05 +02:00
|
|
|
* This is thread-safe on BSD/OS using flockfile/funlockfile.
|
2002-10-23 22:56:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
fseeko(FILE *stream, off_t offset, int whence)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
off_t floc;
|
2002-10-23 22:56:24 +02:00
|
|
|
struct stat filestat;
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_CUR:
|
2003-01-11 20:38:23 +01:00
|
|
|
#ifdef bsdi
|
2002-10-23 23:16:17 +02:00
|
|
|
flockfile(stream);
|
2003-01-11 20:38:23 +01:00
|
|
|
#endif
|
2002-10-23 22:56:24 +02:00
|
|
|
if (fgetpos(stream, &floc) != 0)
|
2002-10-23 23:39:27 +02:00
|
|
|
goto failure;
|
2002-10-23 22:56:24 +02:00
|
|
|
floc += offset;
|
|
|
|
if (fsetpos(stream, &floc) != 0)
|
2002-10-23 23:39:27 +02:00
|
|
|
goto failure;
|
2003-01-11 20:38:23 +01:00
|
|
|
#ifdef bsdi
|
2003-01-02 07:00:33 +01:00
|
|
|
funlockfile(stream);
|
2003-01-11 20:38:23 +01:00
|
|
|
#endif
|
2002-10-23 22:56:24 +02:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
if (fsetpos(stream, &offset) != 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2003-01-11 20:38:23 +01:00
|
|
|
#ifdef bsdi
|
2002-10-23 23:16:17 +02:00
|
|
|
flockfile(stream);
|
2003-01-11 20:38:23 +01:00
|
|
|
#endif
|
2004-08-29 07:07:03 +02:00
|
|
|
fflush(stream); /* force writes to fd for stat() */
|
2002-10-23 22:56:24 +02:00
|
|
|
if (fstat(fileno(stream), &filestat) != 0)
|
2002-10-23 23:39:27 +02:00
|
|
|
goto failure;
|
2002-10-23 22:56:24 +02:00
|
|
|
floc = filestat.st_size;
|
2005-08-12 01:05:14 +02:00
|
|
|
floc += offset;
|
2002-10-23 22:56:24 +02:00
|
|
|
if (fsetpos(stream, &floc) != 0)
|
2002-10-23 23:39:27 +02:00
|
|
|
goto failure;
|
2003-01-11 20:38:23 +01:00
|
|
|
#ifdef bsdi
|
2002-10-23 23:16:17 +02:00
|
|
|
funlockfile(stream);
|
2003-01-11 20:38:23 +01:00
|
|
|
#endif
|
2002-10-23 22:56:24 +02:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
2003-08-04 02:43:34 +02:00
|
|
|
errno = EINVAL;
|
2002-10-23 22:56:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2002-10-23 23:39:27 +02:00
|
|
|
|
|
|
|
failure:
|
2003-01-11 20:38:23 +01:00
|
|
|
#ifdef bsdi
|
2002-10-23 23:39:27 +02:00
|
|
|
funlockfile(stream);
|
2003-01-11 20:38:23 +01:00
|
|
|
#endif
|
2002-10-23 23:39:27 +02:00
|
|
|
return -1;
|
2002-10-23 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
off_t
|
|
|
|
ftello(FILE *stream)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
off_t floc;
|
2002-10-23 22:56:24 +02:00
|
|
|
|
|
|
|
if (fgetpos(stream, &floc) != 0)
|
|
|
|
return -1;
|
|
|
|
return floc;
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2002-10-23 22:56:24 +02:00
|
|
|
#endif
|