LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2005, 12:54 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
reading from stdout


I want to be able to read the output from a command invoked from with in a C program. I'm not very well versed on this topic so any help would be appreciated.
 
Old 02-23-2005, 01:24 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Check out the popen() function.
 
Old 02-24-2005, 08:49 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Here's a popen() example I made a while ago.
It slow-scrolls man pages (or info pages if man page not found) by executing tha "man" command with popen(), reading it's output (to stdout) line-by-line in a delayed loop.
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>


#define MAXLINELEN 100
#define MAXCOMMAND 100
#define DISCARDSTDERR " 2>/dev/null"


static char *progname;


int slowdisplay(char *docprog, char *docname)
{
    char line[MAXLINELEN + 1];
    char command[MAXCOMMAND + 1];
    FILE *pipe;

    /*
     * Construct a command line, (e.g. "man ls 2>/dev/null") with safety
     * checks for string length 
     */
    strncpy(command, docprog, MAXCOMMAND);
    command[MAXCOMMAND] = '\0';
    if (strlen(command) + strlen(docname) + strlen(DISCARDSTDERR) >
        MAXCOMMAND) {
        fprintf(stderr, "Error: Command would be too long (max = %d).\n",
                MAXCOMMAND);
        return 0;
    }
    strcat(command, " ");
    strcat(command, docname);
    strcat(command, DISCARDSTDERR);        /* we do not want to see the
                                            * output to stderr */

    /*
     * Execute the command, setting up a pipe for getting its output. 
     */
    pipe = popen(command, "r");
    if ((pipe = popen(command, "r")) == NULL) {
        perror(progname);
        return 0;
    }

    /*
     * Read the doc from the pipe line-by-line, with a short 
     * delay between de lines for demonstration.  
     */
    while (fgets(line, MAXLINELEN + 1, pipe) != NULL) {
        printf(line);
        usleep(100000);                    /* Delay-per-line = 0.1 sec ( =
                                            * 100 ms = 100000 us ) */
    }
    if (pclose(pipe) == -1)
        return 0;
    return 1;
}


int main(int argc, char *argv[])
{
    if ((progname = strrchr(argv[0], '/')) == NULL)
        progname = argv[0];
    else
        progname++;
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <name of man or info page>\n", progname);
        return 1;
    }

    /*
     * First try man page 
     */
    if (slowdisplay("man", argv[1])) {
        return 0;                          /* If successfull, end program */
    }

    /*
     * else try info page 
     */
    if (!slowdisplay("info", argv[1])) {
        fprintf(stderr,
                "Document not found in neither man of info system.\n");
        return 1;                          /* Not succesfull, return exit
                                            * code 1 to indicate failure */
    }
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is stdout stefaandk Linux - Newbie 1 09-06-2005 08:20 PM
stdout to file Machiaveli Programming 8 09-04-2005 09:53 AM
Reading Stdout into a Flatfile omarbohsali Programming 2 01-16-2005 08:24 AM
stdout with tk? sk8guitar Programming 1 07-30-2003 03:48 PM
#@!?$# stdout problem!! beedyrazzle Linux - Newbie 3 01-21-2003 09:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration