~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_seek.c

Merge of Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
{
48
 
  register os_off_t newpos= -1;
49
 
  assert(pos != MY_FILEPOS_ERROR);              /* safety check */
50
 
 
51
 
  /*
52
 
      Make sure we are using a valid file descriptor!
53
 
  */
54
 
  assert(fd != -1);
55
 
#if !defined(HAVE_PREAD)
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);
65
 
  if (newpos == (os_off_t) -1)
66
 
  {
67
 
    my_errno=errno;
68
 
    return(MY_FILEPOS_ERROR);
69
 
  }
70
 
  return((my_off_t) newpos);
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
 
{
79
 
  os_off_t pos;
80
 
  assert(fd >= 0);
81
 
#ifdef HAVE_TELL
82
 
  pos=tell(fd);
83
 
#else
84
 
  pos=lseek(fd, 0L, MY_SEEK_CUR);
85
 
#endif
86
 
  if (pos == (os_off_t) -1)
87
 
    my_errno=errno;
88
 
  return((my_off_t) pos);
89
 
} /* my_tell */