16 size_t len = strlen(cmd);
17 char *copy = malloc(len + 1);
22 strcpy_s(copy, len + 1, cmd);
30 char *token = strtok_s(copy,
" ", &context);
32 char *token = strtok(copy,
" ");
38 token = strtok_s(NULL,
" ", &context);
40 token = strtok(NULL,
" ");
45 strcpy_s(copy, len + 1, cmd);
50 char **argv = malloc((*argc + 1) *
sizeof(
char *));
59 token = strtok_s(copy,
" ", &context);
61 token = strtok(copy,
" ");
65 argv[i] = malloc(strlen(token) + 1);
68 for (
int j = 0; j < i; j++)
75 strcpy_s(argv[i], strlen(token) + 1, token);
77 strcpy(argv[i], token);
81 token = strtok_s(NULL,
" ", &context);
83 token = strtok(NULL,
" ");
122 size_t output_size = 0;
123 size_t output_capacity = 1024;
124 output = malloc(output_capacity);
131 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
132 HANDLE hRead, hWrite;
133 if (!CreatePipe(&hRead, &hWrite, &sa, 0))
139 STARTUPINFO si = {0};
141 si.dwFlags = STARTF_USESTDHANDLES;
142 si.hStdOutput = hWrite;
143 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
144 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
145 PROCESS_INFORMATION pi;
146 if (!CreateProcess(NULL, (
char *)cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
157 while (ReadFile(hRead, buffer,
sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
159 if (output_size + bytesRead >= output_capacity)
161 output_capacity *= 2;
162 char *new_output = realloc(output, output_capacity);
166 CloseHandle(pi.hProcess);
167 CloseHandle(pi.hThread);
174 memcpy(output + output_size, buffer, bytesRead);
175 output_size += bytesRead;
177 output[output_size] =
'\0';
179 WaitForSingleObject(pi.hProcess, INFINITE);
180 CloseHandle(pi.hProcess);
181 CloseHandle(pi.hThread);
184 if (pipe(pipefd) == -1)
202 dup2(pipefd[1], STDOUT_FILENO);
204 execvp(argv[0], argv);
212 while ((bytesRead = read(pipefd[0], buffer,
sizeof(buffer))) > 0)
214 if (output_size + bytesRead >= output_capacity)
216 output_capacity *= 2;
217 char *new_output = realloc(output, output_capacity);
227 memcpy(output + output_size, buffer, bytesRead);
228 output_size += bytesRead;
230 output[output_size] =
'\0';
233 waitpid(pid, &status, 0);
247 size_t output_size = 0;
248 size_t output_capacity = 1024;
249 output = malloc(output_capacity);
256 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
257 HANDLE hRead, hWrite;
258 if (!CreatePipe(&hRead, &hWrite, &sa, 0))
264 STARTUPINFO si = {0};
266 si.dwFlags = STARTF_USESTDHANDLES;
267 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
268 si.hStdError = hWrite;
269 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
270 PROCESS_INFORMATION pi;
271 if (!CreateProcess(NULL, (
char *)cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
282 while (ReadFile(hRead, buffer,
sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
284 if (output_size + bytesRead >= output_capacity)
286 output_capacity *= 2;
287 char *new_output = realloc(output, output_capacity);
291 CloseHandle(pi.hProcess);
292 CloseHandle(pi.hThread);
299 memcpy(output + output_size, buffer, bytesRead);
300 output_size += bytesRead;
302 output[output_size] =
'\0';
304 WaitForSingleObject(pi.hProcess, INFINITE);
305 CloseHandle(pi.hProcess);
306 CloseHandle(pi.hThread);
309 if (pipe(pipefd) == -1)
327 dup2(pipefd[1], STDERR_FILENO);
329 execvp(argv[0], argv);
337 while ((bytesRead = read(pipefd[0], buffer,
sizeof(buffer))) > 0)
339 if (output_size + bytesRead >= output_capacity)
341 output_capacity *= 2;
342 char *new_output = realloc(output, output_capacity);
352 memcpy(output + output_size, buffer, bytesRead);
353 output_size += bytesRead;
355 output[output_size] =
'\0';
358 waitpid(pid, &status, 0);