~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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 */
15
16
#include "mysys_priv.h"
17
18
/* 
19
  Seek to a position in a file.
20
21
  ARGUMENTS
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
28
                   threads.
29
30
  DESCRIPTION
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
37
38
  RETURN VALUE
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
42
                       actual error.
43
*/
44
45
my_off_t my_seek(File fd, my_off_t pos, int whence,
46
		 myf MyFlags __attribute__((unused)))
47
{
489.1.13 by Monty Taylor
One step closer to simpler off_t support.
48
  register off_t newpos= -1;
51.3.16 by Jay Pipes
Phase 4 removal of DBUG in mysys
49
  assert(pos != MY_FILEPOS_ERROR);		/* safety check */
1 by brian
clean slate
50
51
  /*
52
      Make sure we are using a valid file descriptor!
53
  */
51.3.16 by Jay Pipes
Phase 4 removal of DBUG in mysys
54
  assert(fd != -1);
28.1.35 by Monty Taylor
Removed all references to THREAD.
55
#if !defined(HAVE_PREAD)
1 by brian
clean slate
56
  if (MyFlags & MY_THREADSAFE)
57
  {
58
    pthread_mutex_lock(&my_file_info[fd].mutex);
59
    newpos= lseek(fd, pos, whence);
60
    pthread_mutex_unlock(&my_file_info[fd].mutex);
61
  }
62
  else
63
#endif
64
    newpos= lseek(fd, pos, whence);
489.1.13 by Monty Taylor
One step closer to simpler off_t support.
65
  if (newpos == (off_t) -1)
1 by brian
clean slate
66
  {
67
    my_errno=errno;
51.3.16 by Jay Pipes
Phase 4 removal of DBUG in mysys
68
    return(MY_FILEPOS_ERROR);
69
  }
70
  return((my_off_t) newpos);
1 by brian
clean slate
71
} /* my_seek */
72
73
74
	/* Tell current position of file */
75
	/* ARGSUSED */
76
77
my_off_t my_tell(File fd, myf MyFlags __attribute__((unused)))
78
{
489.1.13 by Monty Taylor
One step closer to simpler off_t support.
79
  off_t pos;
51.3.16 by Jay Pipes
Phase 4 removal of DBUG in mysys
80
  assert(fd >= 0);
1 by brian
clean slate
81
#ifdef HAVE_TELL
82
  pos=tell(fd);
83
#else
84
  pos=lseek(fd, 0L, MY_SEEK_CUR);
85
#endif
489.1.13 by Monty Taylor
One step closer to simpler off_t support.
86
  if (pos == (off_t) -1)
1 by brian
clean slate
87
    my_errno=errno;
51.3.16 by Jay Pipes
Phase 4 removal of DBUG in mysys
88
  return((my_off_t) pos);
1 by brian
clean slate
89
} /* my_tell */