• C

exec'ing and grabbing output

I want to exec "ifconfig"  in my program and redirect the output of ifconfig so my program can read line by line the output.. i guess the way to do it is redirect the output to a pty? or what..? don't know where to start.. any help appreciated (i'm using LINUX though.. i don't want to see any windows crap)

thanx
LVL 1
ramsayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
popen
0
ramsayAuthor Commented:
man popen.. looked cool.. let's say i want to do this somewhat manually tho..

show me how i would create a pipe.. and then exec an app and have all it's data output as input (say line by line) in another app (see original question)..

so what i'm after is source code that does all this.. as i don't want to use popen.. i want to do it manually.. with pipe? and exec? etc.

points doubled

0
ramsayAuthor Commented:
man popen.. looked cool.. let's say i want to do this somewhat manually tho..

show me how i would create a pipe.. and then exec an app and have all it's data output as input (say line by line) in another app (see original question)..

so what i'm after is source code that does all this.. as i don't want to use popen.. i want to do it manually.. with pipe? and exec? etc.

points doubled

0
ozoCommented:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
extern int errno;
#define put     1
#define take    0
#define prog "/usr/etc/ifconfig"
main(int argc, char *const *argv, char *const *envp){
        int in[2];
        int out[2];
        int pid;
        FILE *input;
        FILE *outtake;
        int c;
        if( pipe(in) < 0 ){ perror("pipe in failed"); exit(errno); }
        if( pipe(out) < 0 ){ perror("pipe out failed"); exit(errno); }
        if( (pid = fork()) < 0 ){
                perror("fork failed");
                exit(errno);
        }else if( pid == 0 ){
                close(in[put]);
                close(out[take]);
                dup2(in[take],fileno(stdin));
                dup2(out[put],fileno(stdout));
                execve(prog,argv,envp);
                perror("exec " prog "failed");
                exit(errno);
        }else{
                close(in[take]);
                close(out[put]);
                input = fdopen(in[put],"w");
                outtake = fdopen(out[take],"r");
        }



        printf("pid=%d:\n",pid);
        /*fputs("input to " prog "\n",input);
        fclose(input);*/
       
        printf(prog " says:\n");
        while( (c = fgetc(outtake)) != EOF ){
                printf("%c",c);
        }
        printf("\nend of "prog"output\n");

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ramsayAuthor Commented:
i'm very happy with your answer ozo.. it worked great!!! -- i added a bonus 50 points..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.