2002-10-23 22:56:24 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* fseeko.c
|
|
|
|
* 64-bit versions of fseeko/ftello()
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-10-23 23:39:27 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/port/fseeko.c,v 1.3 2002/10/23 21:39:27 momjian Exp $
|
2002-10-23 22:56:24 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __bsdi__
|
|
|
|
|
2002-10-23 23:16:17 +02:00
|
|
|
#include <pthread.h>
|
2002-10-23 22:56:24 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On BSD/OS, off_t and fpos_t are the same. Standards say
|
|
|
|
* off_t is an arithmetic type, but not necessarily integral,
|
|
|
|
* while fpos_t might be neither.
|
|
|
|
*
|
2002-10-23 23:16:17 +02:00
|
|
|
* This is thread-safe using flockfile/funlockfile.
|
2002-10-23 22:56:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
fseeko(FILE *stream, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
off_t floc;
|
|
|
|
struct stat filestat;
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_CUR:
|
2002-10-23 23:16:17 +02:00
|
|
|
flockfile(stream);
|
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;
|
2002-10-23 23:16:17 +02:00
|
|
|
flockfile(stream);
|
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:
|
2002-10-23 23:16:17 +02:00
|
|
|
flockfile(stream);
|
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;
|
|
|
|
if (fsetpos(stream, &floc) != 0)
|
2002-10-23 23:39:27 +02:00
|
|
|
goto failure;
|
2002-10-23 23:16:17 +02:00
|
|
|
funlockfile(stream);
|
2002-10-23 22:56:24 +02:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2002-10-23 23:39:27 +02:00
|
|
|
|
|
|
|
failure:
|
|
|
|
funlockfile(stream);
|
|
|
|
return -1;
|
2002-10-23 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
off_t
|
|
|
|
ftello(FILE *stream)
|
|
|
|
{
|
|
|
|
off_t floc;
|
|
|
|
|
|
|
|
if (fgetpos(stream, &floc) != 0)
|
|
|
|
return -1;
|
|
|
|
return floc;
|
|
|
|
}
|
|
|
|
#endif
|