MCPcopy Create free account
hub / github.com/WayfireWM/wayfire / read_output

Function read_output

src/debug.cpp:93–127  ·  view source on GitHub ↗

* Execute the given command and read the first line of its output. * * Returns an empty string in case of an error. */

Source from the content-addressed store, hash-verified

91 * Returns an empty string in case of an error.
92 */
93std::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.

Callers 2

locate_executableFunction · 0.85
try_addr2lineFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected