pmode = va_arg(ap, int);
va_end(ap);
ws = NULL;
+
+ /* _(w)sopen_s fails if we provide any other modes */
+ pmode = pmode & (_S_IREAD | _S_IWRITE);
+
if ((flags & ~O_BINARY) == O_RDONLY) {
/*
* When we open a directory, _open function returns
TODO: Fix mode of new file. */
r = _open(path, flags);
#else
- r = _open(path, flags, pmode);
+ _sopen_s(&r, path, flags, _SH_DENYNO, pmode);
#endif
if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
/* Simulate other POSIX system action to pass our test suite. */
return (-1);
}
}
- r = _wopen(ws, flags, pmode);
+ _wsopen_s(&r, ws, flags, _SH_DENYNO, pmode);
if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
/* Simulate other POSIX system action to pass our test suite. */
attr = GetFileAttributesW(ws);
return (r);
}
+int
+__la_wopen(const wchar_t *path, int flags, ...)
+{
+ va_list ap;
+ wchar_t *fullpath;
+ int r, pmode;
+ DWORD attr;
+
+ va_start(ap, flags);
+ pmode = va_arg(ap, int);
+ va_end(ap);
+ fullpath = NULL;
+
+ /* _(w)sopen_s fails if we provide any other modes */
+ pmode = pmode & (_S_IREAD | _S_IWRITE);
+
+ if ((flags & ~O_BINARY) == O_RDONLY) {
+ /*
+ * When we open a directory, _open function returns
+ * "Permission denied" error.
+ */
+ attr = GetFileAttributesW(path);
+#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
+ if (attr == (DWORD)-1 && GetLastError() == ERROR_PATH_NOT_FOUND)
+#endif
+ {
+ fullpath = __la_win_permissive_name_w(path);
+ if (fullpath == NULL) {
+ errno = EINVAL;
+ return (-1);
+ }
+ path = fullpath;
+ attr = GetFileAttributesW(fullpath);
+ }
+ if (attr == (DWORD)-1) {
+ la_dosmaperr(GetLastError());
+ free(fullpath);
+ return (-1);
+ }
+ if (attr & FILE_ATTRIBUTE_DIRECTORY) {
+ HANDLE handle;
+#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION (WINAPI_PARTITION_DESKTOP)
+ if (fullpath != NULL)
+ handle = CreateFileW(fullpath, 0, 0, NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_BACKUP_SEMANTICS |
+ FILE_ATTRIBUTE_READONLY,
+ NULL);
+ else
+ handle = CreateFileW(path, 0, 0, NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_BACKUP_SEMANTICS |
+ FILE_ATTRIBUTE_READONLY,
+ NULL);
+#else /* !WINAPI_PARTITION_DESKTOP */
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
+ ZeroMemory(&createExParams, sizeof(createExParams));
+ createExParams.dwSize = sizeof(createExParams);
+ createExParams.dwFileAttributes = FILE_ATTRIBUTE_READONLY;
+ createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
+ handle = CreateFile2(fullpath, 0, 0,
+ OPEN_EXISTING, &createExParams);
+#endif /* !WINAPI_PARTITION_DESKTOP */
+ free(fullpath);
+ if (handle == INVALID_HANDLE_VALUE) {
+ la_dosmaperr(GetLastError());
+ return (-1);
+ }
+ r = _open_osfhandle((intptr_t)handle, _O_RDONLY);
+ return (r);
+ }
+ }
+ _wsopen_s(&r, path, flags, _SH_DENYNO, pmode);
+ if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
+ /* Simulate other POSIX system action to pass our test suite. */
+ attr = GetFileAttributesW(path);
+ if (attr == (DWORD)-1)
+ la_dosmaperr(GetLastError());
+ else if (attr & FILE_ATTRIBUTE_DIRECTORY)
+ errno = EISDIR;
+ else
+ errno = EACCES;
+ }
+ free(fullpath);
+ return (r);
+}
+
ssize_t
__la_read(int fd, void *buf, size_t nbytes)
{