blob: 658acc97cabcb61a3632aefc97b6c24c5683552e [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * llseek.c -- stub calling the llseek system call
3 *
4 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 * %End-Header%
10 */
11
12#define _LARGEFILE_SOURCE
13#define _LARGEFILE64_SOURCE
14
Theodore Ts'od1154eb2011-09-18 17:34:37 -040015#include "config.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050016#if HAVE_SYS_TYPES_H
17#include <sys/types.h>
18#endif
19
20#if HAVE_ERRNO_H
21#include <errno.h>
22#endif
23#if HAVE_UNISTD_H
24#include <unistd.h>
25#endif
26#ifdef __MSDOS__
27#include <io.h>
28#endif
29
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050030#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050031
32#ifdef __linux__
33
34#if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE)
35
36#define my_llseek lseek64
37
38#elif defined(HAVE_LLSEEK)
39#include <syscall.h>
40
41#ifndef HAVE_LLSEEK_PROTOTYPE
42extern long long llseek(int fd, long long offset, int origin);
43#endif
44
45#define my_llseek llseek
46
47#else /* ! HAVE_LLSEEK */
48
Theodore Ts'oc2dbc182008-01-27 00:35:32 -050049#if SIZEOF_LONG == SIZEOF_LONG_LONG
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050050
51#define llseek lseek
52
Theodore Ts'oc2dbc182008-01-27 00:35:32 -050053#else /* SIZEOF_LONG != SIZEOF_LONG_LONG */
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050054
55#include <linux/unistd.h>
56
57#ifndef __NR__llseek
58#define __NR__llseek 140
59#endif
60
61#ifndef __i386__
62static int _llseek(unsigned int, unsigned long, unsigned long,
63 blkid_loff_t *, unsigned int);
64
65static _syscall5(int, _llseek, unsigned int, fd, unsigned long, offset_high,
66 unsigned long, offset_low, blkid_loff_t *, result,
67 unsigned int, origin)
68#endif
69
70static blkid_loff_t my_llseek(int fd, blkid_loff_t offset, int origin)
71{
72 blkid_loff_t result;
73 int retval;
74
75#ifndef __i386__
76 retval = _llseek(fd, ((unsigned long long) offset) >> 32,
77 ((unsigned long long)offset) & 0xffffffff,
78 &result, origin);
79#else
80 retval = syscall(__NR__llseek, fd, ((unsigned long long) offset) >> 32,
81 ((unsigned long long)offset) & 0xffffffff,
82 &result, origin);
83#endif
84 return (retval == -1 ? (blkid_loff_t) retval : result);
85}
86
87#endif /* __alpha__ || __ia64__ */
88
89#endif /* HAVE_LLSEEK */
90
91blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int whence)
92{
93 blkid_loff_t result;
94 static int do_compat = 0;
95
96 if ((sizeof(off_t) >= sizeof(blkid_loff_t)) ||
97 (offset < ((blkid_loff_t) 1 << ((sizeof(off_t)*8) -1))))
98 return lseek(fd, (off_t) offset, whence);
99
100 if (do_compat) {
101 errno = EOVERFLOW;
102 return -1;
103 }
104
105 result = my_llseek(fd, offset, whence);
106 if (result == -1 && errno == ENOSYS) {
107 /*
108 * Just in case this code runs on top of an old kernel
109 * which does not support the llseek system call
110 */
111 do_compat++;
112 errno = EOVERFLOW;
113 }
114 return result;
115}
116
117#else /* !linux */
118
119#ifndef EOVERFLOW
120#ifdef EXT2_ET_INVALID_ARGUMENT
121#define EOVERFLOW EXT2_ET_INVALID_ARGUMENT
122#else
123#define EOVERFLOW 112
124#endif
125#endif
126
127blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int origin)
128{
Theodore Ts'o488f3c22004-09-17 17:47:12 -0400129#if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE)
130 return lseek64 (fd, offset, origin);
131#else
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500132 if ((sizeof(off_t) < sizeof(blkid_loff_t)) &&
133 (offset >= ((blkid_loff_t) 1 << ((sizeof(off_t)*8) - 1)))) {
134 errno = EOVERFLOW;
135 return -1;
136 }
137 return lseek(fd, (off_t) offset, origin);
Theodore Ts'o488f3c22004-09-17 17:47:12 -0400138#endif
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500139}
140
141#endif /* linux */
142
143