DWORD WINAPI GetLongPathNameU(LPCTSTR lpShortName, LPTSTR lpBuf, DWORD nBufferLength) { WCHAR *pwszShortName = NULL; WCHAR *pwBuf = NULL; int n; int lResult; int lName = lstrlen(lpShortName); int lNameNul = lName + 1; int lwBuf = lNameNul + MAX_PATH; /* In most cases, the long path will be shorter than MAX_PATH. If not, the buffer will be extended below. */ DEBUG_ENTER(("GetLongPathNameU(\"%s\", %p, %d);\n", lpShortName, lpBuf, nBufferLength)); pwszShortName = MultiByteToNewWidePath(CP_UTF8, lpShortName); if (!pwszShortName) { out_of_mem: RETURN_INT_COMMENT(0, ("Not enough memory\n")); } realloc_wBuf: pwBuf = GlobalAlloc(GMEM_FIXED, sizeof(WCHAR) * lwBuf); if (!pwBuf) { free(pwszShortName); goto out_of_mem; } lResult = (int)GetLongPathNameW(pwszShortName, pwBuf, lwBuf); if (lResult > lwBuf) { /* In the possible but unlikely case that the result does not fit in pwBuf, */ GlobalFree(pwBuf); /* then extend the buffer and retry. */ lwBuf = lResult; goto realloc_wBuf; } free(pwszShortName); /* We won't need this buffer anymore */ if (!lResult) { GlobalFree(pwBuf); RETURN_INT_COMMENT(0, ("GetLongPathNameW() failed\n")); } /* nRead = UnicodeToBytes(pwStr, len, buf, bufsize); */ n = WideCharToMultiByte(CP_UTF8, /* CodePage, (CP_ACP, CP_OEMCP, CP_UTF8, ...) */ 0, /* dwFlags, */ pwBuf, /* lpWideCharStr, */ lResult + 1, /* cchWideChar, */ lpBuf, /* lpMultiByteStr, */ (int)nBufferLength, /* cbMultiByte, */ NULL, /* lpDefaultChar, */ NULL /* lpUsedDefaultChar */ ); GlobalFree(pwBuf); if (!n) RETURN_INT_COMMENT(0, ("Failed to convert the Long name from Unicode\n")); n -= 1; /* Do not count the final NUL */ /* Remove the long pathname \\?\ prefix, if it was not there before */ if ((!strncmp(lpBuf, "\\\\?\\", 4)) && strncmp(lpShortName, "\\\\?\\", 4)) { n = TrimLongPathPrefix(lpBuf); } RETURN_INT_COMMENT(n, ("\"%s\"\n", lpBuf)); }
int _accessM(const char *pszName, int iMode, UINT cp) { WCHAR *pwszName; int iAccess; /* Convert the pathname to a unicode string, with the proper extension prefixes if it's longer than 260 bytes */ pwszName = MultiByteToNewWidePath(cp, pszName); if (!pwszName) return -1; /* The C library returns errno=EINVAL for iMode X_OK. As all files are executable in Windows, change it to an existence test. */ if (iMode == X_OK) iMode = F_OK; iAccess = _waccess(pwszName, iMode); free(pwszName); return iAccess; }
int chdirM(const char *pszDir, UINT cp) { WCHAR *pwszDir; BOOL bDone; int iErr = 0; DEBUG_ENTER(("chdir(\"%s\");\n", pszDir)); /* Convert the pathname to a unicode string, with the proper extension prefixes if it's longer than 260 bytes */ pwszDir = MultiByteToNewWidePath(cp, pszDir); if (!pwszDir) return -1; bDone = SetCurrentDirectoryW(pwszDir); if (!bDone) { errno = Win32ErrorToErrno(); iErr = -1; } free(pwszDir); DEBUG_QUIET_LEAVE(); return iErr; }