Skip to content

Commit

Permalink
Merge pull request haskell#3657 from NovaDenizen/master
Browse files Browse the repository at this point in the history
Now allocates properly sized result buffer.
  • Loading branch information
ezyang authored Aug 1, 2016
2 parents 9fea99d + 3c1b619 commit 5a1f7d5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Cabal/Distribution/Compat/GetShortPathName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ foreign import WINAPI unsafe "windows.h GetShortPathNameW"

-- | On Windows, retrieves the short path form of the specified path. On
-- non-Windows, does nothing. See https://github.com/haskell/cabal/issues/3185.
--
-- From MS's GetShortPathName docs:
--
-- Passing NULL for [the second] parameter and zero for cchBuffer
-- will always return the required buffer size for a
-- specified lpszLongPath.
--
getShortPathName :: FilePath -> IO FilePath
getShortPathName path =
Win32.withTString path $ \c_path ->
Win32.withTString path $ \c_path -> do
c_len <- Win32.failIfZero "GetShortPathName #1 failed!" $
c_GetShortPathName c_path Win32.nullPtr 0
let arr_len = fromIntegral c_len
allocaArray arr_len $ \c_out -> do
void $ Win32.failIfZero "GetShortPathName failed!" $
void $ Win32.failIfZero "GetShortPathName #2 failed!" $
c_GetShortPathName c_path c_out c_len
Win32.peekTString c_out
where
arr_len = length path + 1
c_len = fromIntegral arr_len

#else

Expand Down

0 comments on commit 5a1f7d5

Please sign in to comment.