Whamcloud - gitweb
Ignore generated files.
[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
64         fil = _sysio_fd_find(fd);
65         if (!fil)
66                 return -EBADF;
67
68         off = -1;
69         switch (whence) {
70         
71         case SEEK_SET:
72                 off = 0;
73                 break;
74         case SEEK_CUR:
75                 off = fil->f_pos;
76                 break;
77         case SEEK_END:
78                 {
79                         int     err;
80
81                         err =
82                             (*fil->f_ino->i_ops.inop_getattr)(NULL,
83                                                               fil->f_ino,
84                                                               &stbuf);
85                         if (err)
86                                 return err;
87         
88                 }
89                 off = stbuf.st_size;
90                 break;
91         default:
92                 return -EINVAL;
93         }
94         pos = off + offset;
95         if ((offset < 0 && -offset > off) || (offset > 0 && pos <= off))
96                 return -EINVAL;
97
98 #ifdef O_LARGEFILE
99         if (pos >= ((fil->f_flags & O_LARGEFILE) ? _SYSIO_OFF_T_MAX : LONG_MAX))
100                 return -EOVERFLOW;
101 #else
102         if (pos >= _SYSIO_OFF_T_MAX)
103                 return -EOVERFLOW;
104 #endif
105         pos = (fil->f_ino->i_ops.inop_pos)(fil->f_ino, pos);
106         if (pos < 0)
107                 return pos;
108         fil->f_pos = pos;
109         return pos;
110 }
111
112 #if _LARGEFILE64_SOURCE
113 #undef lseek64
114
115 extern off64_t
116 SYSIO_INTERFACE_NAME(lseek64)(int fd, off64_t offset, int whence)
117 {
118         _SYSIO_OFF_T off;
119         off_t   rtn;
120         SYSIO_INTERFACE_DISPLAY_BLOCK;
121
122         SYSIO_INTERFACE_ENTER;
123         off = _sysio_lseek(fd, offset, whence);
124         if (off < 0)
125                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )off);
126         rtn = (off64_t )off;
127         assert(rtn == off);
128         SYSIO_INTERFACE_RETURN(rtn, 0);
129 }
130 #ifdef __GLIBC__
131 #undef __lseek64
132 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek64),
133                      PREPEND(__, SYSIO_INTERFACE_NAME(lseek64)))
134 #endif
135 #ifdef REDSTORM
136 #undef __libc_lseek64
137 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek64),
138                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_lseek64)))
139 #endif
140 #endif
141
142 #undef lseek
143
144 extern off_t
145 SYSIO_INTERFACE_NAME(lseek)(int fd, off_t offset, int whence)
146 {
147         _SYSIO_OFF_T off;
148         off_t   rtn;
149         SYSIO_INTERFACE_DISPLAY_BLOCK;
150
151         SYSIO_INTERFACE_ENTER;
152         off = _sysio_lseek(fd, offset, whence);
153         if (off < 0)
154                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )off);
155         rtn = (off_t )off;
156         assert(rtn == off);
157         SYSIO_INTERFACE_RETURN(rtn, 0);
158 }
159
160 #ifdef __GLIBC__
161 #undef __lseek
162 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek),
163                      PREPEND(__, SYSIO_INTERFACE_NAME(lseek)))
164 #endif
165
166 #if 0
167 #ifdef __linux__
168 #undef llseek
169 int
170 SYSIO_INTERFACE_NAME(llseek)(unsigned int fd __IS_UNUSED,
171        unsigned long offset_high __IS_UNUSED,
172        unsigned long offset_low __IS_UNUSED,
173        loff_t *result __IS_UNUSED,
174        unsigned int whence __IS_UNUSED)
175 {
176         SYSIO_INTERFACE_DISPLAY_BLOCK;
177
178         /*
179          * Something is very wrong if this was called.
180          */
181         SYSIO_INTERFACE_ENTER;
182         SYSIO_INTERFACE_RETURN(-1, -ENOTSUP);
183 }
184
185 #undef __llseek
186 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(llseek), 
187                      PREPEND(__, SYSIO_INTERFACE_NAME(llseek)))
188 #endif
189 #endif