Whamcloud - gitweb
d16efc49d9e21fdfbceb6e8d9fc27c6cd6021551
[fs/lustre-release.git] / libsysio / src / lseek.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2003 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <errno.h>
45 #include <unistd.h>
46 #include <assert.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/queue.h>
50
51 #include "sysio.h"
52 #include "inode.h"
53 #include "file.h"
54
55 #include "sysio-symbols.h"
56
57 static _SYSIO_OFF_T
58 _sysio_lseek(int fd, _SYSIO_OFF_T offset, int whence)
59 {
60         struct file *fil;
61         _SYSIO_OFF_T off, pos;
62         struct intnl_stat stbuf;
63         SYSIO_INTERFACE_DISPLAY_BLOCK;
64
65         SYSIO_INTERFACE_ENTER;
66         fil = _sysio_fd_find(fd);
67         if (!fil)
68                 return -EBADF;
69
70         off = -1;
71         switch (whence) {
72         
73         case SEEK_SET:
74                 off = 0;
75                 break;
76         case SEEK_CUR:
77                 off = fil->f_pos;
78                 break;
79         case SEEK_END:
80                 {
81                         int     err;
82
83                         err =
84                             (*fil->f_ino->i_ops.inop_getattr)(NULL,
85                                                               fil->f_ino,
86                                                               &stbuf);
87                         if (err)
88                                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )err);
89         
90                 }
91                 off = stbuf.st_size;
92                 break;
93         default:
94                 return -EINVAL;
95         }
96         pos = off + offset;
97         if ((offset < 0 && -offset > off) || (offset > 0 && pos <= off))
98                 SYSIO_INTERFACE_RETURN((off_t )-1, -EINVAL);
99
100 #ifdef O_LARGEFILE
101         if (pos >= ((fil->f_flags & O_LARGEFILE) ? _SYSIO_OFF_T_MAX : LONG_MAX))
102                 SYSIO_INTERFACE_RETURN((off_t )-1, -EOVERFLOW);
103 #else
104         if (pos >= _SYSIO_OFF_T_MAX)
105                 SYSIO_INTERFACE_RETURN((off_t )-1, -EOVERFLOW);
106 #endif
107         pos = (fil->f_ino->i_ops.inop_pos)(fil->f_ino, pos);
108         if (pos < 0)
109                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )pos);
110         fil->f_pos = pos;
111         SYSIO_INTERFACE_RETURN((off_t )pos, 0);
112 }
113
114 #if _LARGEFILE64_SOURCE
115 #undef lseek64
116 sysio_sym_weak_alias(_sysio_lseek, SYSIO_INTERFACE_NAME(lseek64))
117 #ifdef __GLIBC__
118 #undef __lseek64
119 sysio_sym_weak_alias(_sysio_lseek, PREPEND(__, SYSIO_INTERFACE_NAME(lseek64)))
120 #endif
121 #ifdef REDSTORM
122 #undef __libc_lseek64
123 sysio_sym_weak_alias(_sysio_lseek, 
124                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_lseek64)))
125 #endif
126 #endif
127
128 #undef lseek
129
130 extern off_t
131 SYSIO_INTERFACE_NAME(lseek)(int fd, off_t offset, int whence)
132 {
133         _SYSIO_OFF_T off;
134         off_t   rtn;
135         SYSIO_INTERFACE_DISPLAY_BLOCK;
136
137         SYSIO_INTERFACE_ENTER;
138         off = _sysio_lseek(fd, offset, whence);
139         if (off < 0)
140                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )off);
141         rtn = (off_t )off;
142         assert(rtn == off);
143         SYSIO_INTERFACE_RETURN(rtn, 0);
144 }
145
146 #ifdef __GLIBC__
147 #undef __lseek
148 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek),
149                      PREPEND(__, SYSIO_INTERFACE_NAME(lseek)))
150 #endif
151
152 #if 0
153 #ifdef __linux__
154 #undef llseek
155 int
156 SYSIO_INTERFACE_NAME(llseek)(unsigned int fd __IS_UNUSED,
157        unsigned long offset_high __IS_UNUSED,
158        unsigned long offset_low __IS_UNUSED,
159        loff_t *result __IS_UNUSED,
160        unsigned int whence __IS_UNUSED)
161 {
162         SYSIO_INTERFACE_DISPLAY_BLOCK;
163
164         /*
165          * Something is very wrong if this was called.
166          */
167         SYSIO_INTERFACE_ENTER;
168         SYSIO_INTERFACE_RETURN(-1, -ENOTSUP);
169 }
170
171 #undef __llseek
172 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(llseek), 
173                      PREPEND(__, SYSIO_INTERFACE_NAME(llseek)))
174 #endif
175 #endif