small warning fix:
[mplayer/glamo.git] / osdep / fseeko.c
blob0ac11c29a751595167221bbb5121a83e0303fd4d
1 /*
2 * fseeko.c
3 * 64-bit versions of fseeko/ftello() for systems which do not have them
4 */
6 #include "../config.h"
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
15 #ifdef WIN32
16 #define flockfile
17 #define funlockfile
18 #endif
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.
25 * This is thread-safe on BSD/OS using flockfile/funlockfile.
28 #ifndef HAVE_FSEEKO
29 int
30 fseeko(FILE *stream, off_t offset, int whence)
32 fpos_t floc;
33 struct stat filestat;
35 switch (whence)
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;
67 failure:
68 funlockfile(stream);
69 return -1;
71 #endif
74 #ifndef HAVE_FTELLO
75 off_t
76 ftello(FILE *stream)
78 fpos_t floc;
80 if (fgetpos(stream, &floc) != 0)
81 return -1;
82 return floc;
84 #endif