[52944] | 1 | /* $Id: RTPathGetCurrentDrive-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * IPRT - RTPathGetCurrentDrive, generic implementation.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2014-2023 Oracle and/or its affiliates.
|
---|
[52944] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[52944] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[52944] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[52944] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[52944] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[52944] | 41 | #define LOG_GROUP RTLOGGROUP_PATH
|
---|
| 42 | #include <iprt/path.h>
|
---|
| 43 | #include "internal/iprt.h"
|
---|
| 44 |
|
---|
| 45 | #include <iprt/ctype.h>
|
---|
| 46 | #include <iprt/err.h>
|
---|
[78048] | 47 | #include <iprt/mem.h>
|
---|
[52944] | 48 | #include <iprt/string.h>
|
---|
| 49 | #include "internal/path.h"
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | RTDECL(int) RTPathGetCurrentDrive(char *pszPath, size_t cbPath)
|
---|
| 53 | {
|
---|
| 54 | #ifdef HAVE_DRIVE
|
---|
| 55 | /*
|
---|
| 56 | * Query the current directroy and extract the wanted information from it.
|
---|
| 57 | */
|
---|
[78048] | 58 | char *pszPathFree = NULL;
|
---|
| 59 | char *pszCwd = pszPath;
|
---|
| 60 | int rc = RTPathGetCurrent(pszCwd, cbPath);
|
---|
[52944] | 61 | if (RT_SUCCESS(rc))
|
---|
[78048] | 62 | { /* likely */ }
|
---|
| 63 | else if (rc != VERR_BUFFER_OVERFLOW)
|
---|
| 64 | return rc;
|
---|
| 65 | else
|
---|
[52944] | 66 | {
|
---|
[78048] | 67 | pszPathFree = pszCwd = (char *)RTMemTmpAlloc(RTPATH_BIG_MAX);
|
---|
| 68 | AssertReturn(pszPathFree, VERR_NO_TMP_MEMORY);
|
---|
| 69 | rc = RTPathGetCurrent(pszCwd, RTPATH_BIG_MAX);
|
---|
| 70 | }
|
---|
| 71 | if (RT_SUCCESS(rc))
|
---|
| 72 | {
|
---|
[52944] | 73 | /*
|
---|
| 74 | * Drive letter? Chop off at root slash.
|
---|
| 75 | */
|
---|
[78048] | 76 | if (pszCwd[0] && RTPATH_IS_VOLSEP(pszCwd[1]))
|
---|
| 77 | pszCwd[2] = '\0';
|
---|
[52944] | 78 | /*
|
---|
| 79 | * UNC? Chop off after share.
|
---|
| 80 | */
|
---|
[78048] | 81 | else if ( RTPATH_IS_SLASH(pszCwd[0])
|
---|
| 82 | && RTPATH_IS_SLASH(pszCwd[1])
|
---|
| 83 | && !RTPATH_IS_SLASH(pszCwd[2])
|
---|
| 84 | && pszCwd[2])
|
---|
[52944] | 85 | {
|
---|
| 86 | /* Work thru the server name. */
|
---|
| 87 | size_t off = 3;
|
---|
[78048] | 88 | while (!RTPATH_IS_SLASH(pszCwd[off]) && pszCwd[off])
|
---|
[52944] | 89 | off++;
|
---|
| 90 | size_t offServerSlash = off;
|
---|
| 91 |
|
---|
| 92 | /* Is there a share name? */
|
---|
[78048] | 93 | if (RTPATH_IS_SLASH(pszCwd[off]))
|
---|
[52944] | 94 | {
|
---|
[78048] | 95 | while (RTPATH_IS_SLASH(pszCwd[off]))
|
---|
[52944] | 96 | off++;
|
---|
[78048] | 97 | if (pszCwd[off])
|
---|
[52944] | 98 | {
|
---|
| 99 | /* Work thru the share name. */
|
---|
[78048] | 100 | while (!RTPATH_IS_SLASH(pszCwd[off]) && pszCwd[off])
|
---|
[52944] | 101 | off++;
|
---|
| 102 | }
|
---|
| 103 | /* No share name, chop at server name. */
|
---|
| 104 | else
|
---|
| 105 | off = offServerSlash;
|
---|
| 106 | }
|
---|
[78048] | 107 | pszCwd[off] = '\0';
|
---|
[52944] | 108 | }
|
---|
[78048] | 109 | else
|
---|
| 110 | rc = VERR_INTERNAL_ERROR_4;
|
---|
[52944] | 111 | }
|
---|
[78048] | 112 | if (pszPathFree)
|
---|
| 113 | {
|
---|
| 114 | if (RT_SUCCESS(rc))
|
---|
| 115 | rc = RTStrCopy(pszPath, cbPath, pszCwd);
|
---|
| 116 | RTMemTmpFree(pszPathFree);
|
---|
| 117 | }
|
---|
[52944] | 118 | return rc;
|
---|
| 119 |
|
---|
| 120 | #else /* !HAVE_DRIVE */
|
---|
| 121 | /*
|
---|
| 122 | * No drive letters on this system, return empty string.
|
---|
| 123 | */
|
---|
| 124 | if (cbPath > 0)
|
---|
| 125 | {
|
---|
| 126 | *pszPath = '\0';
|
---|
| 127 | return VINF_SUCCESS;
|
---|
| 128 | }
|
---|
| 129 | return VERR_BUFFER_OVERFLOW;
|
---|
| 130 | #endif /* !HAVE_DRIVE */
|
---|
| 131 | }
|
---|
| 132 | RT_EXPORT_SYMBOL(RTPathGetCurrentDrive);
|
---|
| 133 |
|
---|