~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_seek.cc

  • Committer: Brian Aker
  • Date: 2008-12-09 17:33:02 UTC
  • mfrom: (656.1.54 devel)
  • Revision ID: brian@tangent.org-20081209173302-aptngvc7efxnatnt
Merge from Monty.

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, myf)
46
 
{
47
 
  register off_t newpos= -1;
48
 
  assert(pos != MY_FILEPOS_ERROR);              /* safety check */
49
 
 
50
 
  /*
51
 
      Make sure we are using a valid file descriptor!
52
 
  */
53
 
  assert(fd != -1);
54
 
#if !defined(HAVE_PREAD)
55
 
  if (MyFlags & MY_THREADSAFE)
56
 
  {
57
 
    pthread_mutex_lock(&my_file_info[fd].mutex);
58
 
    newpos= lseek(fd, pos, whence);
59
 
    pthread_mutex_unlock(&my_file_info[fd].mutex);
60
 
  }
61
 
  else
62
 
#endif
63
 
    newpos= lseek(fd, pos, whence);
64
 
  if (newpos == (off_t) -1)
65
 
  {
66
 
    my_errno=errno;
67
 
    return(MY_FILEPOS_ERROR);
68
 
  }
69
 
  return((my_off_t) newpos);
70
 
} /* my_seek */
71
 
 
72
 
 
73
 
        /* Tell current position of file */
74
 
        /* ARGSUSED */
75
 
 
76
 
my_off_t my_tell(File fd)
77
 
{
78
 
  off_t pos;
79
 
  assert(fd >= 0);
80
 
#ifdef HAVE_TELL
81
 
  pos=tell(fd);
82
 
#else
83
 
  pos=lseek(fd, 0L, MY_SEEK_CUR);
84
 
#endif
85
 
  if (pos == (off_t) -1)
86
 
    my_errno=errno;
87
 
  return((my_off_t) pos);
88
 
} /* my_tell */