473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Catching a process' own stdout

Hi All,

I have been crunching my brains on this one all day. I use this library that
creates output I want, but it can only write it to a file or the stdout. The
problem is that I don't want to use the filesystem (permissions, latency
through NFS, etc, etc), but a memory buffer. I've been looking for a way to
redirect stdout to a buffer that I manage myself. Obviously I considered
setbuf (from stdio.h), but then I'ld still have to redirect output to
/dev/null on posix systems and to something I'm not too sure about on
non-posix systems.

I'ld really prefer an ANSI C solution to this problem. Basically what I want
is the following:

int main(void)
{
char buf[BUFFER_SIZE];
FILE *stdout_bkp, *stdout_rep;

some_function_to_backup_a_stream(stdout, stdout_bkp);
stdout_rep = some_function_to_create_a_FILEPTR_to_a_memptr(&buf );
some_function_to_redirect_a_stream(stdout_rep, stdout);

my_library_function();

some_function_to_redirect_a_stream(stdout_bkp, stdout)
fclose(stdout_rep);

my_function_to_do_something_with_the_output(&buf);

return 0;
}

I tried stuff like (open_memstream is part of the GNU C lib, which I'ld
rather not use, if ANSI has alternatives):

int main(void)
{
char *buf;
size_t size;
int stdout_bkp, stdout_rep = open_memstream(&buf, &size);

stdout_bkp = dup(1);
dup2(fileno(stdout_rep), 1);

printf("This should end up in the buffer\n");
libfunc();
fprintf(stdout_rep, "Just to make sure that SOMETHING ends up in the
buffer");
fclose(stdout_rep);

fprintf(stderr, "Result in buffer:\n%s\n", buf);
return 0;
}

This results in the output:
Result in buffer:
Just to make sure that SOMETHING ends up in the buffer


Does anyone have any clue as to what I want and wether it is possible?

Regards,
Philip
Nov 14 '05 #1
5 2448
Philip Hölzenspies wrote:
Hi All,

I have been crunching my brains on this one all day. I use this librarythat
creates output I want, but it can only write it to a file or the stdout. The
problem is that I don't want to use the filesystem (permissions, latency
through NFS, etc, etc), but a memory buffer. I've been looking for a way to
redirect stdout to a buffer that I manage myself. Obviously I considered
setbuf (from stdio.h), but then I'ld still have to redirect output to
/dev/null on posix systems and to something I'm not too sure about on
non-posix systems.

I'ld really prefer an ANSI C solution to this problem. [...]


There isn't one. See Questions 19.30 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

By the way, setbuf() and setvbuf() do not do what you
seem to think they do. What goes into the buffer you
provide, if anything at all does, is not specified. You
have no way of knowing which parts of the buffer contain
what, or whether the contents might vanish while you're
attempting to use them. Don't Do That.

--
Er*********@sun.com

Nov 14 '05 #2
Thanks for your warning on setbuf, I had indeed misread the docs on that.
However, I had already seen 19.30 of the FAQ, but figured it to be
irrelevant, since I don't mean to fork a new process. The point is that I
want to reroute stdout to a buffer before I hand it over to a function I
don't have any control over.

Essentially, the dup-method should work, but doesn't and I simply don't get
why not.

Still open for suggestions.

Philip
Nov 14 '05 #3
Philip Hölzenspies wrote:
Thanks for your warning on setbuf, I had indeed misread the docs on that.
However, I had already seen 19.30 of the FAQ, but figured it to be
irrelevant, since I don't mean to fork a new process.
Sorry; my mistake. I read too hastily. The answer
remains the same, though: there's no Standard-provided
way to do what you're attempting.
The point is that I
want to reroute stdout to a buffer before I hand it over to a function I
don't have any control over.

Essentially, the dup-method should work, but doesn't and I simply don'tget
why not.


Well, you *did* say you were looking for "an ANSI C
solution." Since Standard C doesn't have open_memstream()
or dup() or dup2() or fileno() -- Standard C doesn't even
have any concept of "file descriptor" in the POSIX sense --
I think you're out of luck. You're going to need platform-
specific facilities to fill your need.

--
Er*********@sun.com

Nov 14 '05 #4
Eric Sosman <er*********@sun.com> writes:
[...]
I'ld really prefer an ANSI C solution to this problem. [...]


There isn't one. See Questions 19.30 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html


Actually, 19.30 doesn't address the OP's question. It's about
invoking another program and capturing its output; the OP wants to
capture the output of the current program.

You're right that there's no portable way to do this without using an
external file. You might be able to do something with tmpnam() and
freopen() (I'm too lazy to work out the details). <OT>On Unix-like
systems, tmpnam() is likely to give you the name of a file on /tmp,
which is unlikely to be NFS-mounted.</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
In article <news:ck**********@netlx020.civ.utwente.nl>
Philip Hölzenspies <p.***************@student.utwente.nl> wrote:
... The point is that I want to reroute stdout to a buffer before
I hand it over to a function I don't have any control over.
Unfortunately, this cannot be done in Standard C. Even the
GNU extension you are using is insufficient, because (begin
off-topicness...):
Essentially, the dup-method should work, but doesn't and I simply don't get
why not.


The "FILE *" entity you obtain when you open a "memory stream"
has no underlying file descriptor associated with it. The same
is true for the "function oriented stdio" I put into 4.xBSD.
Since there *is* no file descriptor, no amount of dup()ing will
make the file descriptor become the right one. (End off-topic-ness.)

What you need is to modify the apparently-unmodifiable function
so that instead of writing to stdout, it writes to a user-specified
"FILE *" value. While Standard C will still be unable to solve
the problem, the GNU extension will then do the trick.

One can lump this under the general claim that "global variables
are bad": stdout is in effect a global variable, and the library-writer
used it.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tung Wai Yip | last post by:
I've build the following utility. It works so far but I want to make sure I'm doing the right thing. I am running Python 2.3 on windows 2000. def execute(cmd): """ execute cmd in sub-process,...
1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
9
by: Bryan Olson | last post by:
Here's a module to show stderr output from console-less Python apps, and stay out of the way otherwise. I plan to make a ASPN recipe of it, but I thought I'd run it by this group first. To use...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a C# application in which I start another process which produces output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++ compiler itself! I would like to...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
3
by: Thomas Jansson | last post by:
Dear all I have tkinkter based frontend to a Fortran based program. I use subprocess to launch the fortran program as a child process and I wish to see the output of the fortran program as it is...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
2
by: newlasdjfk | last post by:
I have read tons of posts but still can't seem to figure it out. I want to subprocess.Popen() rsync.exe in windows, and print the stdout in python. This works... import subprocess, time, os,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.