1996-09-04 07:17:43 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Pascal Cuoq, 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. */
|
1996-09-04 07:17:43 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
1996-09-05 06:32:25 -07:00
|
|
|
#include "unixsupport.h"
|
1996-09-04 07:17:43 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
static int open_access_flags[12] = {
|
|
|
|
GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0
|
1996-09-04 07:17:43 -07:00
|
|
|
};
|
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
static int open_create_flags[12] = {
|
|
|
|
0, 0, 0, 0, 0, O_CREAT, O_TRUNC, O_EXCL, 0, 0, 0, 0
|
1996-09-04 07:17:43 -07:00
|
|
|
};
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_open(value path, value flags, value perm)
|
1996-09-04 07:17:43 -07:00
|
|
|
{
|
1997-09-04 06:45:56 -07:00
|
|
|
int fileaccess, createflags, fileattrib, filecreate;
|
|
|
|
SECURITY_ATTRIBUTES attr;
|
1997-09-03 07:38:02 -07:00
|
|
|
HANDLE h;
|
1996-09-04 07:17:43 -07:00
|
|
|
|
1997-09-03 07:38:02 -07:00
|
|
|
fileaccess = convert_flag_list(flags, open_access_flags);
|
1997-09-04 06:45:56 -07:00
|
|
|
|
1997-09-03 07:38:02 -07:00
|
|
|
createflags = convert_flag_list(flags, open_create_flags);
|
|
|
|
if ((createflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
|
|
|
filecreate = CREATE_NEW;
|
|
|
|
else if ((createflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
|
|
|
|
filecreate = CREATE_ALWAYS;
|
|
|
|
else if (createflags & O_TRUNC)
|
|
|
|
filecreate = TRUNCATE_EXISTING;
|
|
|
|
else if (createflags & O_CREAT)
|
|
|
|
filecreate = OPEN_ALWAYS;
|
|
|
|
else
|
|
|
|
filecreate = OPEN_EXISTING;
|
1997-09-04 06:45:56 -07:00
|
|
|
|
1997-09-03 07:38:02 -07:00
|
|
|
if ((createflags & O_CREAT) && (Int_val(perm) & 0200) == 0)
|
|
|
|
fileattrib = FILE_ATTRIBUTE_READONLY;
|
|
|
|
else
|
|
|
|
fileattrib = FILE_ATTRIBUTE_NORMAL;
|
1997-09-04 06:45:56 -07:00
|
|
|
|
|
|
|
attr.nLength = sizeof(attr);
|
|
|
|
attr.lpSecurityDescriptor = NULL;
|
|
|
|
attr.bInheritHandle = TRUE;
|
|
|
|
|
1998-12-02 06:39:27 -08:00
|
|
|
h = CreateFile(String_val(path), fileaccess,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, &attr,
|
1997-09-03 07:38:02 -07:00
|
|
|
filecreate, fileattrib, NULL);
|
1997-09-04 06:45:56 -07:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2001-08-28 07:47:48 -07:00
|
|
|
win32_maperr(GetLastError());
|
1997-09-04 06:45:56 -07:00
|
|
|
uerror("open", path);
|
|
|
|
}
|
1997-09-03 07:38:02 -07:00
|
|
|
return win_alloc_handle(h);
|
1996-09-04 07:17:43 -07:00
|
|
|
}
|