]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/contrib/otherlibs/win32unix/open.c
Inital import
[l4.git] / l4 / pkg / ocaml / contrib / otherlibs / win32unix / open.c
1 /***********************************************************************/
2 /*                                                                     */
3 /*                           Objective Caml                            */
4 /*                                                                     */
5 /*  Xavier Leroy and Pascal Cuoq, 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: open.c 8768 2008-01-11 16:13:18Z doligez $ */
15
16 #include <mlvalues.h>
17 #include <alloc.h>
18 #include "unixsupport.h"
19 #include <fcntl.h>
20
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
24 };
25
26 static int open_create_flags[12] = {
27   0, 0, 0, 0, 0, O_CREAT, O_TRUNC, O_EXCL, 0, 0, 0, 0
28 };
29
30 CAMLprim value unix_open(value path, value flags, value perm)
31 {
32   int fileaccess, createflags, fileattrib, filecreate;
33   SECURITY_ATTRIBUTES attr;
34   HANDLE h;
35
36   fileaccess = convert_flag_list(flags, open_access_flags);
37
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;
47   else
48     filecreate = OPEN_EXISTING;
49
50   if ((createflags & O_CREAT) && (Int_val(perm) & 0200) == 0)
51     fileattrib = FILE_ATTRIBUTE_READONLY;
52   else
53     fileattrib = FILE_ATTRIBUTE_NORMAL;
54
55   attr.nLength = sizeof(attr);
56   attr.lpSecurityDescriptor = NULL;
57   attr.bInheritHandle = TRUE;
58
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());
64     uerror("open", path);
65   }
66   return win_alloc_handle(h);
67 }