|
| 1 | +#define COLORS |
| 2 | + |
| 3 | +#include "headers/exec.h" |
| 4 | +#include "headers/escape.h" |
| 5 | + |
| 6 | +#if _WIN32 |
| 7 | + |
| 8 | +BOOL FindExecutableInPath(LPCSTR executable, LPCSTR *path_found) { |
| 9 | + LPCSTR env_var = getenv("PATH"); |
| 10 | + if (env_var == NULL) |
| 11 | + fprintf(stderr, |
| 12 | + RED "!%s Failed to find path variable, is your windows pc ok?", |
| 13 | + CLEAR) return FALSE; |
| 14 | + |
| 15 | + LPCSTR path = env_var; |
| 16 | + do { |
| 17 | + size_t len = strlen(path); |
| 18 | + if (len > 0 && path[len - 1] == ';') { |
| 19 | + path[len - 1] = '\0'; // Remove trailing semicolon |
| 20 | + } |
| 21 | + if (_stricmp(executable, path) == 0) { |
| 22 | + *path_found = path; |
| 23 | + return TRUE; |
| 24 | + } |
| 25 | + path += len + 1; // Move to next directory in PATH |
| 26 | + } while (*path); |
| 27 | + |
| 28 | + return FALSE; |
| 29 | +} |
| 30 | + |
| 31 | +#endif |
| 32 | + |
| 33 | +int execute(char *executable, char **argv, int status) { |
| 34 | + |
| 35 | +#if __linux__ |
| 36 | + pid_t pid = fork(); |
| 37 | + |
| 38 | + if (pid < 0) { |
| 39 | + perror("failed to fork"); |
| 40 | + } else if (pid == 0) { |
| 41 | + if (execvp(executable, argv) == -1) { |
| 42 | + if (errno == ENOENT) { |
| 43 | + fprintf(stderr, RED "!%s %s: Command not found\n", CLEAR, executable); |
| 44 | + } else { |
| 45 | + perror("Failed to exec to childprocess. (execvp)"); |
| 46 | + } |
| 47 | + return -1; |
| 48 | + } |
| 49 | + } else { |
| 50 | + wait(&status); |
| 51 | + } |
| 52 | + |
| 53 | +#elif _WIN32 |
| 54 | + |
| 55 | + LPCSTR win_executable = (LPCSTR)executable; |
| 56 | + LPCSTR path_found = {0}; |
| 57 | + |
| 58 | + if (find_executable_in_path(win_executable, &path_found)) { |
| 59 | + STARTUPINFO start_i; |
| 60 | + PROCESS_INFORMATION proc_i; |
| 61 | + |
| 62 | + ZeroMemory(&start_i, sizeof(start_i)); |
| 63 | + start_i.cb = sizeof(start_i); |
| 64 | + ZeroMemory(&proc_i, sizeof(proc_i)); |
| 65 | + |
| 66 | + LPSTR full_path = malloc(strlen(path_found) + strlen(win_executable) + 2); |
| 67 | + strlcpy(full_path, path_found, strlen(full_path)); |
| 68 | + strlcat(full_path, "\\", 2); |
| 69 | + strlcat(full_path, win_executable, strlen(win_executable)); |
| 70 | + |
| 71 | + if (!CreateProcess(0, full_path, 0, 0, FALSE, 0, 0, 0, &start_i, &proc_i)) { |
| 72 | + fprintf(stderr, RED "!%s CreateProcess failed (%d).\n", CLEAR, |
| 73 | + GetLastError()); |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + CloseHandle(proc_i.hProcess); |
| 78 | + CloseHandle(proc_i.hThread); |
| 79 | + free(full_path); |
| 80 | + } else { |
| 81 | + // Signify nonexistent executable. |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | +#endif |
| 86 | + return 0; |
| 87 | +} |
0 commit comments