]> mj.ucw.cz Git - libucw.git/blob - lib/lfs.h
Added ABS macro.
[libucw.git] / lib / lfs.h
1 /*
2  *      Sherlock Library -- Large File Support
3  *
4  *      (c) 1999--2001 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _SHERLOCK_LFS_H
8 #define _SHERLOCK_LFS_H
9
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 #ifdef SHERLOCK_CONFIG_LFS
14
15 #ifdef SHERLOCK_CONFIG_LFS_LIBC
16
17 /*
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!
21  */
22
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 sh_mmap(a,l,p,f,d,o) mmap64(a,l,p,f,d,o)
29 #define SHERLOCK_HAVE_PREAD
30
31 #else
32
33 #error Non-libc interface to LFS is currently broken, you have to fix it.
34
35 /*
36  *  Talk directly with the kernel. The implementations of LFS in Linux 2.2
37  *  and 2.4 differ, but fortunately for us only in things like stat64 which
38  *  we don't need to use.
39  */
40
41 #ifndef O_LARGEFILE
42 #if defined(__linux__) && defined(__i386__)
43 #define O_LARGEFILE 0100000
44 #else
45 #error O_LARGEFILE unknown
46 #endif
47 #endif
48
49 static inline int
50 sh_open(char *name, int flags, int mode)
51 {
52   return open(name, flags | O_LARGEFILE, mode);
53 }
54
55 #if 0
56
57 /* A "do it yourself" solution */
58
59 #include <asm/unistd.h>
60 #include <errno.h>
61
62 _syscall5(int, _llseek, int, fd, int, hi, int, lo, loff_t *, result, int, whence);
63
64 static inline loff_t sh_seek(int fd, sh_off_t pos, int whence)
65 {
66   loff_t result;
67   int err;
68
69   err = _llseek(fd, pos >> 32, pos, &result, whence);
70   return (err < 0) ? err : result;
71 }
72 #else
73
74 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
75 /* glibc 2.1 or newer -> has lseek64 */
76 #define sh_seek(f,o,w) lseek64(f,o,w)
77 #else
78 /* Touching hidden places in glibc */
79 extern loff_t llseek(int fd, loff_t pos, int whence);
80 #define sh_seek(f,o,w) llseek(f,o,w)
81 #endif
82
83 #endif
84
85 #endif  /* !SHERLOCK_CONFIG_LFS_LIBC */
86
87 #else   /* !SHERLOCK_CONFIG_LFS */
88
89 #define sh_open open
90 #define sh_seek(f,o,w) lseek(f,o,w)
91 #define sh_ftruncate(f,o) ftruncate(f,o)
92 #define sh_mmap(a,l,p,f,d,o) mmap(a,l,p,f,d,o)
93
94 #endif  /* !SHERLOCK_CONFIG_LFS */
95
96 /*
97  *  We'd like to use pread/pwrite for our file accesses, but unfortunately it
98  *  isn't simple at all since all libc's until glibc 2.1 don't define it.
99  */
100
101 #ifndef SHERLOCK_HAVE_PREAD
102
103 #ifdef __linux__
104 #define SHERLOCK_HAVE_PREAD
105 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
106 /* glibc 2.1 or newer -> pread/pwrite supported automatically */
107 #ifdef SHERLOCK_CONFIG_LFS
108 /* but have to use the 64-bit versions explicitly */
109 #define sh_pread pread64
110 #define sh_pwrite pwrite64
111 #else
112 #define sh_pread pread
113 #define sh_pwrite pwrite
114 #endif
115 #elif defined(i386) && defined(__GLIBC__)
116 /* glibc 2.0 on i386 -> call syscalls directly */
117 #include <asm/unistd.h>
118 #include <syscall-list.h>
119 #include <sys/types.h>
120 #include <unistd.h>
121 #ifndef SYS_pread
122 #define SYS_pread 180
123 #endif
124 static int sh_pread(unsigned int fd, void *buf, size_t size, loff_t where)
125 { return syscall(SYS_pread, fd, buf, size, where); }
126 #ifndef SYS_pwrite
127 #define SYS_pwrite 181
128 #endif
129 static int sh_pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
130 { return syscall(SYS_pwrite, fd, buf, size, where); }
131 #elif defined(i386)
132 /* old libc on i386 -> call syscalls directly the old way */
133 #include <asm/unistd.h>
134 #include <errno.h>
135 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
136 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
137 #define sh_pread pread
138 #define sh_pwrite pwrite
139 #else
140 #undef SHERLOCK_HAVE_PREAD
141 #endif
142 #endif  /* __linux__ */
143
144 #endif  /* !SHERLOCK_HAVE_PREAD */
145
146 #endif  /* !_SHERLOCK_LFS_H */