slackware linux programming question - Slackware

This is a discussion on slackware linux programming question - Slackware ; Does anyone know how to capture the normal terminal output of a program called from a 'C' program system() call? I want to get the output of pstree from within my program. example: returnval = system("pstree"); How can I capture ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: slackware linux programming question

  1. slackware linux programming question

    Does anyone know how to capture the normal terminal output of a program
    called from a 'C' program system() call?

    I want to get the output of pstree from within my program.

    example: returnval = system("pstree");

    How can I capture the output of pstree that would normally show up in
    the terminal window?

    Thanks

  2. Re: slackware linux programming question

    George trolled:

    > Does anyone know how to capture the normal terminal output of a program
    > called from a 'C' program system() call?


    > I want to get the output of pstree from within my program.


    > example: returnval = system("pstree");


    > How can I capture the output of pstree that would normally show up in
    > the terminal window?


    Try returnval = system("pstree > psoutput");

    The tree should be captured to the file psoutput.

    See how that works.

    cordially, as always,

    rm

  3. Re: slackware linux programming question

    On Wed, 04 Jul 2007 03:24:20 +0000, George wrote:

    > Does anyone know how to capture the normal terminal output of a program
    > called from a 'C' program system() call?
    >
    > I want to get the output of pstree from within my program.
    >
    > example: returnval = system("pstree");
    >
    > How can I capture the output of pstree that would normally show up in
    > the terminal window?


    What does any of that have to do with "Slackware Linux"? Just because you
    put it in the subject line doesn't make it Slackware-related, dimwit.

    Try asking in one of the programming newsgroups.

    Duh.



    --
    "Ubuntu" -- an African word, meaning "Slackware is too hard for me".


  4. Re: slackware linux programming question

    George wrote:

    > example: returnval = system("pstree");
    >
    > How can I capture the output of pstree that would normally show up in
    > the terminal window?


    handle = popen("pstree", "r");
    if( handle )
    {
    while( fgets(buf, 1024, handle) )
    {
    /* do something with buf */
    }
    pclose(handle);
    }

    regards Henrik
    --
    The address in the header is only to prevent spam. My real address is:
    hc1(at)poolhem.se Examples of addresses which go to spammers:
    root@localhost postmaster@localhost


  5. Re: slackware linux programming question

    Henrik Carlqvist wrote:
    > George wrote:
    >
    >> example: returnval = system("pstree");
    >>
    >> How can I capture the output of pstree that would normally show up in
    >> the terminal window?

    >
    > handle = popen("pstree", "r");
    > if( handle )
    > {
    > while( fgets(buf, 1024, handle) )
    > {
    > /* do something with buf */
    > }
    > pclose(handle);
    > }
    >
    > regards Henrik


    Thanks, this example worked great. It's awsome to be able to place
    command output on a web page.

    Thanks!!!

  6. Re: slackware linux programming question

    George wrote:
    > Thanks, this example worked great. It's awsome to be able to place
    > command output on a web page.


    I usually use php for that. The php code looks very much like the C code
    I gave as an example, the main difference is that all variable names start
    with $ in php.

    regards Henrik
    --
    The address in the header is only to prevent spam. My real address is:
    hc1(at)poolhem.se Examples of addresses which go to spammers:
    root@localhost postmaster@localhost


  7. Re: slackware linux programming question

    George trolled:
    >Henrik Carlqvist wrote:
    >> George wrote:


    >>> example: returnval = system("pstree");


    >>> How can I capture the output of pstree that would normally show up in
    >>> the terminal window?


    >> handle = popen("pstree", "r");
    >> if( handle )
    >> {
    >> while( fgets(buf, 1024, handle) )
    >> {
    >> /* do something with buf */
    >> }
    >> pclose(handle);
    >> }
    >>
    >> regards Henrik


    >Thanks, this example worked great. It's awsome to be able to place
    >command output on a web page.


    >Thanks!!!


    And is there some reason that you didn't thank us for our answer?

    You only got two answers, and although you preferred the other
    answer to ours, ours worked equally as well. In fact, by writing
    the output to a file first, and retrieving it with getc or fgetc,
    you avoid the problem of a buffer size limitation.

    In fact, you might want to change the fgets in Henrik's answer to an
    fgetc and then you won't have to worry about buffer size. It will
    be a bit slower (not much) but since it is for a web page, that
    doesn't seem to be important.

    In any case, that still does not absolve you of the responsibility
    of thanking all the posters that tried to help you out.

    Or don't you think so?

    cordially, as always,

    rm
    --
    .... the only things that separate it (slackware) from others are the
    things it lacks... not has. -- ANC

+ Reply to Thread