| 1 |
// copied from: https://github.com/wheybags/simple_exec/blob/5a74c507c4ce1b2bb166177ead4cca7cfa23cb35/simple_exec.h |
| 2 |
|
| 3 |
// simple_exec.h, single header library to run external programs + retrieve their status code and output (unix only for now) |
| 4 |
// |
| 5 |
// do this: |
| 6 |
// #define SIMPLE_EXEC_IMPLEMENTATION |
| 7 |
// before you include this file in *one* C or C++ file to create the implementation. |
| 8 |
// i.e. it should look like this: |
| 9 |
// #define SIMPLE_EXEC_IMPLEMENTATION |
| 10 |
// #include "simple_exec.h" |
| 11 |
|
| 12 |
#ifndef SIMPLE_EXEC_H |
| 13 |
#define SIMPLE_EXEC_H |
| 14 |
|
| 15 |
int runCommand(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* command, ...); |
| 16 |
int runCommandArray(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* const* allArgs); |
| 17 |
|
| 18 |
#endif // SIMPLE_EXEC_H |
| 19 |
|
| 20 |
#ifdef SIMPLE_EXEC_IMPLEMENTATION |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <assert.h> |
| 27 |
#include <sys/wait.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <fcntl.h> |
| 30 |
|
| 31 |
#define release_assert(exp) { if (!(exp)) { abort(); } } |
| 32 |
|
| 33 |
enum PIPE_FILE_DESCRIPTORS |
| 34 |
{ |
| 35 |
READ_FD = 0, |
| 36 |
WRITE_FD = 1 |
| 37 |
}; |
| 38 |
|
| 39 |
enum RUN_COMMAND_ERROR |
| 40 |
{ |
| 41 |
COMMAND_RAN_OK = 0, |
| 42 |
COMMAND_NOT_FOUND = 1 |
| 43 |
}; |
| 44 |
|
| 45 |
int runCommandArray(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* const* allArgs) |
| 46 |
{ |
| 47 |
// adapted from: https://stackoverflow.com/a/479103 |
| 48 |
|
| 49 |
int bufferSize = 256; |
| 50 |
char buffer[bufferSize + 1]; |
| 51 |
|
| 52 |
int dataReadFromChildDefaultSize = bufferSize * 5; |
| 53 |
int dataReadFromChildSize = dataReadFromChildDefaultSize; |
| 54 |
int dataReadFromChildUsed = 0; |
| 55 |
char* dataReadFromChild = (char*)malloc(dataReadFromChildSize); |
| 56 |
|
| 57 |
|
| 58 |
int parentToChild[2]; |
| 59 |
release_assert(pipe(parentToChild) == 0); |
| 60 |
|
| 61 |
int childToParent[2]; |
| 62 |
release_assert(pipe(childToParent) == 0); |
| 63 |
|
| 64 |
int errPipe[2]; |
| 65 |
release_assert(pipe(errPipe) == 0); |
| 66 |
|
| 67 |
pid_t pid; |
| 68 |
switch( pid = fork() ) |
| 69 |
{ |
| 70 |
case -1: |
| 71 |
{ |
| 72 |
release_assert(0 && "Fork failed"); |
| 73 |
break; |
| 74 |
} |
| 75 |
|
| 76 |
case 0: // child |
| 77 |
{ |
| 78 |
release_assert(dup2(parentToChild[READ_FD ], STDIN_FILENO ) != -1); |
| 79 |
release_assert(dup2(childToParent[WRITE_FD], STDOUT_FILENO) != -1); |
| 80 |
|
| 81 |
if(includeStdErr) |
| 82 |
{ |
| 83 |
release_assert(dup2(childToParent[WRITE_FD], STDERR_FILENO) != -1); |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
int devNull = open("/dev/null", O_WRONLY); |
| 88 |
release_assert(dup2(devNull, STDERR_FILENO) != -1); |
| 89 |
} |
| 90 |
|
| 91 |
// unused |
| 92 |
release_assert(close(parentToChild[WRITE_FD]) == 0); |
| 93 |
release_assert(close(childToParent[READ_FD ]) == 0); |
| 94 |
release_assert(close(errPipe[READ_FD]) == 0); |
| 95 |
|
| 96 |
const char* command = allArgs[0]; |
| 97 |
execvp(command, allArgs); |
| 98 |
|
| 99 |
char err = 1; |
| 100 |
ssize_t result = write(errPipe[WRITE_FD], &err, 1); |
| 101 |
release_assert(result != -1); |
| 102 |
|
| 103 |
close(errPipe[WRITE_FD]); |
| 104 |
close(parentToChild[READ_FD]); |
| 105 |
close(childToParent[WRITE_FD]); |
| 106 |
|
| 107 |
exit(0); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
default: // parent |
| 112 |
{ |
| 113 |
// unused |
| 114 |
release_assert(close(parentToChild[READ_FD]) == 0); |
| 115 |
release_assert(close(childToParent[WRITE_FD]) == 0); |
| 116 |
release_assert(close(errPipe[WRITE_FD]) == 0); |
| 117 |
|
| 118 |
while(1) |
| 119 |
{ |
| 120 |
ssize_t bytesRead = 0; |
| 121 |
switch(bytesRead = read(childToParent[READ_FD], buffer, bufferSize)) |
| 122 |
{ |
| 123 |
case 0: // End-of-File, or non-blocking read. |
| 124 |
{ |
| 125 |
int status = 0; |
| 126 |
release_assert(waitpid(pid, &status, 0) == pid); |
| 127 |
|
| 128 |
// done with these now |
| 129 |
release_assert(close(parentToChild[WRITE_FD]) == 0); |
| 130 |
release_assert(close(childToParent[READ_FD]) == 0); |
| 131 |
|
| 132 |
char errChar = 0; |
| 133 |
ssize_t result = read(errPipe[READ_FD], &errChar, 1); |
| 134 |
release_assert(result != -1); |
| 135 |
close(errPipe[READ_FD]); |
| 136 |
|
| 137 |
if(errChar) |
| 138 |
{ |
| 139 |
free(dataReadFromChild); |
| 140 |
return COMMAND_NOT_FOUND; |
| 141 |
} |
| 142 |
|
| 143 |
// free any un-needed memory with realloc + add a null terminator for convenience |
| 144 |
dataReadFromChild = (char*)realloc(dataReadFromChild, dataReadFromChildUsed + 1); |
| 145 |
dataReadFromChild[dataReadFromChildUsed] = '\0'; |
| 146 |
|
| 147 |
if(stdOut != NULL) |
| 148 |
*stdOut = dataReadFromChild; |
| 149 |
else |
| 150 |
free(dataReadFromChild); |
| 151 |
|
| 152 |
if(stdOutByteCount != NULL) |
| 153 |
*stdOutByteCount = dataReadFromChildUsed; |
| 154 |
if(returnCode != NULL) |
| 155 |
*returnCode = WEXITSTATUS(status); |
| 156 |
|
| 157 |
return COMMAND_RAN_OK; |
| 158 |
} |
| 159 |
case -1: |
| 160 |
{ |
| 161 |
release_assert(0 && "read() failed"); |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
default: |
| 166 |
{ |
| 167 |
if(dataReadFromChildUsed + bytesRead + 1 >= dataReadFromChildSize) |
| 168 |
{ |
| 169 |
dataReadFromChildSize += dataReadFromChildDefaultSize; |
| 170 |
dataReadFromChild = (char*)realloc(dataReadFromChild, dataReadFromChildSize); |
| 171 |
} |
| 172 |
|
| 173 |
memcpy(dataReadFromChild + dataReadFromChildUsed, buffer, bytesRead); |
| 174 |
dataReadFromChildUsed += bytesRead; |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
int runCommand(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* command, ...) |
| 184 |
{ |
| 185 |
va_list vl; |
| 186 |
va_start(vl, command); |
| 187 |
|
| 188 |
char* currArg = NULL; |
| 189 |
|
| 190 |
int allArgsInitialSize = 16; |
| 191 |
int allArgsSize = allArgsInitialSize; |
| 192 |
char** allArgs = (char**)malloc(sizeof(char*) * allArgsSize); |
| 193 |
allArgs[0] = command; |
| 194 |
|
| 195 |
int i = 1; |
| 196 |
do |
| 197 |
{ |
| 198 |
currArg = va_arg(vl, char*); |
| 199 |
allArgs[i] = currArg; |
| 200 |
|
| 201 |
i++; |
| 202 |
|
| 203 |
if(i >= allArgsSize) |
| 204 |
{ |
| 205 |
allArgsSize += allArgsInitialSize; |
| 206 |
allArgs = (char**)realloc(allArgs, sizeof(char*) * allArgsSize); |
| 207 |
} |
| 208 |
|
| 209 |
} while(currArg != NULL); |
| 210 |
|
| 211 |
va_end(vl); |
| 212 |
|
| 213 |
int retval = runCommandArray(stdOut, stdOutByteCount, returnCode, includeStdErr, allArgs); |
| 214 |
free(allArgs); |
| 215 |
return retval; |
| 216 |
} |
| 217 |
|
| 218 |
#endif //SIMPLE_EXEC_IMPLEMENTATION |