-
-
Save h4k1m0u/f0802d694d675fec43fc6914c7de6b98 to your computer and use it in GitHub Desktop.
Open stream to read output of shell command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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