Last active
May 30, 2024 23:47
-
-
Save jeremyd2019/8e088a72dfef44ee29ed3442957c1e65 to your computer and use it in GitHub Desktop.
list windows volume roots as `\0`-delimited cygwin paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <wchar.h> | |
#include <sys/cygwin.h> | |
/* based on the starting point | |
https://learn.microsoft.com/en-us/windows/win32/fileio/displaying-volume-paths | |
*/ | |
void main(void) | |
{ | |
DWORD Error = ERROR_SUCCESS; | |
HANDLE FindHandle = INVALID_HANDLE_VALUE; | |
BOOL Success = FALSE; | |
WCHAR VolumeName[MAX_PATH] = L""; | |
FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName)); | |
if (FindHandle == INVALID_HANDLE_VALUE) | |
{ | |
Error = GetLastError(); | |
fprintf(stderr, "FindFirstVolumeW failed with error code %d\n", Error); | |
return; | |
} | |
do | |
{ | |
DWORD CharCount = MAX_PATH + 1; | |
PWCHAR Names = (PWCHAR) malloc(CharCount * sizeof(WCHAR)); | |
if ( !Names ) | |
return; | |
while (!(Success = GetVolumePathNamesForVolumeNameW( | |
VolumeName, Names, CharCount, &CharCount | |
)) && | |
GetLastError() == ERROR_MORE_DATA && | |
(Names = (PWCHAR) realloc(Names, CharCount * sizeof(WCHAR)))); | |
if ( !Names ) | |
return; | |
if ( Success ) | |
{ | |
for ( PWCHAR NameIdx = Names; | |
NameIdx[0] != L'\0'; | |
NameIdx += wcslen(NameIdx) + 1 ) | |
{ | |
PCHAR cygpath = cygwin_create_path(CCP_WIN_W_TO_POSIX, NameIdx); | |
// strlen + 1 to also output terminating NUL | |
fwrite(cygpath, 1, strlen(cygpath)+1, stdout); | |
free(cygpath); | |
} | |
} | |
free(Names); | |
} while (Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName))); | |
if ( !Success ) | |
{ | |
Error = GetLastError(); | |
if (Error != ERROR_NO_MORE_FILES) | |
fprintf(stderr, "FindNextVolumeW failed with error code %d\n", Error); | |
} | |
FindVolumeClose(FindHandle); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment