Operating environment
CentOS 6.5
I was wondering if I could get the result in a C program by executing a shell command (eg who) string with execl () etc.
reference http://stackoverflow.com/questions/1776632/how-to-catch-the-ouput-from-a-execl-command
I've tried. The code linked above gets only one line, but below it tries to get multiple lines.
get_execl.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int cmd_quem(void) {
int result;
int pipefd[2];
FILE *cmd_output;
char buf[1024];
int status;
result = pipe(pipefd);
if (result < 0) {
perror("pipe");
exit(-1);
}
result = fork();
if(result < 0) {
exit(-1);
}
if (result == 0) {
dup2(pipefd[1], STDOUT_FILENO); /* Duplicate writing end to stdout */
close(pipefd[0]);
close(pipefd[1]);
execl("/usr/bin/who", "who", NULL);
_exit(1);
}
/* Parent process */
close(pipefd[1]); /* Close writing end of pipe */
cmd_output = fdopen(pipefd[0], "r");
while(1) {
if (feof(cmd_output)) {
break;
}
if (fgets(buf, sizeof buf, cmd_output)) {
printf("Data from who command: %s", buf);
} else {
// printf("No data received.\n");
}
}
wait(&status);
// printf("Child exit status = %d\n", status);
return 0;
}
int main()
{
cmd_quem();
}
result
% ./a.out
Data from who command: wrf tty1 2016-10-21 01:26 (:0)
Data from who command: wrf pts/0 2016-10-21 01:29 (:0.0)
Data from who command: wrf pts/1 2016-10-21 01:32 (:0.0)
who's result
% who
wrf tty1 2016-10-21 01:26 (:0)
wrf pts/0 2016-10-21 01:29 (:0.0)
wrf pts/1 2016-10-21 01:32 (:0.0)
The drawback is that the code is very long.
(Addition 2016/10/21)
@ hurou927 taught me how to use popen ().
This is much simpler and easier to see.
Recommended Posts