1 /***********************************************************************/
5 /* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt */
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. */
12 /***********************************************************************/
14 /* $Id: open.c 8768 2008-01-11 16:13:18Z doligez $ */
18 #include "unixsupport.h"
21 static int open_access_flags[12] = {
22 GENERIC_READ, GENERIC_WRITE, GENERIC_READ|GENERIC_WRITE,
23 0, 0, 0, 0, 0, 0, 0, 0, 0
26 static int open_create_flags[12] = {
27 0, 0, 0, 0, 0, O_CREAT, O_TRUNC, O_EXCL, 0, 0, 0, 0
30 CAMLprim value unix_open(value path, value flags, value perm)
32 int fileaccess, createflags, fileattrib, filecreate;
33 SECURITY_ATTRIBUTES attr;
36 fileaccess = convert_flag_list(flags, open_access_flags);
38 createflags = convert_flag_list(flags, open_create_flags);
39 if ((createflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
40 filecreate = CREATE_NEW;
41 else if ((createflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
42 filecreate = CREATE_ALWAYS;
43 else if (createflags & O_TRUNC)
44 filecreate = TRUNCATE_EXISTING;
45 else if (createflags & O_CREAT)
46 filecreate = OPEN_ALWAYS;
48 filecreate = OPEN_EXISTING;
50 if ((createflags & O_CREAT) && (Int_val(perm) & 0200) == 0)
51 fileattrib = FILE_ATTRIBUTE_READONLY;
53 fileattrib = FILE_ATTRIBUTE_NORMAL;
55 attr.nLength = sizeof(attr);
56 attr.lpSecurityDescriptor = NULL;
57 attr.bInheritHandle = TRUE;
59 h = CreateFile(String_val(path), fileaccess,
60 FILE_SHARE_READ | FILE_SHARE_WRITE, &attr,
61 filecreate, fileattrib, NULL);
62 if (h == INVALID_HANDLE_VALUE) {
63 win32_maperr(GetLastError());
66 return win_alloc_handle(h);