10l: comparison of char* ptrs with string literals
[mplayer.git] / osdep / fseeko.c
blob7d942a1c25c7c3fa207daf3b4faf15b1d7e90a2f
1 /*
2 * fseeko.c
3 * 64-bit versions of fseeko/ftello() for systems which do not have them
4 */
6 #include "config.h"
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <errno.h>
13 #ifdef WIN32
14 #define flockfile
15 #define funlockfile
16 #endif
19 * On BSD/OS and NetBSD (and perhaps others), off_t and fpos_t are the
20 * same. Standards say off_t is an arithmetic type, but not necessarily
21 * integral, while fpos_t might be neither.
23 * This is thread-safe on BSD/OS using flockfile/funlockfile.
26 int
27 fseeko(FILE *stream, off_t offset, int whence)
29 fpos_t floc;
30 struct stat filestat;
32 switch (whence)
34 case SEEK_CUR:
35 flockfile(stream);
36 if (fgetpos(stream, &floc) != 0)
37 goto failure;
38 floc += offset;
39 if (fsetpos(stream, &floc) != 0)
40 goto failure;
41 funlockfile(stream);
42 return 0;
43 break;
44 case SEEK_SET:
45 if (fsetpos(stream, &offset) != 0)
46 return -1;
47 return 0;
48 break;
49 case SEEK_END:
50 flockfile(stream);
51 if (fstat(fileno(stream), &filestat) != 0)
52 goto failure;
53 floc = filestat.st_size;
54 if (fsetpos(stream, &floc) != 0)
55 goto failure;
56 funlockfile(stream);
57 return 0;
58 break;
59 default:
60 errno = EINVAL;
61 return -1;
64 failure:
65 funlockfile(stream);
66 return -1;