xref: /AOO41X/main/sal/systools/win32/uwinapi/GetLongPathName.cpp (revision fc0bc00825ec02ef04af0b032f1c57b312b79937)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 {
26     DWORD   dwResult = 0;   // Assume failure
27 
28     if ( IsBadStringPtr( lpShortPath, MAX_PATH ) )
29     {
30         SetLastError( ERROR_INVALID_PARAMETER );
31         return dwResult;
32     }
33 
34     // Assume a not existing buffer means a bufsize of zero
35     if ( !lpLongPath )
36         cchBuffer = 0;
37 
38     if ( _tcslen( lpShortPath ) == 2 && lpShortPath[1] == ':' )
39     {
40         _tcscpy( lpLongPath, lpShortPath );
41         dwResult = _tcslen( lpLongPath );
42     }
43     else
44     {
45         HANDLE          hFind;
46         WIN32_FIND_DATA aFindFileData;
47 
48         if ( lpShortPath[_tcslen(lpShortPath)-1] == '\\' )
49         {
50             TCHAR   szFilePath[MAX_PATH];
51 
52             _tcscpy( szFilePath, lpShortPath );
53             _tcscat( szFilePath, TEXT("*.*") );
54             hFind = FindFirstFile( szFilePath, &aFindFileData );;
55             aFindFileData.cFileName[0] = 0;
56         }
57         else
58         {
59             hFind = FindFirstFile( lpShortPath, &aFindFileData );
60             if ( !IsValidHandle( hFind ) )
61             {
62                 TCHAR   szFilePath[MAX_PATH];
63 
64                 _tcscpy( szFilePath, lpShortPath );
65                 _tcscat( szFilePath, TEXT("\\*.*") );
66                 hFind = FindFirstFile( szFilePath, &aFindFileData );;
67                 aFindFileData.cFileName[0] = 0;
68             }
69         }
70 
71         if ( IsValidHandle( hFind ) )
72         {
73             FindClose( hFind );
74 
75             LPCTSTR lpLastSlash = _tcsrchr( lpShortPath, '\\' );
76 
77             if ( lpLastSlash )
78             {
79                 int nParentLen = lpLastSlash - lpShortPath;
80                 LPTSTR  lpParentPath = (LPTSTR)_alloca( (nParentLen + 1) * sizeof(TCHAR) );
81 
82                 CopyMemory( lpParentPath, lpShortPath, nParentLen * sizeof(TCHAR) );
83                 lpParentPath[nParentLen] = 0;
84 
85                 dwResult = GetLongPathName( lpParentPath, lpLongPath, cchBuffer );
86 
87                 if ( !dwResult )
88                     _tcscpy( lpLongPath, lpParentPath );
89             }
90             else
91             {
92                 _tcscpy( lpLongPath, lpShortPath );
93                 dwResult = _tcslen( lpLongPath );
94             }
95 
96             if ( dwResult < cchBuffer )
97             {
98                 _tcscat( lpLongPath, TEXT("\\") );
99                 _tcscat( lpLongPath, aFindFileData.cFileName );
100                 dwResult = _tcslen( lpLongPath );
101             }
102             else
103                 dwResult += _tcslen( aFindFileData.cFileName ) + 1;
104         }
105     }
106 
107     return dwResult;
108 }
109 
110