2 * Sherlock Library -- Large File Support
4 * (c) 1999--2001 Martin Mares <mj@ucw.cz>
7 #ifndef _SHERLOCK_LFS_H
8 #define _SHERLOCK_LFS_H
13 #ifdef SHERLOCK_CONFIG_LFS
15 #ifdef SHERLOCK_CONFIG_LFS_LIBC
18 * Unfortunately, we need to configure this manually since
19 * out-of-the-box glibc 2.1 offers the 64-bit calls, but
20 * converts them to 32-bit syscalls. Damn it!
23 #define sh_open open64
24 #define sh_seek lseek64
25 #define sh_pread pread64
26 #define sh_pwrite pwrite64
27 #define sh_ftruncate ftruncate64
28 #define SHERLOCK_HAVE_PREAD
33 * Talk directly with the kernel. The implementations of LFS in Linux 2.2
34 * and 2.4 differ, but fortunately for us only in things like stat64 which
35 * we don't need to use.
39 #if defined(__linux__) && defined(__i386__)
40 #define O_LARGEFILE 0100000
42 #error O_LARGEFILE unknown
47 sh_open(char *name, int flags, int mode)
49 return open(name, flags | O_LARGEFILE, mode);
54 /* A "do it yourself" solution */
56 #include <asm/unistd.h>
59 _syscall5(int, _llseek, int, fd, int, hi, int, lo, loff_t *, result, int, whence);
61 static inline loff_t sh_seek(int fd, sh_off_t pos, int whence)
66 err = _llseek(fd, pos >> 32, pos, &result, whence);
67 return (err < 0) ? err : result;
71 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
72 /* glibc 2.1 or newer -> has lseek64 */
73 #define sh_seek(f,o,w) lseek64(f,o,w)
75 /* Touching hidden places in glibc */
76 extern loff_t llseek(int fd, loff_t pos, int whence);
77 #define sh_seek(f,o,w) llseek(f,o,w)
82 #endif /* !SHERLOCK_CONFIG_LFS_LIBC */
84 #else /* !SHERLOCK_CONFIG_LFS */
87 #define sh_seek(f,o,w) lseek(f,o,w)
88 #define sh_ftruncate(f,o) ftruncate(f,o)
90 #endif /* !SHERLOCK_CONFIG_LFS */
93 * We'd like to use pread/pwrite for our file accesses, but unfortunately it
94 * isn't simple at all since all libc's until glibc 2.1 don't define it.
97 #ifndef SHERLOCK_HAVE_PREAD
100 #define SHERLOCK_HAVE_PREAD
101 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
102 /* glibc 2.1 or newer -> pread/pwrite supported automatically */
103 #ifdef SHERLOCK_CONFIG_LFS
104 /* but have to use the 64-bit versions explicitly */
105 #define sh_pread pread64
106 #define sh_pwrite pwrite64
108 #define sh_pread pread
109 #define sh_pwrite pwrite
111 #elif defined(i386) && defined(__GLIBC__)
112 /* glibc 2.0 on i386 -> call syscalls directly */
113 #include <asm/unistd.h>
114 #include <syscall-list.h>
115 #include <sys/types.h>
118 #define SYS_pread 180
120 static int sh_pread(unsigned int fd, void *buf, size_t size, loff_t where)
121 { return syscall(SYS_pread, fd, buf, size, where); }
123 #define SYS_pwrite 181
125 static int sh_pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
126 { return syscall(SYS_pwrite, fd, buf, size, where); }
128 /* old libc on i386 -> call syscalls directly the old way */
129 #include <asm/unistd.h>
131 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
132 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
133 #define sh_pread pread
134 #define sh_pwrite pwrite
136 #undef SHERLOCK_HAVE_PREAD
138 #endif /* __linux__ */
140 #endif /* !SHERLOCK_HAVE_PREAD */
142 #endif /* !_SHERLOCK_LFS_H */