* Execute the given command and read the first line of its output. * * Returns an empty string in case of an error. */
| 91 | * Returns an empty string in case of an error. |
| 92 | */ |
| 93 | std::string read_output(std::string command) |
| 94 | { |
| 95 | // Open a pipe stream to read the output of the process `command`. |
| 96 | FILE *file = popen(command.c_str(), "r"); |
| 97 | if (!file) |
| 98 | { |
| 99 | // popen failed. |
| 100 | return {}; |
| 101 | } |
| 102 | |
| 103 | // Read the first line, up till and including the first newline if any, into a buffer. |
| 104 | char buffer[MAX_FUNCTION_NAME + 1]; // +1 for the terminating zero. |
| 105 | char *line_as_c_str = fgets(buffer, sizeof(buffer), file); |
| 106 | |
| 107 | // We're done with the pipe stream now. |
| 108 | pclose(file); |
| 109 | |
| 110 | if (!line_as_c_str) |
| 111 | { |
| 112 | // fgets returned an error. |
| 113 | return {}; |
| 114 | } |
| 115 | |
| 116 | // If fgets returns non-NULL then the string is guaranteed to be zero terminated. |
| 117 | size_t len = std::strlen(line_as_c_str); |
| 118 | |
| 119 | // Strip the trailing newline, if any. |
| 120 | if ((len > 0) && (line_as_c_str[len - 1] == '\n')) |
| 121 | { |
| 122 | --len; |
| 123 | line_as_c_str[len] = 0; |
| 124 | } |
| 125 | |
| 126 | return {buffer, len}; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Try to find the correct path to the given executable. |
no outgoing calls
no test coverage detected