1 | /* $Id: RTPathGetCurrentDrive-generic.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathGetCurrentDrive, generic implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_PATH
|
---|
32 | #include <iprt/path.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/ctype.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include "internal/path.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | RTDECL(int) RTPathGetCurrentDrive(char *pszPath, size_t cbPath)
|
---|
43 | {
|
---|
44 | #ifdef HAVE_DRIVE
|
---|
45 | /*
|
---|
46 | * Query the current directroy and extract the wanted information from it.
|
---|
47 | */
|
---|
48 | char *pszPathFree = NULL;
|
---|
49 | char *pszCwd = pszPath;
|
---|
50 | int rc = RTPathGetCurrent(pszCwd, cbPath);
|
---|
51 | if (RT_SUCCESS(rc))
|
---|
52 | { /* likely */ }
|
---|
53 | else if (rc != VERR_BUFFER_OVERFLOW)
|
---|
54 | return rc;
|
---|
55 | else
|
---|
56 | {
|
---|
57 | pszPathFree = pszCwd = (char *)RTMemTmpAlloc(RTPATH_BIG_MAX);
|
---|
58 | AssertReturn(pszPathFree, VERR_NO_TMP_MEMORY);
|
---|
59 | rc = RTPathGetCurrent(pszCwd, RTPATH_BIG_MAX);
|
---|
60 | }
|
---|
61 | if (RT_SUCCESS(rc))
|
---|
62 | {
|
---|
63 | /*
|
---|
64 | * Drive letter? Chop off at root slash.
|
---|
65 | */
|
---|
66 | if (pszCwd[0] && RTPATH_IS_VOLSEP(pszCwd[1]))
|
---|
67 | pszCwd[2] = '\0';
|
---|
68 | /*
|
---|
69 | * UNC? Chop off after share.
|
---|
70 | */
|
---|
71 | else if ( RTPATH_IS_SLASH(pszCwd[0])
|
---|
72 | && RTPATH_IS_SLASH(pszCwd[1])
|
---|
73 | && !RTPATH_IS_SLASH(pszCwd[2])
|
---|
74 | && pszCwd[2])
|
---|
75 | {
|
---|
76 | /* Work thru the server name. */
|
---|
77 | size_t off = 3;
|
---|
78 | while (!RTPATH_IS_SLASH(pszCwd[off]) && pszCwd[off])
|
---|
79 | off++;
|
---|
80 | size_t offServerSlash = off;
|
---|
81 |
|
---|
82 | /* Is there a share name? */
|
---|
83 | if (RTPATH_IS_SLASH(pszCwd[off]))
|
---|
84 | {
|
---|
85 | while (RTPATH_IS_SLASH(pszCwd[off]))
|
---|
86 | off++;
|
---|
87 | if (pszCwd[off])
|
---|
88 | {
|
---|
89 | /* Work thru the share name. */
|
---|
90 | while (!RTPATH_IS_SLASH(pszCwd[off]) && pszCwd[off])
|
---|
91 | off++;
|
---|
92 | }
|
---|
93 | /* No share name, chop at server name. */
|
---|
94 | else
|
---|
95 | off = offServerSlash;
|
---|
96 | }
|
---|
97 | pszCwd[off] = '\0';
|
---|
98 | }
|
---|
99 | else
|
---|
100 | rc = VERR_INTERNAL_ERROR_4;
|
---|
101 | }
|
---|
102 | if (pszPathFree)
|
---|
103 | {
|
---|
104 | if (RT_SUCCESS(rc))
|
---|
105 | rc = RTStrCopy(pszPath, cbPath, pszCwd);
|
---|
106 | RTMemTmpFree(pszPathFree);
|
---|
107 | }
|
---|
108 | return rc;
|
---|
109 |
|
---|
110 | #else /* !HAVE_DRIVE */
|
---|
111 | /*
|
---|
112 | * No drive letters on this system, return empty string.
|
---|
113 | */
|
---|
114 | if (cbPath > 0)
|
---|
115 | {
|
---|
116 | *pszPath = '\0';
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 | return VERR_BUFFER_OVERFLOW;
|
---|
120 | #endif /* !HAVE_DRIVE */
|
---|
121 | }
|
---|
122 | RT_EXPORT_SYMBOL(RTPathGetCurrentDrive);
|
---|
123 |
|
---|