1843366 Members
3777 Online
110215 Solutions
New Discussion

C code and stdin

 
Marc Ahrendt
Super Advisor

C code and stdin

i do not know how to get my C code (c_code) to take stdin and pass it as stdin to a UNIX command (unix_cmd) given as an argument ...actually the c_code does many things but below is the part i am having trouble with

also note that there may be an argument/s for the unix_cmd like unix_cmd_arg1 etc...


echo fileA | c_code unix_cmd unix_cmd_arg1


i want the above to behave as if i did the following


echo fileA | unix_cmd unix_cmd_arg1


basically my c_code C program is tweaking certain HP-UX environment settings and is to be used as a "wrapper" for some application binary files that some of my users need to run


i hope i explained this clear enough and understand that i am very rusty on C to the point where i am having problems knowing what i even need to make this work
hola
2 REPLIES 2
Jannik
Honored Contributor

Re: C code and stdin

Hey Marc,

for i in $(cat fileA)
do
unix_cmd $i
done

or you could take a look at the unix command xargs.

hope it helps!
jaton
ranganath ramachandra
Esteemed Contributor

Re: C code and stdin

you would want to do an execvp(3) call for executing the command. i guess stdin/stdout/stderr are inherited by the exec'd process from the parent. so you need something like this:
---
#include
main (int argc, char *argv[])
{
/* your customizations here */
execvp(argv[1], &(argv[1]));
}
---
this seems to work :
----
[ranga@adishesha c]$ cc ./exc.c -o exc
[ranga@adishesha c]$ ls | grep out
a.out
[ranga@adishesha c]$ ls | ./exc grep out
a.out
[ranga@adishesha c]$ echo $?
0
[ranga@adishesha c]$ ls | ./exc grep ranga
[ranga@adishesha c]$ echo $?
1
[ranga@adishesha c]$ ./exc ls ranga 2>error
[ranga@adishesha c]$ cat error
ls: ranga: No such file or directory
----
 
--
ranga
hp-ux 11i v3[i work for hpe]

Accept or Kudo