2 * posix.c: Posix-specific support.
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 * Copyright (c) 2002-2009 Novell, Inc.
18 #include <sys/types.h>
21 #include <mono/io-layer/wapi.h>
22 #include <mono/io-layer/wapi-private.h>
23 #include <mono/io-layer/handles-private.h>
24 #include <mono/io-layer/io-private.h>
27 #define DEBUG(...) g_message(__VA_ARGS__)
33 convert_from_flags(int flags)
38 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
41 if((flags & O_ACCMODE) == O_RDONLY) {
42 fileaccess=GENERIC_READ;
43 } else if ((flags & O_ACCMODE) == O_WRONLY) {
44 fileaccess=GENERIC_WRITE;
45 } else if ((flags & O_ACCMODE) == O_RDWR) {
46 fileaccess=GENERIC_READ|GENERIC_WRITE;
48 DEBUG("%s: Can't figure out flags 0x%x", __func__, flags);
51 /* Maybe sort out create mode too */
57 gpointer _wapi_stdhandle_create (int fd, const gchar *name)
59 struct _WapiHandle_file file_handle = {0};
63 DEBUG("%s: creating standard handle type %s, fd %d", __func__,
66 #if !defined(__native_client__)
67 /* Check if fd is valid */
69 flags=fcntl(fd, F_GETFL);
70 } while (flags == -1 && errno == EINTR);
73 /* Invalid fd. Not really much point checking for EBADF
76 DEBUG("%s: fcntl error on fd %d: %s", __func__, fd,
79 SetLastError (_wapi_get_win32_file_error (errno));
80 return(INVALID_HANDLE_VALUE);
82 file_handle.fileaccess=convert_from_flags(flags);
85 * fcntl will return -1 in nacl, as there is no real file system API.
86 * Yet, standard streams are available.
88 file_handle.fileaccess = (fd == STDIN_FILENO) ? GENERIC_READ : GENERIC_WRITE;
92 file_handle.filename = g_strdup(name);
93 /* some default security attributes might be needed */
94 file_handle.security_attributes=0;
96 /* Apparently input handles can't be written to. (I don't
97 * know if output or error handles can't be read from.)
100 file_handle.fileaccess &= ~GENERIC_WRITE;
103 file_handle.sharemode=0;
106 handle = _wapi_handle_new_fd (WAPI_HANDLE_CONSOLE, fd, &file_handle);
107 if (handle == _WAPI_HANDLE_INVALID) {
108 g_warning ("%s: error creating file handle", __func__);
109 SetLastError (ERROR_GEN_FAILURE);
110 return(INVALID_HANDLE_VALUE);
113 DEBUG("%s: returning handle %p", __func__, handle);