Sys.Signals module for a Variant type of signals (and a set_signal function that...
[ocaml.git] / otherlibs / win32unix / lseek.c
blob2df3a86dc0ad8117399f2c764b3838afd01c74e4
1 /***********************************************************************/
2 /* */
3 /* Objective Caml */
4 /* */
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
6 /* */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
11 /* */
12 /***********************************************************************/
14 /* $Id$ */
16 #include <mlvalues.h>
17 #include <alloc.h>
18 #include "unixsupport.h"
20 #ifdef HAS_UNISTD
21 #include <unistd.h>
22 #else
23 #define SEEK_SET 0
24 #define SEEK_CUR 1
25 #define SEEK_END 2
26 #endif
28 static DWORD seek_command_table[] = {
29 FILE_BEGIN, FILE_CURRENT, FILE_END
32 #ifndef INVALID_SET_FILE_POINTER
33 #define INVALID_SET_FILE_POINTER (-1)
34 #endif
36 static __int64 caml_set_file_pointer(HANDLE h, __int64 dist, DWORD mode)
38 LARGE_INTEGER i;
39 DWORD err;
41 i.QuadPart = dist;
42 i.LowPart = SetFilePointer(h, i.LowPart, &i.HighPart, mode);
43 if (i.LowPart == INVALID_SET_FILE_POINTER) {
44 err = GetLastError();
45 if (err != NO_ERROR) { win32_maperr(err); uerror("lseek", Nothing); }
47 return i.QuadPart;
50 CAMLprim value unix_lseek(value fd, value ofs, value cmd)
52 __int64 ret;
54 ret = caml_set_file_pointer(Handle_val(fd), Long_val(ofs),
55 seek_command_table[Int_val(cmd)]);
56 if (ret > Max_long) {
57 win32_maperr(ERROR_ARITHMETIC_OVERFLOW);
58 uerror("lseek", Nothing);
60 return Val_long(ret);
63 CAMLprim value unix_lseek_64(value fd, value ofs, value cmd)
65 __int64 ret;
67 ret = caml_set_file_pointer(Handle_val(fd), Int64_val(ofs),
68 seek_command_table[Int_val(cmd)]);
69 return copy_int64(ret);