]> granicus.if.org Git - postgresql/blob - src/port/fseeko.c
Make psql reject attempts to set special variables to invalid values.
[postgresql] / src / port / fseeko.c
1 /*-------------------------------------------------------------------------
2  *
3  * fseeko.c
4  *        64-bit versions of fseeko/ftello()
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/port/fseeko.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 /*
17  * We have to use the native defines here because configure hasn't
18  * completed yet.
19  */
20 #ifdef __NetBSD__
21
22 #include "c.h"
23
24 #include <sys/stat.h>
25
26
27 /*
28  *      On NetBSD, off_t and fpos_t are the same.  Standards
29  *      say off_t is an arithmetic type, but not necessarily integral,
30  *      while fpos_t might be neither.
31  */
32
33 int
34 fseeko(FILE *stream, off_t offset, int whence)
35 {
36         off_t           floc;
37         struct stat filestat;
38
39         switch (whence)
40         {
41                 case SEEK_CUR:
42                         if (fgetpos(stream, &floc) != 0)
43                                 goto failure;
44                         floc += offset;
45                         if (fsetpos(stream, &floc) != 0)
46                                 goto failure;
47                         return 0;
48                         break;
49                 case SEEK_SET:
50                         if (fsetpos(stream, &offset) != 0)
51                                 return -1;
52                         return 0;
53                         break;
54                 case SEEK_END:
55                         fflush(stream);         /* force writes to fd for stat() */
56                         if (fstat(fileno(stream), &filestat) != 0)
57                                 goto failure;
58                         floc = filestat.st_size;
59                         floc += offset;
60                         if (fsetpos(stream, &floc) != 0)
61                                 goto failure;
62                         return 0;
63                         break;
64                 default:
65                         errno = EINVAL;
66                         return -1;
67         }
68
69 failure:
70         return -1;
71 }
72
73
74 off_t
75 ftello(FILE *stream)
76 {
77         off_t           floc;
78
79         if (fgetpos(stream, &floc) != 0)
80                 return -1;
81         return floc;
82 }
83
84 #endif