comparison osdep/fseeko.c @ 12071:ab3590ad2101

fseeko emulation patch by Steven M. Schultz <sms at 2bsd.com>
author faust3
date Fri, 26 Mar 2004 15:21:44 +0000
parents
children cfe440920be2
comparison
equal deleted inserted replaced
12070:070fc453a20b 12071:ab3590ad2101
1 /*
2 * fseeko.c
3 * 64-bit versions of fseeko/ftello() for systems which do not have them
4 */
5
6 #include "../config.h"
7
8 #if !defined(HAVE_FSEEKO) || !defined(HAVE_FTELLO)
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <errno.h>
13 #endif
14
15 #ifdef WIN32
16 #define flockfile
17 #define funlockfile
18 #endif
19
20 /*
21 * On BSD/OS and NetBSD (and perhaps others), off_t and fpos_t are the
22 * same. Standards say off_t is an arithmetic type, but not necessarily
23 * integral, while fpos_t might be neither.
24 *
25 * This is thread-safe on BSD/OS using flockfile/funlockfile.
26 */
27
28 #ifndef HAVE_FSEEKO
29 int
30 fseeko(FILE *stream, off_t offset, int whence)
31 {
32 off_t floc;
33 struct stat filestat;
34
35 switch (whence)
36 {
37 case SEEK_CUR:
38 flockfile(stream);
39 if (fgetpos(stream, &floc) != 0)
40 goto failure;
41 floc += offset;
42 if (fsetpos(stream, &floc) != 0)
43 goto failure;
44 funlockfile(stream);
45 return 0;
46 break;
47 case SEEK_SET:
48 if (fsetpos(stream, &offset) != 0)
49 return -1;
50 return 0;
51 break;
52 case SEEK_END:
53 flockfile(stream);
54 if (fstat(fileno(stream), &filestat) != 0)
55 goto failure;
56 floc = filestat.st_size;
57 if (fsetpos(stream, &floc) != 0)
58 goto failure;
59 funlockfile(stream);
60 return 0;
61 break;
62 default:
63 errno = EINVAL;
64 return -1;
65 }
66
67 failure:
68 funlockfile(stream);
69 return -1;
70 }
71 #endif
72
73
74 #ifndef HAVE_FTELLO
75 off_t
76 ftello(FILE *stream)
77 {
78 off_t floc;
79
80 if (fgetpos(stream, &floc) != 0)
81 return -1;
82 return floc;
83 }
84 #endif