summaryrefslogtreecommitdiffstats
path: root/otherlibs/win32unix/lseek.c
blob: acc7b10044363d81d081ea3c9610a09f51ba2176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/***********************************************************************/
/*                                                                     */
/*                           Objective Caml                            */
/*                                                                     */
/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1996 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License, with    */
/*  the special exception on linking described in file ../../LICENSE.  */
/*                                                                     */
/***********************************************************************/

/* $Id$ */

#include <mlvalues.h>
#include <alloc.h>
#include "unixsupport.h"

#ifdef HAS_UNISTD
#include <unistd.h>
#else
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif

static int seek_command_table[] = {
  FILE_BEGIN, FILE_CURRENT, FILE_END
};

#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER (-1)
#endif

CAMLprim value unix_lseek(value fd, value ofs, value cmd)
{
  long ret;
  long ofs_low = Long_val(ofs);
  long ofs_high = ofs_low >= 0 ? 0 : -1;
  long err;

  ret = SetFilePointer(Handle_val(fd), ofs_low, &ofs_high,
                       seek_command_table[Int_val(cmd)]);
  if (ret == INVALID_SET_FILE_POINTER) {
    err = GetLastError();
    if (err != NO_ERROR) {
      win32_maperr(err);
      uerror("lseek", Nothing);
    }
  }
  if (ofs_high != 0 || ret > Max_long) {
    win32_maperr(ERROR_ARITHMETIC_OVERFLOW);
    uerror("lseek", Nothing);
  }
  return Val_long(ret);
}

CAMLprim value unix_lseek_64(value fd, value ofs, value cmd)
{
  long ret;
  long ofs_low = (long) Int64_val(ofs);
  long ofs_high = (long) (Int64_val(ofs) >> 32);
  long err;

  ret = SetFilePointer(Handle_val(fd), ofs_low, &ofs_high,
                       seek_command_table[Int_val(cmd)]);
  if (ret == INVALID_SET_FILE_POINTER) {
    err = GetLastError();
    if (err != NO_ERROR) {
      win32_maperr(err);
      uerror("lseek", Nothing);
    }
  }
  return copy_int64((int64) ofs_high << 32 | ret);
}