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