1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*++ |
6 | |
7 | |
8 | |
9 | Module Name: |
10 | |
11 | path.c |
12 | |
13 | Abstract: |
14 | |
15 | Implementation of path functions part of Windows runtime library. |
16 | |
17 | Revision History: |
18 | |
19 | |
20 | |
21 | --*/ |
22 | |
23 | #include "pal/palinternal.h" |
24 | #include "pal/dbgmsg.h" |
25 | #include "pal/file.h" |
26 | #include "pal/printfcpp.hpp" |
27 | |
28 | #include <string.h> |
29 | #include <stdlib.h> |
30 | #include <sys/param.h> |
31 | #include <errno.h> |
32 | #include <limits.h> |
33 | |
34 | SET_DEFAULT_DEBUG_CHANNEL(CRT); |
35 | |
36 | /*++ |
37 | Function: |
38 | _fullpath |
39 | |
40 | See MSDN doc. |
41 | |
42 | --*/ |
43 | char * |
44 | __cdecl |
45 | _fullpath( |
46 | char *absPath, |
47 | const char *relPath, |
48 | size_t maxLength) |
49 | { |
50 | char realpath_buf[PATH_MAX+1]; |
51 | char path_copy[PATH_MAX+1]; |
52 | char *retval = NULL; |
53 | DWORD cPathCopy = sizeof(path_copy)/sizeof(path_copy[0]); |
54 | size_t min_length; |
55 | BOOL fBufAllocated = FALSE; |
56 | |
57 | PERF_ENTRY(_fullpath); |
58 | ENTRY("_fullpath (absPath=%p, relPath=%p (%s), maxLength = %lu)\n" , |
59 | absPath, relPath ? relPath:"NULL" , relPath ? relPath:"NULL" , maxLength); |
60 | |
61 | if (strncpy_s(path_copy, sizeof(path_copy), relPath ? relPath : "." , cPathCopy) != SAFECRT_SUCCESS) |
62 | { |
63 | TRACE("_fullpath: strncpy_s failed!\n" ); |
64 | goto fullpathExit; |
65 | } |
66 | |
67 | FILEDosToUnixPathA(path_copy); |
68 | |
69 | if(NULL == realpath(path_copy, realpath_buf)) |
70 | { |
71 | ERROR("realpath() failed; problem path is '%s'. errno is %d (%s)\n" , |
72 | realpath_buf, errno, strerror(errno)); |
73 | goto fullpathExit; |
74 | } |
75 | |
76 | TRACE("real path is %s\n" , realpath_buf); |
77 | min_length = strlen(realpath_buf)+1; // +1 for the NULL terminator |
78 | |
79 | if(NULL == absPath) |
80 | { |
81 | absPath = static_cast<char *>( |
82 | PAL_malloc(_MAX_PATH * sizeof(char))); |
83 | if (!absPath) |
84 | { |
85 | ERROR("PAL_malloc failed with error %d\n" , errno); |
86 | goto fullpathExit; |
87 | } |
88 | maxLength = _MAX_PATH; |
89 | fBufAllocated = TRUE; |
90 | } |
91 | |
92 | if(min_length > maxLength) |
93 | { |
94 | ERROR("maxLength is %lu, we need at least %lu\n" , |
95 | maxLength, min_length); |
96 | if (fBufAllocated) |
97 | { |
98 | PAL_free(absPath); |
99 | fBufAllocated = FALSE; |
100 | } |
101 | goto fullpathExit; |
102 | } |
103 | |
104 | strcpy_s(absPath, maxLength, realpath_buf); |
105 | retval = absPath; |
106 | |
107 | fullpathExit: |
108 | LOGEXIT("_fullpath returns char * %p\n" , retval); |
109 | PERF_EXIT(_fullpath); |
110 | return retval; |
111 | } |
112 | |
113 | |
114 | |
115 | |