Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:55:34+00:00 2026-06-06T03:55:34+00:00

I read an exec’d program’s stdout using a pipe: int pipes[2]; pipe(pipes); if (fork()

  • 0

I read an exec’d program’s stdout using a pipe:

int pipes[2];
pipe(pipes);
if (fork() == 0) {
    dup2(pipes[1], 1);
    close(pipes[1]);
    execlp("some_prog", "");
} else {
    char* buf = auto_read(pipes[0]);
}

To read from stdout, I have a function auto_read which automatically allocates more memory as needed.

char* auto_read(int fp) {
    int bytes = 1000;
    char* buf = (char*)malloc(bytes+1);
    int bytes_read = read(fp, buf, bytes);
    int total_reads = 1;
    while (bytes_read != 0) {
        realloc(buf, total_reads * bytes + 1);
        bytes_read = read(fp, buf + total_reads * bytes, bytes);
        total_reads++;
    }
    buf[(total_reads - 1) * bytes + bytes_read] = 0;
    return buf;
}

The reason I do it this way is I don’t know how much text the program is going to spew out ahead of time, and I don’t want to create an overly large buffer and be a memory hog. I’m wondering if there is:

  1. A cleaner way to write this.
  2. A more memory or speed-efficient way of doing this.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:55:36+00:00Added an answer on June 6, 2026 at 3:55 am

    Use popen if you only need to read from a process and are on a *NIX platform:

    FILE *programStdout = popen("command", "r");
    
    // read from programStdout (fread(), fgets(), etc.)
    char buffer[1024];
    
    while (fgets(buffer, 1024, programStdout))
    {
        puts(buffer);
    }
    

    EDIT: You asked for a way to map a programs output to a file, so here you go:

    #import <stdio.h>
    #import <unistd.h>
    #import <sys/mman.h>
    
    void *dataWithContentsOfMappedProgram(const char *command,  size_t *len)
    {
        // read the data
        char template[] = "/tmp/tmpfile_XXXXXX";
        int fd = mkstemp(template);
    
        FILE *output = fdopen(fd, "w+");
        FILE *input = popen(command, "r");
    
    #define BUF_SIZ 1024
        char buffer[BUF_SIZ];
        size_t readSize = 0;
        while ((readSize = fread(buffer, 1, BUF_SIZ, input)))
        {
            fwrite(buffer, 1, readSize, output);
        }
        fclose(input);
    
        input = NULL;
    #undef BUF_SIZ
    
        // now we map the file
        long fileLength = ftell(output);
        fseek(output, 0, SEEK_SET);
    
        void *data = mmap(NULL, fileLength, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fd, 0);
    
        close(fd);
    
        if (data == MAP_FAILED)
            return NULL;
    
        return data;
    }
    
    
    int main()
    {
        size_t fileLen = 0;
        char *mapped = dataWithContentsOfMappedProgram("echo Hello World!", &fileLen);
    
        puts(mapped);
    
        munmap(mapped, fileLen);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying various methods (popen, pipes + fork/exec, ...) to read a child
I read that the executables for the commands issued using exec() calls are supposed
Process p = Runtime.getRuntime().exec(command); is = p.getInputStream(); byte[] userbytes = new byte[1024]; is.read(userbytes); I
A new process is created by fork/exec. Exec sets up the command line arguments
I'm using a PHP script to read an RSS feed in my Flex 4
I have the following statement in a stored procedure: DECLARE @Count INT EXEC @Count
when using RunTime.exec(), one can specify the working directory of the subprocess (very useful,
I have a simple QT program to read an sqlite3 database. Annoyingly, my program
I'm using antiword to read doc files in php: <?php $filename = 'sample.doc'; $content
I am trying to read a weather feed from Yahoo to my site. Using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.