1997-09-03 07:38:02 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1997-09-03 07:38:02 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <mlvalues.h>
|
2002-06-07 02:49:45 -07:00
|
|
|
#include <alloc.h>
|
1997-09-03 07:38:02 -07:00
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
2002-03-06 08:55:20 -08:00
|
|
|
#ifndef INVALID_SET_FILE_POINTER
|
|
|
|
#define INVALID_SET_FILE_POINTER (-1)
|
|
|
|
#endif
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_lseek(value fd, value ofs, value cmd)
|
1997-09-03 07:38:02 -07:00
|
|
|
{
|
|
|
|
long ret;
|
2002-03-06 08:55:20 -08:00
|
|
|
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,
|
1997-09-03 07:38:02 -07:00
|
|
|
seek_command_table[Int_val(cmd)]);
|
2002-03-06 08:55:20 -08:00
|
|
|
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);
|
1997-09-03 07:38:02 -07:00
|
|
|
uerror("lseek", Nothing);
|
|
|
|
}
|
|
|
|
return Val_long(ret);
|
|
|
|
}
|
2002-03-06 08:55:20 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2002-06-07 02:49:45 -07:00
|
|
|
return copy_int64((int64) ofs_high << 32 | ret);
|
2002-03-06 08:55:20 -08:00
|
|
|
}
|