OSDN Git Service

Update copyright for the year 2010.
[pg-rex/syncrep.git] / src / port / fseeko.c
1 /*-------------------------------------------------------------------------
2  *
3  * fseeko.c
4  *        64-bit versions of fseeko/ftello()
5  *
6  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/port/fseeko.c,v 1.24 2010/01/02 16:58:13 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 /*
17  * We have to use the native defines here because configure hasn't
18  * completed yet.
19  */
20 #if defined(__bsdi__) || defined(__NetBSD__)
21
22 #include "c.h"
23
24 #ifdef bsdi
25 #include <pthread.h>
26 #endif
27 #include <sys/stat.h>
28
29
30 /*
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.
34  *
35  *      This is thread-safe on BSD/OS using flockfile/funlockfile.
36  */
37
38 int
39 fseeko(FILE *stream, off_t offset, int whence)
40 {
41         off_t           floc;
42         struct stat filestat;
43
44         switch (whence)
45         {
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;
84         }
85
86 failure:
87 #ifdef bsdi
88         funlockfile(stream);
89 #endif
90         return -1;
91 }
92
93
94 off_t
95 ftello(FILE *stream)
96 {
97         off_t           floc;
98
99         if (fgetpos(stream, &floc) != 0)
100                 return -1;
101         return floc;
102 }
103
104 #endif