test_popen_sh/main.c

24 lines
514 B
C
Raw Normal View History

2024-10-10 20:53:37 +09:00
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const auto cmd = "./subdir/entry.sh";
FILE *f = popen(cmd, "r");
if (f == NULL) {
perror("popen failed");
return EXIT_FAILURE;
}
char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
printf("read: %s", buf);
}
int ret = pclose(f);
if (ret == -1) {
perror("pclose failed");
return EXIT_FAILURE;
};
printf("Return code: %d", ret);
return EXIT_SUCCESS;
}