[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ale] C question



set up a pipe, fork(), dup2(pipe_output, STDIN); exec(), (from parent
process) push "stuff" into pipe should work for that.

Something like:
(Please add error checking...)

pid_t child;
int pipefd[2];
char stuff[] = "stuff";

pipe(pipefd);
if((child = fork()) == 0) {
  // Child
  dup2(pipefd[0], STDIN_FILENO);
  execl("/bin/foo", "/bin/foo", "--some-arg");
}
write(pipefd[1], stuff, strlen(stuff));
waitpid(child, ...);

(Haven't tested it, but this should work...)




On Sun, Apr 7, 2013 at 4:03 PM, Geoffrey Myers
<lists at serioustechnology.com>wrote:

> Yeah, I've got the standard parent writes to the child process down.
>  That's not the issue.  I've got to write to one process via a pipe.  That
> process is not a process I'm writing, but an existing program.  Again,
> check the example:
>
> system("echo stuff | somebinary arg1 arg2");
>
> The part I'm wrestling with is the:
>
> echo stuff | somebinary
>
>
>
> On Apr 5, 2013, at 7:56 PM, Ed Cashin wrote:
>
> I was going to write this example before realizing it is probably online,
> and I found it here:
>
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html
>
> ... in the docs for standard pipe(2) behavior.  It looks like the stuff
> after the ellipsis is in main or some other function whose beginning and
> end aren't shown.
>
> #include <stdlib.h>
> #include <unistd.h>
> ...
>
> int fildes[2];
> const int BSIZE = 100;
> char buf[BSIZE];
> ssize_t nbytes;
> int status;
>
> status = pipe(fildes);
> if (status == -1 ) {
>     /* an error occurred */
>     ...
> }
>
> switch (fork()) {
> case -1: /* Handle error */
>     break;
>
> case 0:  /* Child - reads from pipe */
>     close(fildes[1]);                       /* Write end is unused */
>     nbytes = read(fildes[0], buf, BSIZE);   /* Get data from pipe */
>     /* At this point, a further read would see end of file ... */
>     close(fildes[0]);                       /* Finished with pipe */
>     exit(EXIT_SUCCESS);
>
> default:  /* Parent - writes to pipe */
>     close(fildes[0]);                       /* Read end is unused */
>     write(fildes[1], "Hello world\n", 12);  /* Write data on pipe */
>     close(fildes[1]);                       /* Child will see EOF */
>     exit(EXIT_SUCCESS);
> }
>
>
>
>
> On Fri, Apr 5, 2013 at 3:33 PM, Geoffrey Myers <
> lists at serioustechnology.com> wrote:
>
>> So, shaking some cobwebs loose here. How do I replace the following:
>>
>> system("echo stuff | somebinary arg1 arg2");
>>
>> With a fork/exec ??
>>
>> (Digging out my old C books....)
>>
>> --
>> From my iPhone
>> Geoffrey Myers
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://mail.ale.org/mailman/listinfo/ale
>> See JOBS, ANNOUNCE and SCHOOLS lists at
>> http://mail.ale.org/mailman/listinfo
>>
>
>
>
> --
>   Ed Cashin <ecashin at noserose.net>
>   http://noserose.net/e/
>   http://www.coraid.com/
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
>
> --
> Until later, Geof
>
>
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
>


-- 
David Tomaschik
OpenPGP: 0x5DEA789B
http://systemoverlord.com
david at systemoverlord.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20130407/cb39ff50/attachment.html>