1 | /* $Id: nocrt-open.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - No-CRT - read().
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2022-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define IPRT_NO_CRT_FOR_3RD_PARTY
|
---|
42 | #include "internal/nocrt.h"
|
---|
43 | #include <iprt/nocrt/fcntl.h>
|
---|
44 | #include <iprt/nocrt/errno.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/assertcompile.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 | #include <iprt/file.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | #undef open
|
---|
52 | int RT_NOCRT(open)(const char *pszFilename, uint64_t fFlags, ... /*RTFMODE fMode*/)
|
---|
53 | {
|
---|
54 | /*
|
---|
55 | * Make fFlags into proper RTFILE_O_XXX.
|
---|
56 | */
|
---|
57 | /* Make sure we got exactly one RTFILE_O_ACTION_MASK value: */
|
---|
58 | AssertCompile(O_CREAT == RTFILE_O_OPEN_CREATE); AssertCompile(RT_IS_POWER_OF_TWO(O_CREAT));
|
---|
59 | AssertCompile(O_EXCL == RTFILE_O_CREATE); AssertCompile(RT_IS_POWER_OF_TWO(O_EXCL));
|
---|
60 | if (fFlags & O_CREAT)
|
---|
61 | {
|
---|
62 | if (fFlags & O_EXCL)
|
---|
63 | fFlags &= ~(uint64_t)O_CREAT;
|
---|
64 | va_list va;
|
---|
65 | va_start(va, fFlags);
|
---|
66 | int fMode = va_arg(va, int);
|
---|
67 | va_end(va);
|
---|
68 | fFlags &= ~(uint64_t)RTFILE_O_CREATE_MODE_MASK;
|
---|
69 | fFlags |= ((uint64_t)fMode << RTFILE_O_CREATE_MODE_SHIFT) & RTFILE_O_CREATE_MODE_MASK;
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | if (fFlags & O_EXCL)
|
---|
74 | fFlags &= ~(uint64_t)O_EXCL;
|
---|
75 | fFlags |= RTFILE_O_OPEN;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* Close on exec / inherit flag needs inverting: */
|
---|
79 | AssertCompile(O_CLOEXEC == RTFILE_O_INHERIT);
|
---|
80 | fFlags ^= O_CLOEXEC;
|
---|
81 |
|
---|
82 | /* Add deny selection: */
|
---|
83 | fFlags |= RTFILE_O_DENY_NONE;
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Try open it.
|
---|
87 | */
|
---|
88 | RTFILE hFile = NIL_RTFILE;
|
---|
89 | int rc = RTFileOpen(&hFile, pszFilename, fFlags);
|
---|
90 | if (RT_SUCCESS(rc))
|
---|
91 | {
|
---|
92 | intptr_t fd = RTFileToNative(hFile);
|
---|
93 | if ((int)fd == fd)
|
---|
94 | return (int)fd;
|
---|
95 |
|
---|
96 | AssertMsgFailed(("fd=%zd (%p)\n", fd, fd));
|
---|
97 | RTFileClose(hFile);
|
---|
98 | rc = VERR_INTERNAL_ERROR;
|
---|
99 | }
|
---|
100 | errno = RTErrConvertToErrno(rc);
|
---|
101 | return -1;
|
---|
102 | }
|
---|
103 | RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(open);
|
---|
104 |
|
---|