]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/fcntl.c
Update.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / fcntl.c
1 /* Copyright (C) 2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdarg.h>
23
24 #include <sysdep.h>
25 #include <sys/syscall.h>
26 #include "../kernel-features.h"
27
28 #undef __libc_fcntl
29 #undef __fcntl
30
31 extern int __syscall_fcntl (int __fd, int __cmd, ...);
32 #ifdef __NR_fcntl64
33 extern int __syscall_fcntl64 (int __fd, int __cmd, ...);
34 #endif
35
36 #if __ASSUME_FCNTL64 == 0
37 /* This variable is shared with all files that check for fcntl64. */
38 int __have_no_fcntl64;
39 #endif
40
41 int
42 __libc_fcntl (int fd, int cmd, ...)
43 {
44 va_list ap;
45 void *arg;
46
47 va_start (ap, cmd);
48 arg = va_arg (ap, void *);
49 va_end (ap);
50
51 #if __ASSUME_FCNTL64 > 0
52 return INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
53 #else
54 # ifdef __NR_fcntl64
55 if (! __have_no_fcntl64)
56 {
57 int result = INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
58 if (result >= 0 || errno != ENOSYS)
59 return result;
60
61 __have_no_fcntl64 = 1;
62 }
63 # endif
64 switch (cmd)
65 {
66 case F_GETLK64:
67 /* Convert arg from flock64 to flock and back. */
68 {
69 struct flock fl;
70 struct flock64 *fl64 = arg;
71 int res;
72
73 fl.l_start = (off_t)fl64->l_start;
74 /* Check if we can represent the values with the smaller type. */
75 if ((off64_t) fl.l_start != fl64->l_start)
76 {
77 __set_errno (EOVERFLOW);
78 return -1;
79 }
80 fl.l_len = (off_t) fl64->l_len;
81 /* Check if we can represent the values with the smaller type. */
82 if ((off64_t) fl.l_len != fl64->l_len)
83 {
84 __set_errno (EOVERFLOW);
85 return -1;
86 }
87 fl.l_type = fl64->l_type;
88 fl.l_whence = fl64->l_whence;
89 fl.l_pid = fl64->l_pid;
90
91 res = INLINE_SYSCALL (fcntl, 3, fd, F_GETLK, &fl);
92 if (res != 0)
93 return res;
94 /* Everything ok, convert back. */
95 fl64->l_type = fl.l_type;
96 fl64->l_whence = fl.l_whence;
97 fl64->l_start = fl.l_start;
98 fl64->l_len = fl.l_len;
99 fl64->l_pid = fl.l_pid;
100
101 return 0;
102 }
103 case F_SETLK64:
104 case F_SETLKW64:
105 /* Try to convert arg from flock64 to flock. */
106 {
107 struct flock fl;
108 struct flock64 *fl64 = arg;
109
110 fl.l_start = (off_t) fl64->l_start;
111 /* Check if we can represent the values with the smaller type. */
112 if ((off64_t) fl.l_start != fl64->l_start)
113 {
114 __set_errno (EOVERFLOW);
115 return -1;
116 }
117 fl.l_len = (off_t)fl64->l_len;
118 /* Check if we can represent the values with the smaller type. */
119 if ((off64_t) fl.l_len != fl64->l_len)
120 {
121 __set_errno (EOVERFLOW);
122 return -1;
123 }
124 fl.l_type = fl64->l_type;
125 fl.l_whence = fl64->l_whence;
126 fl.l_pid = fl64->l_pid;
127 assert (F_SETLK - F_SETLKW == F_SETLK64 - F_SETLKW64);
128 return INLINE_SYSCALL (fcntl, 3, fd, cmd + F_SETLK - F_SETLK64, &fl);
129 }
130 default:
131 return INLINE_SYSCALL (fcntl, 3, fd, cmd, arg);
132 }
133 return -1;
134 #endif /* __ASSUME_FCNTL64 */
135 }
136 INTDEF2(__libc_fcntl, __fcntl);
137
138 weak_alias (__libc_fcntl, __fcntl)
139 weak_alias (__libc_fcntl, fcntl)