MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_win_stdin_path

Function cbm_win_stdin_path

src/foundation/subprocess.c:873–926  ·  view source on GitHub ↗

Match cbm_path_to_wide's file-path spelling using the process arena. Existing * option conversions retain their legacy heap ownership. */

Source from the content-addressed store, hash-verified

871/* Match cbm_path_to_wide's file-path spelling using the process arena. Existing
872 * option conversions retain their legacy heap ownership. */
873static wchar_t *cbm_win_stdin_path(cbm_subprocess_t *process) {
874 int length =
875 MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, process->stdin_file, -1, NULL, 0);
876 if (length <= 0 || length > 32767) {
877 return NULL;
878 }
879 wchar_t *wide = cbm_arena_alloc(&process->stdout_arena, (size_t)length * sizeof(*wide));
880 if (!wide || MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, process->stdin_file, -1, wide,
881 length) != length) {
882 return NULL;
883 }
884 size_t input_length = (size_t)length - 1U;
885 if (cbm_win_path_has_namespace_prefix(wide, input_length)) {
886 cbm_win_path_normalize_wide_separators(wide);
887 return wide;
888 }
889 if (input_length < 240U) {
890 return wide;
891 }
892 DWORD needed = GetFullPathNameW(wide, 0, NULL, NULL);
893 if (needed == 0U || needed > 32767U) {
894 return NULL;
895 }
896 wchar_t *full = cbm_arena_alloc(&process->stdout_arena, (size_t)needed * sizeof(*full));
897 DWORD copied = full ? GetFullPathNameW(wide, needed, full, NULL) : 0U;
898 if (copied == 0U || copied >= needed) {
899 return NULL;
900 }
901 cbm_win_path_normalize_wide_separators(full);
902 bool drive_absolute =
903 copied >= 3U &&
904 ((full[0] >= L'A' && full[0] <= L'Z') || (full[0] >= L'a' && full[0] <= L'z')) &&
905 full[1] == L':' && full[2] == L'\\';
906 bool unc_absolute = copied >= 5U && full[0] == L'\\' && full[1] == L'\\';
907 if (!drive_absolute && !unc_absolute) {
908 return NULL;
909 }
910 const wchar_t *prefix = drive_absolute ? L"\\\\?\\" : L"\\\\?\\UNC\\";
911 size_t prefix_length = drive_absolute ? 4U : 8U;
912 size_t tail_offset = drive_absolute ? 0U : 2U;
913 size_t tail_length = (size_t)copied - tail_offset;
914 size_t extended_length = prefix_length + tail_length;
915 if (extended_length >= 32767U) {
916 return NULL;
917 }
918 wchar_t *prefixed =
919 cbm_arena_alloc(&process->stdout_arena, (extended_length + 1U) * sizeof(*prefixed));
920 if (!prefixed) {
921 return NULL;
922 }
923 wmemcpy(prefixed, prefix, prefix_length);
924 wmemcpy(prefixed + prefix_length, full + tail_offset, tail_length + 1U);
925 return prefixed;
926}
927
928static void cbm_win_close_spawn_handles(HANDLE nul, HANDLE input, HANDLE log, HANDLE stdout_capture,
929 LPPROC_THREAD_ATTRIBUTE_LIST attrs, bool attrs_init) {

Callers 1

cbm_subprocess_spawn_winFunction · 0.85

Tested by

no test coverage detected