]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/ocaml/contrib/otherlibs/win32unix/lseek.c
Update
[l4.git] / l4 / pkg / ocaml / ocaml / contrib / otherlibs / win32unix / lseek.c
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 /***********************************************************************/
13
14 /* $Id: lseek.c 6774 2005-02-02 15:52:26Z xleroy $ */
15
16 #include <mlvalues.h>
17 #include <alloc.h>
18 #include "unixsupport.h"
19
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
27
28 static DWORD seek_command_table[] = {
29   FILE_BEGIN, FILE_CURRENT, FILE_END
30 };
31
32 #ifndef INVALID_SET_FILE_POINTER
33 #define INVALID_SET_FILE_POINTER (-1)
34 #endif
35
36 static __int64 caml_set_file_pointer(HANDLE h, __int64 dist, DWORD mode)
37 {
38   LARGE_INTEGER i;
39   DWORD err;
40
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); }
46   }
47   return i.QuadPart;
48 }
49
50 CAMLprim value unix_lseek(value fd, value ofs, value cmd)
51 {
52   __int64 ret;
53
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);
59   }
60   return Val_long(ret);
61 }
62
63 CAMLprim value unix_lseek_64(value fd, value ofs, value cmd)
64 {
65   __int64 ret;
66
67   ret = caml_set_file_pointer(Handle_val(fd), Int64_val(ofs),
68                               seek_command_table[Int_val(cmd)]);
69   return copy_int64(ret);
70 }