- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int main()
{
FILE *popen_result;
char buff[512];
char *cmd = "lss";
int temperr = dup(2); // copy where stderr is at the moment
freopen("/dev/null", "w", stderr); // trash stderr reports from popen shell
popen_result = popen(cmd, "r");
if( popen_result==NULL )
printf("Error!\n");
stderr = fdopen(temperr,"w"); // get back stderr
perror(cmd);
if ( feof(popen_result) )
printf("Already at EOF!\n");
if ( ferror(popen_result) )
printf("Already at error!\n");
int err=1;
while(fgets(buff, sizeof(buff), popen_result)!=NULL){
printf(">>> %s", buff);
err=0;
}
if( err==1 )
printf("Error! Command %s not found\n", cmd);
if( feof(popen_result) && err==0)
printf("EOF cmd\n");
pclose(popen_result);
}
Комментарии (0) RSS
Добавить комментарий