1

I have count = read(pipe, buffer, buffsize); and am trying to run what is received (buffer) through another executable to have a differing process done on it.

printf("%s", buffer); prints it out correctly, but running it through execl("/path", "/path", buffer, NULL); or a number of other ways I've tried doesn't seem to run the executable. path is a compiled executable.

The executable does run properly if I use execv("./path", STDIN_FILENO);, but that isn't being taken from the pipe. path is expecting a the string as standard input.

The situation of the program is that I'm typing in input on one program using a while loop and read(), using a pipe to send that text to the program that is running execl (nothing else needs to be done in this program), that is then trying to call the executable with the string as an stdin. Only the intended input is coming in through the pipe, in chunks when the user presses enter.

An example of a string coming through the pipe is this is an example. The executable needs to have this inputted as standard input.

How can I get the string to be used as standard input for /path executable correctly?

6
  • There are several different problems tangled up in here; we need more information. Does the second executable expect its input on its stdin, or as a command line argument? Does it not run at all, or does it just not process the input as expected? Are you doing execl in a child process or the original process? Is there anything else coming in on that pipe, or is it entirely the intended input for the second executable? Does the program that's doing all this have other stuff that it needs to do? Commented Oct 8, 2014 at 21:28
  • @Zack Thanks, I wasn't sure what all I needed to include. I added some more information Commented Oct 8, 2014 at 21:42
  • When the string coming through the pipe is "this is an example", what is the value of count? Commented Oct 8, 2014 at 22:01
  • @chux The value is 1 Commented Oct 8, 2014 at 22:09
  • run the two programs like this: pgm1 | pgm2 Then have pgm1 read from stdin and output to stdout. Have pgm2 read from stdin and output to stdout. Then the connection is automatic and all you have to do is type the input, after having started the two programs with the command line, as indicated above. Commented Oct 10, 2014 at 5:47

1 Answer 1

2

It sounds like popen() is what you're looking for, to open a pipe to your desired executable so you can either pass stuff to its standard input, or read stuff from its standard output.

For instance:

#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    FILE * p = popen("/bin/cat", "w");
    if ( !p ) {
        perror("error opening pipe");
        return EXIT_FAILURE;
    }

    fputs("Echo me via /bin/cat\n", p);

    if ( pclose(p) == -1 ) {
        perror("error closing pipe");
        return EXIT_FAILURE;
    }

    return 0;
}

which outputs:

paul@thoth:~/src/sandbox$ ./testpopen
Echo me via /bin/cat
paul@thoth:~/src/sandbox$ 
Sign up to request clarification or add additional context in comments.

8 Comments

I'm not sure if I didn't make the problem clear or if I'm misunderstanding your answer. The issue is not with transferring text from the input program to the second program via a pipe, it's with transferring the text from the second program to the executable
@ZachSaucier: And you can use popen() in the second program to run the executable and pass the text to its standard input.
I see. Wouldn't the executable have to be running then? Also I'd prefer not to have to change the executable's code as it was given to us (school project)
@ZachSaucier: No, it wouldn't have to be running, popen() would execute it. popen() essentially opens up a shell, runs your executable, and then passes everything you write to the file pointer it returns to that executable's standard input (or reads from its standard output, if you call it with a "r" mode). When you call pclose(), it'll wait for your executable to terminate, and then return. There's no need to change any code in the executable, as far as it will know it's just getting reading from its standard input as it always would.
execv() expects an array of strings representing command line arguments as its second argument, passing it a small integer instead would be a catastrophic error if it tried to read from that array, and it certainly wouldn't cause anything to happen with its standard input.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.