| 931 | #undef KWSYSPE_IDX_CHK |
| 932 | |
| 933 | void kwsysProcess_Execute(kwsysProcess* cp) |
| 934 | { |
| 935 | int i; |
| 936 | |
| 937 | /* Do not execute a second time. */ |
| 938 | if (!cp || cp->State == kwsysProcess_State_Executing) { |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | /* Make sure we have something to run. */ |
| 943 | if (cp->NumberOfCommands < 1) { |
| 944 | strcpy(cp->ErrorMessage, "No command"); |
| 945 | cp->State = kwsysProcess_State_Error; |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | /* Initialize the control structure for a new process. */ |
| 950 | if (!kwsysProcessInitialize(cp)) { |
| 951 | strcpy(cp->ErrorMessage, "Out of memory"); |
| 952 | cp->State = kwsysProcess_State_Error; |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | /* Save the real working directory of this process and change to |
| 957 | the working directory for the child processes. This is needed |
| 958 | to make pipe file paths evaluate correctly. */ |
| 959 | if (cp->WorkingDirectory) { |
| 960 | if (!GetCurrentDirectoryW(cp->RealWorkingDirectoryLength, |
| 961 | cp->RealWorkingDirectory)) { |
| 962 | kwsysProcessCleanup(cp, GetLastError()); |
| 963 | return; |
| 964 | } |
| 965 | if (!SetCurrentDirectoryW(cp->WorkingDirectory)) { |
| 966 | kwsysProcessCleanup(cp, GetLastError()); |
| 967 | return; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /* Setup the stdin pipe for the first process. */ |
| 972 | if (cp->PipeFileSTDIN) { |
| 973 | /* Create a handle to read a file for stdin. */ |
| 974 | wchar_t* wstdin = kwsysEncoding_DupToWide(cp->PipeFileSTDIN); |
| 975 | DWORD error; |
| 976 | cp->PipeChildStd[0] = |
| 977 | CreateFileW(wstdin, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, |
| 978 | OPEN_EXISTING, 0, 0); |
| 979 | error = GetLastError(); /* Check now in case free changes this. */ |
| 980 | free(wstdin); |
| 981 | if (cp->PipeChildStd[0] == INVALID_HANDLE_VALUE) { |
| 982 | kwsysProcessCleanup(cp, error); |
| 983 | return; |
| 984 | } |
| 985 | } else if (cp->PipeSharedSTDIN) { |
| 986 | /* Share this process's stdin with the child. */ |
| 987 | kwsysProcessSetupSharedPipe(STD_INPUT_HANDLE, &cp->PipeChildStd[0]); |
| 988 | } else if (cp->PipeNativeSTDIN[0]) { |
| 989 | /* Use the provided native pipe. */ |
| 990 | kwsysProcessSetupPipeNative(cp->PipeNativeSTDIN[0], &cp->PipeChildStd[0]); |
nothing calls this directly
no test coverage detected
searching dependent graphs…