ReactOS 0.4.16-dev-814-g656a5dc
fullpath.cpp
Go to the documentation of this file.
1/***
2*fullpath.c -
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose: contains the function _fullpath which makes an absolute path out
7* of a relative path. i.e. ..\pop\..\main.c => c:\src\main.c if the
8* current directory is c:\src\src
9*
10*******************************************************************************/
11
12#include <stdio.h>
13#include <direct.h>
14#include <errno.h>
15#include <stdlib.h>
17#include <windows.h>
18
19/***
20*_TCHAR *_fullpath( _TCHAR *buf, const _TCHAR *path, maxlen );
21*
22*Purpose:
23*
24* _fullpath - combines the current directory with path to form
25* an absolute path. i.e. _fullpath takes care of .\ and ..\
26* in the path.
27*
28* The result is placed in buf. If the length of the result
29* is greater than maxlen nullptr is returned, otherwise
30* the address of buf is returned.
31*
32* If buf is nullptr then a buffer is malloc'ed and maxlen is
33* ignored. If there are no errors then the address of this
34* buffer is returned.
35*
36* If path specifies a drive, the curent directory of this
37* drive is combined with path. If the drive is not valid
38* and _fullpath needs the current directory of this drive
39* then nullptr is returned. If the current directory of this
40* non existant drive is not needed then a proper value is
41* returned.
42* For example: path = "z:\\pop" does not need z:'s current
43* directory but path = "z:pop" does.
44*
45*
46*
47*Entry:
48* _TCHAR *buf - pointer to a buffer maintained by the user;
49* _TCHAR *path - path to "add" to the current directory
50* int maxlen - length of the buffer pointed to by buf
51*
52*Exit:
53* Returns pointer to the buffer containing the absolute path
54* (same as buf if non-nullptr; otherwise, malloc is
55* used to allocate a buffer)
56*
57*Exceptions:
58*
59*******************************************************************************/
60
61template <typename Character>
62_Success_(return != 0)
63static Character* __cdecl common_fullpath(
64 _Out_writes_z_(max_count) Character* const user_buffer,
65 Character const* const path,
71{
72 // These are referenced only in the Debug CRT build
76
77 typedef __crt_char_traits<Character> traits;
78
79 // If the path is empty, we have no work to do:
80 if (path == nullptr || path[0] == '\0')
81 {
82#pragma warning(suppress:__WARNING_POSTCONDITION_NULLTERMINATION_VIOLATION) // 26036 Prefast does not understand perfect forwarding.
83 return traits::tgetcwd(user_buffer, static_cast<int>(__min(max_count, INT_MAX)));
84 }
85
86 if (user_buffer != nullptr) {
87 // Using user buffer. Fail if not enough space.
89 if (!traits::get_full_path_name(path, buffer)) {
90 return user_buffer;
91 } else {
92 return nullptr;
93 }
94 } else {
95 // Always new memory suitable for debug mode and releasing to the user.
98 );
99 traits::get_full_path_name(path, buffer);
100 return buffer.detach();
101 }
102}
103
104
105
106extern "C" char* __cdecl _fullpath(
107 char* const user_buffer,
108 char const* const path,
109 size_t const max_count
110 )
111{
112 return common_fullpath(user_buffer, path, max_count, _NORMAL_BLOCK, nullptr, 0);
113}
114
115extern "C" wchar_t* __cdecl _wfullpath(
116 wchar_t* const user_buffer,
117 wchar_t const* const path,
118 size_t const max_count
119 )
120{
121 return common_fullpath(user_buffer, path, max_count, _NORMAL_BLOCK, nullptr, 0);
122}
123
124#ifdef _DEBUG
125
126#undef _fullpath_dbg
127#undef _wfullpath_dbg
128
129extern "C" char* __cdecl _fullpath_dbg(
130 char* const user_buffer,
131 char const* const path,
132 size_t const max_count,
133 int const block_use,
134 char const* const file_name,
135 int const line_number
136 )
137{
138 return common_fullpath(user_buffer, path, max_count, block_use, file_name, line_number);
139}
140
141extern "C" wchar_t* __cdecl _wfullpath_dbg(
142 wchar_t* const user_buffer,
143 wchar_t const* const path,
144 size_t const max_count,
145 int const block_use,
146 char const* const file_name,
147 int const line_number
148 )
149{
150 return common_fullpath(user_buffer, path, max_count, block_use, file_name, line_number);
151}
152
153#endif // _DEBUG
#define __cdecl
Definition: accygwin.h:79
#define _wfullpath_dbg(s1, s2, le, t, f, l)
Definition: crtdbg.h:229
#define _NORMAL_BLOCK
Definition: crtdbg.h:67
#define _fullpath_dbg(s1, s2, le, t, f, l)
Definition: crtdbg.h:228
int const char const *const int const line_number
Definition: debug_heap.cpp:499
Character const *const size_t const int const block_use
Definition: fullpath.cpp:67
char *__cdecl _fullpath(char *const user_buffer, char const *const path, size_t const max_count)
Definition: fullpath.cpp:106
Character const *const size_t const int const char const *const int const line_number throw()
Definition: fullpath.cpp:70
Character const *const size_t const max_count
Definition: fullpath.cpp:66
Character const *const size_t const int const char const *const file_name
Definition: fullpath.cpp:68
wchar_t *__cdecl _wfullpath(wchar_t *const user_buffer, wchar_t const *const path, size_t const max_count)
Definition: fullpath.cpp:115
GLuint buffer
Definition: glext.h:5915
#define __min(a, b)
Definition: stdlib.h:102
#define INT_MAX
Definition: intsafe.h:150
#define _Out_writes_z_(s)
Definition: no_sal2.h:180
#define _Success_(c)
Definition: no_sal2.h:84
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325
#define const
Definition: zconf.h:233