2 Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "mysys_priv.h"
19 #include "mysys_err.h"
22 Seek to a position in a file.
25 File fd The file descriptor
26 my_off_t pos The expected position (absolute or relative)
27 int whence A direction parameter and one of
28 {SEEK_SET, SEEK_CUR, SEEK_END}
29 myf MyFlags MY_THREADSAFE must be set in case my_seek may be mixed
30 with my_pread/my_pwrite calls and fd is shared among
34 The my_seek function is a wrapper around the system call lseek and
35 repositions the offset of the file descriptor fd to the argument
36 offset according to the directive whence as follows:
37 SEEK_SET The offset is set to offset bytes.
38 SEEK_CUR The offset is set to its current location plus offset bytes
39 SEEK_END The offset is set to the size of the file plus offset bytes
42 my_off_t newpos The new position in the file.
43 MY_FILEPOS_ERROR An error was encountered while performing
44 the seek. my_errno is set to indicate the
48 my_off_t
my_seek(File fd
, my_off_t pos
, int whence
, myf MyFlags
)
50 reg1 os_off_t newpos
= -1;
51 DBUG_ENTER("my_seek");
52 DBUG_PRINT("my",("Fd: %d Hpos: %lu Pos: %lu Whence: %d MyFlags: %d",
53 fd
, (ulong
) (((ulonglong
) pos
) >> 32), (ulong
) pos
,
55 DBUG_ASSERT(pos
!= MY_FILEPOS_ERROR
); /* safety check */
58 Make sure we are using a valid file descriptor!
60 DBUG_ASSERT(fd
!= -1);
61 #if defined(THREAD) && !defined(HAVE_PREAD)
62 if (MyFlags
& MY_THREADSAFE
)
64 pthread_mutex_lock(&my_file_info
[fd
].mutex
);
65 newpos
= lseek(fd
, pos
, whence
);
66 pthread_mutex_unlock(&my_file_info
[fd
].mutex
);
70 newpos
= lseek(fd
, pos
, whence
);
71 if (newpos
== (os_off_t
) -1)
75 my_error(EE_CANT_SEEK
, MYF(0), my_filename(fd
), my_errno
);
76 DBUG_PRINT("error",("lseek: %lu errno: %d", (ulong
) newpos
,errno
));
77 DBUG_RETURN(MY_FILEPOS_ERROR
);
79 if ((my_off_t
) newpos
!= pos
)
81 DBUG_PRINT("exit",("pos: %lu", (ulong
) newpos
));
83 DBUG_RETURN((my_off_t
) newpos
);
87 /* Tell current position of file */
90 my_off_t
my_tell(File fd
, myf MyFlags
)
93 DBUG_ENTER("my_tell");
94 DBUG_PRINT("my",("Fd: %d MyFlags: %d",fd
, MyFlags
));
99 pos
=lseek(fd
, 0L, MY_SEEK_CUR
);
101 if (pos
== (os_off_t
) -1)
104 if (MyFlags
& MY_WME
)
105 my_error(EE_CANT_SEEK
, MYF(0), my_filename(fd
), my_errno
);
106 DBUG_PRINT("error", ("tell: %lu errno: %d", (ulong
) pos
, my_errno
));
108 DBUG_PRINT("exit",("pos: %lu", (ulong
) pos
));
109 DBUG_RETURN((my_off_t
) pos
);