Unix Programming - printing and opening a pipe

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2004 > printing and opening a pipe





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author printing and opening a pipe
Billy N. Patton

2004-10-18, 8:48 pm

I'm calling a library function that prsnts something to stdout.

Long story but I can't change this function for now, too much ripple.

I need to
1. open a pipe to capture stdout.
2. save to string.
3. return string
4. close pipe.


Ony thing I have is:

bool ExCommand(string& cmd,vector<string>& ret)
{
string s;
static char buf[BUFSZ];
FILE *ptr = NULL;

if (cmd.empty()) return false;

if ((ptr = popen(cmd.c_str(), "r")) NE NULL)
{
while (fgets(buf, BUFSZ, ptr) NE NULL)
{
buf[strlen(buf)-1] = '\0';
s = buf;
ret.push_back(s);
}
pclose(ptr);
}
else
{
return false;
}
return true;
}


But this executes an extern shell command. I'm calling a function
inside a library.



--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-patton@ti.com
Lew Pitcher

2004-10-18, 8:48 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Billy N. Patton wrote:
> I'm calling a library function that prsnts something to stdout.
>
> Long story but I can't change this function for now, too much ripple.
>
> I need to
> 1. open a pipe to capture stdout.
> 2. save to string.
> 3. return string
> 4. close pipe.

[snip]

That seems a bit difficult to do with pipes.

Have you considered an alternative, like using freopen(3) to capture stdout to a
file, then reading the file?


- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBdAkIagVFX4UWr64RAvW5AKCSEnhAc7ed
47uWucul8RhteB+nlwCfVsTK
fNJVXzwNbB/OD8p5M8vL3S4=
=54+A
-----END PGP SIGNATURE-----
Billy N. Patton

2004-10-18, 8:48 pm

Lew Pitcher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Billy N. Patton wrote:
>
>
> [snip]
>
> That seems a bit difficult to do with pipes.
>
> Have you considered an alternative, like using freopen(3) to capture stdout to a
> file, then reading the file?


Yes but don't want to do that much IO.
Besides I may have 100's running in batch around the network.
Bookkeeping could get nasty for unique file names.

>
>
> - --
> Lew Pitcher
> IT Consultant, Enterprise Data Systems,
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed are my own, not my employers')
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
> iD8DBQFBdAkIagVFX4UWr64RAvW5AKCSEnhAc7ed
47uWucul8RhteB+nlwCfVsTK
> fNJVXzwNbB/OD8p5M8vL3S4=
> =54+A
> -----END PGP SIGNATURE-----



--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-patton@ti.com
Barry Margolin

2004-10-18, 8:48 pm

In article <cl101j$h4g$1@home.itg.ti.com>,
"Billy N. Patton" <b-patton@ti.com> wrote:

> I'm calling a library function that prsnts something to stdout.
>
> Long story but I can't change this function for now, too much ripple.
>
> I need to
> 1. open a pipe to capture stdout.
> 2. save to string.
> 3. return string
> 4. close pipe.

....

> But this executes an extern shell command. I'm calling a function
> inside a library.


What you could do is something like the following pseudocode:

dup(saved_stdout, STDOUT_FILENO);
pipe(pipefd);
dup2(STDOUT_FILENO, pipefd[1]);
close(pipefd[1]);
create a thread that reads from pipefd[0] and accumulates it into a
string
call the function
dup2(STDOUT_FILENO, saved_stdout);
The thread should now exit when it reads EOF from the pipe
close(pipefd[0])
Process the string

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com