2

In a C program (p1), how to launch a dynamically constructed command (and its arguments) that reads its standard input from p1's standard output?

Note that:

  1. A method other than this stdout --> stdin piping is also OK provided it is PORTABLE across Windows and Linux.

  2. I cannot use C++, Java, Perl, Ruby, Python, etc here.

Also, will this have a MinGW dependency for its Windows build?

REOPENED: The question below answers it for Linux, but this question wants a portable method. Execute program from within a C program

Community
  • 1
  • 1
user10955
  • 161
  • 2
  • 8

4 Answers4

4

The Microsoft C runtime calls it _popen instead of popen, but it appears to have the same functionality in Windows (for console applications) and Linux.

bk1e
  • 23,871
  • 6
  • 54
  • 65
2

It's not 100% clear to me what you're trying to achieve exactly to be honest.

But as I understand it, you could take a look at Boost.Process

You can do things like

 bp::child cs = p.start();
 bp::postream& os = cs.get_stdin();

And then use the os as any stream to dump stuff in the standard input of your child process.

Anyway, an awful lot can be achieved with the library w.r.t. pipe redirecting and chaining.

Pieter
  • 17,435
  • 8
  • 50
  • 89
  • Hmm, boost. An 800-pound gorilla that I will need to link and shake hands with. I will still be watching out for any overall lightweight, C responses. Thanks, though. (Note that I'm now dropping the C++ from the condition.) – user10955 Oct 23 '08 at 14:37
1

The linked anser (which you reject) refers to POSIX pipes. POSIX is an extension to C, which adds features missing from standard C. POSIX pipes are a good example of such a feature: they were added to POSIX because standard C does not have that functionality.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

The glib library is written in C and has implementations that are well tested on both Linux and Windows. The manual section on spawning new processes will give you information about how to start a new process and hook into its stdin and stdout handles using the g_spawn_async_with_pipes call.

Ben Combee
  • 16,831
  • 6
  • 41
  • 42