Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #164215
| From | John Forkosh <forkosh@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: pipe a buffer to a program's stdin using popen |
| Date | 2022-01-02 09:12 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <sqrq9o$lcv$1@reader1.panix.com> (permalink) |
| References | <sqp79k$po6$1@reader1.panix.com> <20220101101839.914@kylheku.com> <sqqfdr$2jpnj$1@news.xmission.com> |
Kenny McCormack <gazelle@shell.xmission.com> wrote:
> Kaz Kylheku <480-992-1380@kylheku.com> wrote:
>> John Forkosh <forkosh@panix.com> wrote:
>>> Suppose I have a program that, in part, runs another program
>>> using FILE *outptr=popen("some command string","r") and then
>>> fread()'s its stdout from outptr. Now, if that other program
>>> is otherpgm which reads input from its stdin, and I also want
>>> that input to come from a file inputfile, then I could easily
>>> just write FILE *outptr=popen("cat inputfile|otherpgm","r");
>>
>>That would be a useless use of cat; it could be done with:
>>
>> FILE *outptr=popen("otherpgm < inputfile","r");
>
> Yeah, I noticed that, too. I think that, in this day and age, we can
> probably ignore it - assume it was there only for pedagogic purposes,
> and that OP actually knows better.
Yup (maybe "illustrative" rather than "pedagogic").
> That said, two observations on the original question:
>
> 1) This is, of course, completely OT in clc. People who live their
> lives around keeping clc pure and untainted by OT posts will no doubt
> start jumping up and down about this any post now...
Sorry about that, though I think maybe in stackexchange there'd possibly
be two simultaneously relevant tags: Unix C. I'm obviously interested
in implementing a solution, in C, whereby the C-relevance.
> 2) There are at least a half-dozen possible solutions to OP's problem
> that spring to mind almost immediately. Which one is best will depend
> on two things:
> a) How deep OP's problem actually is. I.e., how much rigor is needed
> in a solution - i.e., which solution is going to be "good enough".
If it works, it's good enough.
> b) OP's skill level - and how much grit-and-grime he is willing to put
> up with.
If it works, I'm probably willing to put up with it.
> That all said, one solution that comes to mind is to use bash's <<<
> operator, like this:
>
> sprintf(buffer,"otherpgm <<< '%s'",myinputstring);
> FILE *outptr=popen(buffer,"r");
Problem is that myinputstring (which is what I called
unsigned char buffer[9999] in original question) can be
many megabytes long, containing lots of hex chars (including \000).
If it were short and ascii, I might have illustratively
alternatively written
sprintf(buffer,"echo \"%s\" | otherpgm",myinputstring);
along the same lines as our "cat" discussion above.
> Now, the problem with this is that, on most systems,
> things run with popen() don't get bash, even if bash
> is the standard and most commonly used shell on the system.
> Sometimes, they get bash, but invoke it in a way that
> disables most of the bash goodies (e.g., <<<).
>
> Fixing this is left as an exercise for the reader...
> (It *can* be done, but it gets messy)
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-01 09:35 +0000
Re: pipe a buffer to a program's stdin using popen Mark Bluemel <mark.bluemel@gmail.com> - 2022-01-01 03:22 -0800
Re: pipe a buffer to a program's stdin using popen Meredith Montgomery <mmontgomery@levado.to> - 2022-01-01 11:21 -0300
Re: pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-02 09:34 +0000
Re: pipe a buffer to a program's stdin using popen Kaz Kylheku <480-992-1380@kylheku.com> - 2022-01-01 19:51 +0000
Re: pipe a buffer to a program's stdin using popen gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-01 21:00 +0000
Re: pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-02 09:12 +0000
Re: pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-02 09:30 +0000
Re: pipe a buffer to a program's stdin using popen Kaz Kylheku <480-992-1380@kylheku.com> - 2022-01-03 00:03 +0000
Re: pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-03 08:07 +0000
Re: pipe a buffer to a program's stdin using popen Manfred <noname@add.invalid> - 2022-01-03 22:54 +0100
Re: pipe a buffer to a program's stdin using popen Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-01 15:00 -0800
Re: pipe a buffer to a program's stdin using popen gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-02 00:01 +0000
Re: pipe a buffer to a program's stdin using popen Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-01-02 12:54 +0000
Re: pipe a buffer to a program's stdin using popen gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-02 14:54 +0000
Re: pipe a buffer to a program's stdin using popen Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-01-02 15:37 +0000
Re: pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-02 18:00 +0000
Re: pipe a buffer to a program's stdin using popen gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-02 18:03 +0000
Re: pipe a buffer to a program's stdin using popen John Forkosh <forkosh@panix.com> - 2022-01-02 17:51 +0000
csiph-web