1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
19
Seek to a position in a file.
22
File fd The file descriptor
23
my_off_t pos The expected position (absolute or relative)
24
int whence A direction parameter and one of
25
{SEEK_SET, SEEK_CUR, SEEK_END}
26
myf MyFlags MY_THREADSAFE must be set in case my_seek may be mixed
27
with my_pread/my_pwrite calls and fd is shared among
31
The my_seek function is a wrapper around the system call lseek and
32
repositions the offset of the file descriptor fd to the argument
33
offset according to the directive whence as follows:
34
SEEK_SET The offset is set to offset bytes.
35
SEEK_CUR The offset is set to its current location plus offset bytes
36
SEEK_END The offset is set to the size of the file plus offset bytes
39
my_off_t newpos The new position in the file.
40
MY_FILEPOS_ERROR An error was encountered while performing
41
the seek. my_errno is set to indicate the
45
my_off_t my_seek(File fd, my_off_t pos, int whence,
46
myf MyFlags __attribute__((unused)))
48
register os_off_t newpos= -1;
49
DBUG_ENTER("my_seek");
50
DBUG_PRINT("my",("Fd: %d Hpos: %lu Pos: %lu Whence: %d MyFlags: %d",
51
fd, (ulong) (((ulonglong) pos) >> 32), (ulong) pos,
53
DBUG_ASSERT(pos != MY_FILEPOS_ERROR); /* safety check */
56
Make sure we are using a valid file descriptor!
58
DBUG_ASSERT(fd != -1);
59
#if defined(THREAD) && !defined(HAVE_PREAD)
60
if (MyFlags & MY_THREADSAFE)
62
pthread_mutex_lock(&my_file_info[fd].mutex);
63
newpos= lseek(fd, pos, whence);
64
pthread_mutex_unlock(&my_file_info[fd].mutex);
68
newpos= lseek(fd, pos, whence);
69
if (newpos == (os_off_t) -1)
72
DBUG_PRINT("error",("lseek: %lu errno: %d", (ulong) newpos,errno));
73
DBUG_RETURN(MY_FILEPOS_ERROR);
75
if ((my_off_t) newpos != pos)
77
DBUG_PRINT("exit",("pos: %lu", (ulong) newpos));
79
DBUG_RETURN((my_off_t) newpos);
83
/* Tell current position of file */
86
my_off_t my_tell(File fd, myf MyFlags __attribute__((unused)))
89
DBUG_ENTER("my_tell");
90
DBUG_PRINT("my",("Fd: %d MyFlags: %d",fd, MyFlags));
95
pos=lseek(fd, 0L, MY_SEEK_CUR);
97
if (pos == (os_off_t) -1)
99
DBUG_PRINT("exit",("pos: %lu", (ulong) pos));
100
DBUG_RETURN((my_off_t) pos);