Header And Logo

PostgreSQL
| The world's most advanced open source database.

fseeko.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * fseeko.c
00004  *    64-bit versions of fseeko/ftello()
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/port/fseeko.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 
00016 /*
00017  * We have to use the native defines here because configure hasn't
00018  * completed yet.
00019  */
00020 #ifdef __NetBSD__
00021 
00022 #include "c.h"
00023 
00024 #include <sys/stat.h>
00025 
00026 
00027 /*
00028  *  On NetBSD, off_t and fpos_t are the same.  Standards
00029  *  say off_t is an arithmetic type, but not necessarily integral,
00030  *  while fpos_t might be neither.
00031  */
00032 
00033 int
00034 fseeko(FILE *stream, off_t offset, int whence)
00035 {
00036     off_t       floc;
00037     struct stat filestat;
00038 
00039     switch (whence)
00040     {
00041         case SEEK_CUR:
00042             if (fgetpos(stream, &floc) != 0)
00043                 goto failure;
00044             floc += offset;
00045             if (fsetpos(stream, &floc) != 0)
00046                 goto failure;
00047             return 0;
00048             break;
00049         case SEEK_SET:
00050             if (fsetpos(stream, &offset) != 0)
00051                 return -1;
00052             return 0;
00053             break;
00054         case SEEK_END:
00055             fflush(stream);     /* force writes to fd for stat() */
00056             if (fstat(fileno(stream), &filestat) != 0)
00057                 goto failure;
00058             floc = filestat.st_size;
00059             floc += offset;
00060             if (fsetpos(stream, &floc) != 0)
00061                 goto failure;
00062             return 0;
00063             break;
00064         default:
00065             errno = EINVAL;
00066             return -1;
00067     }
00068 
00069 failure:
00070     return -1;
00071 }
00072 
00073 
00074 off_t
00075 ftello(FILE *stream)
00076 {
00077     off_t       floc;
00078 
00079     if (fgetpos(stream, &floc) != 0)
00080         return -1;
00081     return floc;
00082 }
00083 
00084 #endif