Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / src / port / fseeko.c
blob85a5baa8f2724ab486f73c799506998758904fb9
1 /*-------------------------------------------------------------------------
3 * fseeko.c
4 * 64-bit versions of fseeko/ftello()
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
17 * We have to use the native defines here because configure hasn't
18 * completed yet.
20 #if defined(__bsdi__) || defined(__NetBSD__)
22 #include "c.h"
24 #ifdef bsdi
25 #include <pthread.h>
26 #endif
27 #include <sys/stat.h>
31 * On BSD/OS and NetBSD, off_t and fpos_t are the same. Standards
32 * say off_t is an arithmetic type, but not necessarily integral,
33 * while fpos_t might be neither.
35 * This is thread-safe on BSD/OS using flockfile/funlockfile.
38 int
39 fseeko(FILE *stream, off_t offset, int whence)
41 off_t floc;
42 struct stat filestat;
44 switch (whence)
46 case SEEK_CUR:
47 #ifdef bsdi
48 flockfile(stream);
49 #endif
50 if (fgetpos(stream, &floc) != 0)
51 goto failure;
52 floc += offset;
53 if (fsetpos(stream, &floc) != 0)
54 goto failure;
55 #ifdef bsdi
56 funlockfile(stream);
57 #endif
58 return 0;
59 break;
60 case SEEK_SET:
61 if (fsetpos(stream, &offset) != 0)
62 return -1;
63 return 0;
64 break;
65 case SEEK_END:
66 #ifdef bsdi
67 flockfile(stream);
68 #endif
69 fflush(stream); /* force writes to fd for stat() */
70 if (fstat(fileno(stream), &filestat) != 0)
71 goto failure;
72 floc = filestat.st_size;
73 floc += offset;
74 if (fsetpos(stream, &floc) != 0)
75 goto failure;
76 #ifdef bsdi
77 funlockfile(stream);
78 #endif
79 return 0;
80 break;
81 default:
82 errno = EINVAL;
83 return -1;
86 failure:
87 #ifdef bsdi
88 funlockfile(stream);
89 #endif
90 return -1;
94 off_t
95 ftello(FILE *stream)
97 off_t floc;
99 if (fgetpos(stream, &floc) != 0)
100 return -1;
101 return floc;
104 #endif