13 if (!cmd || !*cmd)
return NULL;
14 size_t len = strlen(cmd);
15 char* copy = malloc(len + 1);
16 if (!copy)
return NULL;
19 strcpy_s(copy, len + 1, cmd);
27 char* token = strtok_s(copy,
" ", &context);
29 char* token = strtok(copy,
" ");
34 token = strtok_s(NULL,
" ", &context);
36 token = strtok(NULL,
" ");
41 strcpy_s(copy, len + 1, cmd);
46 char** argv = malloc((*argc + 1) *
sizeof(
char*));
47 if (!argv) { free(copy);
return NULL; }
51 token = strtok_s(copy,
" ", &context);
53 token = strtok(copy,
" ");
56 argv[i] = malloc(strlen(token) + 1);
58 for (
int j = 0; j < i; j++) free(argv[j]);
64 strcpy_s(argv[i], strlen(token) + 1, token);
66 strcpy(argv[i], token);
70 token = strtok_s(NULL,
" ", &context);
72 token = strtok(NULL,
" ");
103 if (!argv)
return NULL;
105 size_t output_size = 0;
106 size_t output_capacity = 1024;
107 output = malloc(output_capacity);
113 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
114 HANDLE hRead, hWrite;
115 if (!CreatePipe(&hRead, &hWrite, &sa, 0)) {
120 STARTUPINFO si = {
sizeof(STARTUPINFO) };
121 si.dwFlags = STARTF_USESTDHANDLES;
122 si.hStdOutput = hWrite;
123 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
124 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
125 PROCESS_INFORMATION pi;
126 if (!CreateProcess(NULL, (
char*)cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
136 while (ReadFile(hRead, buffer,
sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) {
137 if (output_size + bytesRead >= output_capacity) {
138 output_capacity *= 2;
139 char* new_output = realloc(output, output_capacity);
142 CloseHandle(pi.hProcess);
143 CloseHandle(pi.hThread);
150 memcpy(output + output_size, buffer, bytesRead);
151 output_size += bytesRead;
153 output[output_size] =
'\0';
155 WaitForSingleObject(pi.hProcess, INFINITE);
156 CloseHandle(pi.hProcess);
157 CloseHandle(pi.hThread);
160 if (pipe(pipefd) == -1) {
175 dup2(pipefd[1], STDOUT_FILENO);
177 execvp(argv[0], argv);
183 while ((bytesRead = read(pipefd[0], buffer,
sizeof(buffer))) > 0) {
184 if (output_size + bytesRead >= output_capacity) {
185 output_capacity *= 2;
186 char* new_output = realloc(output, output_capacity);
195 memcpy(output + output_size, buffer, bytesRead);
196 output_size += bytesRead;
198 output[output_size] =
'\0';
201 waitpid(pid, &status, 0);
211 if (!argv)
return NULL;
213 size_t output_size = 0;
214 size_t output_capacity = 1024;
215 output = malloc(output_capacity);
221 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
222 HANDLE hRead, hWrite;
223 if (!CreatePipe(&hRead, &hWrite, &sa, 0)) {
228 STARTUPINFO si = {
sizeof(STARTUPINFO) };
229 si.dwFlags = STARTF_USESTDHANDLES;
230 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
231 si.hStdError = hWrite;
232 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
233 PROCESS_INFORMATION pi;
234 if (!CreateProcess(NULL, (
char*)cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
244 while (ReadFile(hRead, buffer,
sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) {
245 if (output_size + bytesRead >= output_capacity) {
246 output_capacity *= 2;
247 char* new_output = realloc(output, output_capacity);
250 CloseHandle(pi.hProcess);
251 CloseHandle(pi.hThread);
258 memcpy(output + output_size, buffer, bytesRead);
259 output_size += bytesRead;
261 output[output_size] =
'\0';
263 WaitForSingleObject(pi.hProcess, INFINITE);
264 CloseHandle(pi.hProcess);
265 CloseHandle(pi.hThread);
268 if (pipe(pipefd) == -1) {
283 dup2(pipefd[1], STDERR_FILENO);
285 execvp(argv[0], argv);
291 while ((bytesRead = read(pipefd[0], buffer,
sizeof(buffer))) > 0) {
292 if (output_size + bytesRead >= output_capacity) {
293 output_capacity *= 2;
294 char* new_output = realloc(output, output_capacity);
303 memcpy(output + output_size, buffer, bytesRead);
304 output_size += bytesRead;
306 output[output_size] =
'\0';
309 waitpid(pid, &status, 0);