From 32de287bb714ff185d943090622b4cb64287dd78 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 22 May 2022 11:06:14 +0100 Subject: win32: code shrink readlink(2) Use PrintName rather than SubstituteName from the reparse data buffer. This avoids the need to normalise the name. Saves 240 bytes. --- win32/mingw.c | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/win32/mingw.c b/win32/mingw.c index bf9b75a7f..95f5e5687 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1526,30 +1526,6 @@ char *realpath(const char *path, char *resolved_path) return NULL; } -static wchar_t *normalize_ntpath(wchar_t *wbuf) -{ - int i; - /* fix absolute path prefixes */ - if (wbuf[0] == '\\') { - /* strip NT namespace prefixes */ - if (!wcsncmp(wbuf, L"\\??\\", 4) || - !wcsncmp(wbuf, L"\\\\?\\", 4)) - wbuf += 4; - else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12)) - wbuf += 12; - /* replace remaining '...UNC\' with '\\' */ - if (!wcsnicmp(wbuf, L"UNC\\", 4)) { - wbuf += 2; - *wbuf = '\\'; - } - } - /* convert backslashes to slashes */ - for (i = 0; wbuf[i]; i++) - if (wbuf[i] == '\\') - wbuf[i] = '/'; - return wbuf; -} - #define SRPB rptr->SymbolicLinkReparseBuffer ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) { @@ -1570,21 +1546,25 @@ ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) CloseHandle(h); if (status && rptr->ReparseTag == IO_REPARSE_TAG_SYMLINK) { - len = SRPB.SubstituteNameLength/sizeof(WCHAR); - name = SRPB.PathBuffer + SRPB.SubstituteNameOffset/sizeof(WCHAR); + len = SRPB.PrintNameLength/sizeof(WCHAR); + name = SRPB.PathBuffer + SRPB.PrintNameOffset/sizeof(WCHAR); } else if (status && rptr->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) { - len = MRPB.SubstituteNameLength/sizeof(WCHAR); - name = MRPB.PathBuffer + MRPB.SubstituteNameOffset/sizeof(WCHAR); + len = MRPB.PrintNameLength/sizeof(WCHAR); + name = MRPB.PathBuffer + MRPB.PrintNameOffset/sizeof(WCHAR); } if (name) { - name[len] = 0; - name = normalize_ntpath(name); - len = wcslen(name); if (len > bufsiz) len = bufsiz; - if (WideCharToMultiByte(CP_ACP, 0, name, len, buf, bufsiz, 0, 0)) + len = WideCharToMultiByte(CP_ACP, 0, name, len, buf, bufsiz, 0, 0); + if (len) { + int i; + + for (i = 0; i < len; i++) + if (buf[i] == '\\') + buf[i] = '/'; return len; + } } } errno = err_win_to_posix(); -- cgit v1.2.3-55-g6feb