Skip to content

Instantly share code, notes, and snippets.

@h4k1m0u
Created May 11, 2022 19:04
Show Gist options
  • Save h4k1m0u/f0802d694d675fec43fc6914c7de6b98 to your computer and use it in GitHub Desktop.
Save h4k1m0u/f0802d694d675fec43fc6914c7de6b98 to your computer and use it in GitHub Desktop.
Open stream to read output of shell command
#include <stdio.h>
/* Inspired by: https://hackaday.com/2022/03/16/linux-fu-simple-pipes/ */
int main() {
// creates a pipe between calling pgm & executed command (`ls`) and
// returns a stream to read from the pipe
FILE* pipe = popen("ls -l", "r");
// read the output of shell command from stream
char buffer[64];
while (!feof(pipe)) {
char* ret = fgets(buffer, 64, pipe);
if (ret != NULL) {
printf("buffer: %s", buffer);
}
}
pclose(pipe);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment