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,
" ");
124 size_t output_size = 0;
125 size_t output_capacity = 1024;
126 output = malloc(output_capacity);
133 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
134 HANDLE hRead, hWrite;
135 if (!CreatePipe(&hRead, &hWrite, &sa, 0))
141 STARTUPINFO si = {
sizeof(STARTUPINFO)};
142 si.dwFlags = STARTF_USESTDHANDLES;
143 si.hStdOutput = hWrite;
144 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
145 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
146 PROCESS_INFORMATION pi;
147 if (!CreateProcess(NULL, (
char *)cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
158 while (ReadFile(hRead, buffer,
sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
160 if (output_size + bytesRead >= output_capacity)
162 output_capacity *= 2;
163 char *new_output = realloc(output, output_capacity);
167 CloseHandle(pi.hProcess);
168 CloseHandle(pi.hThread);
175 memcpy(output + output_size, buffer, bytesRead);
176 output_size += bytesRead;
178 output[output_size] =
'\0';
180 WaitForSingleObject(pi.hProcess, INFINITE);
181 CloseHandle(pi.hProcess);
182 CloseHandle(pi.hThread);
185 if (pipe(pipefd) == -1)
203 dup2(pipefd[1], STDOUT_FILENO);
205 execvp(argv[0], argv);
213 while ((bytesRead = read(pipefd[0], buffer,
sizeof(buffer))) > 0)
215 if (output_size + bytesRead >= output_capacity)
217 output_capacity *= 2;
218 char *new_output = realloc(output, output_capacity);
228 memcpy(output + output_size, buffer, bytesRead);
229 output_size += bytesRead;
231 output[output_size] =
'\0';
234 waitpid(pid, &status, 0);
248 size_t output_size = 0;
249 size_t output_capacity = 1024;
250 output = malloc(output_capacity);
257 SECURITY_ATTRIBUTES sa = {
sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
258 HANDLE hRead, hWrite;
259 if (!CreatePipe(&hRead, &hWrite, &sa, 0))
265 STARTUPINFO si = {
sizeof(STARTUPINFO)};
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);