aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/path2utl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/dutil/path2utl.cpp')
-rw-r--r--src/dutil/path2utl.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/dutil/path2utl.cpp b/src/dutil/path2utl.cpp
new file mode 100644
index 00000000..8f5f03a1
--- /dev/null
+++ b/src/dutil/path2utl.cpp
@@ -0,0 +1,89 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3#include "precomp.h"
4
5
6DAPI_(HRESULT) PathCanonicalizePath(
7 __in_z LPCWSTR wzPath,
8 __deref_out_z LPWSTR* psczCanonicalized
9 )
10{
11 HRESULT hr = S_OK;
12 int cch = MAX_PATH + 1;
13
14 hr = StrAlloc(psczCanonicalized, cch);
15 ExitOnFailure(hr, "Failed to allocate string for the canonicalized path.");
16
17 if (::PathCanonicalizeW(*psczCanonicalized, wzPath))
18 {
19 hr = S_OK;
20 }
21 else
22 {
23 ExitFunctionWithLastError(hr);
24 }
25
26LExit:
27 return hr;
28}
29
30DAPI_(HRESULT) PathDirectoryContainsPath(
31 __in_z LPCWSTR wzDirectory,
32 __in_z LPCWSTR wzPath
33 )
34{
35 HRESULT hr = S_OK;
36 LPWSTR sczPath = NULL;
37 LPWSTR sczDirectory = NULL;
38 LPWSTR sczOriginalPath = NULL;
39 LPWSTR sczOriginalDirectory = NULL;
40
41 hr = PathCanonicalizePath(wzPath, &sczOriginalPath);
42 ExitOnFailure(hr, "Failed to canonicalize the path.");
43
44 hr = PathCanonicalizePath(wzDirectory, &sczOriginalDirectory);
45 ExitOnFailure(hr, "Failed to canonicalize the directory.");
46
47 if (!sczOriginalPath || !*sczOriginalPath)
48 {
49 ExitFunction1(hr = S_FALSE);
50 }
51 if (!sczOriginalDirectory || !*sczOriginalDirectory)
52 {
53 ExitFunction1(hr = S_FALSE);
54 }
55
56 sczPath = sczOriginalPath;
57 sczDirectory = sczOriginalDirectory;
58
59 for (; *sczDirectory;)
60 {
61 if (!*sczPath)
62 {
63 ExitFunction1(hr = S_FALSE);
64 }
65
66 if (CSTR_EQUAL != ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, sczDirectory, 1, sczPath, 1))
67 {
68 ExitFunction1(hr = S_FALSE);
69 }
70
71 ++sczDirectory;
72 ++sczPath;
73 }
74
75 --sczDirectory;
76 if (('\\' == *sczDirectory && *sczPath) || '\\' == *sczPath)
77 {
78 hr = S_OK;
79 }
80 else
81 {
82 hr = S_FALSE;
83 }
84
85LExit:
86 ReleaseStr(sczOriginalPath);
87 ReleaseStr(sczOriginalDirectory);
88 return hr;
89}