Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created June 22, 2016 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfikes/52332f568be9da2dd72ee9376e5b9f34 to your computer and use it in GitHub Desktop.
Save mfikes/52332f568be9da2dd72ee9376e5b9f34 to your computer and use it in GitHub Desktop.
Read read stderr stdout and write stdin

child.c:

#include "stdio.h"

int main(void) {

   fprintf(stdout, "this is from stdout\n");
   fprintf(stderr, "this is from stderr\n");
}

parent.c:

#include <unistd.h>
#include <stdio.h>

int main()
{
  int outfd[2];
  int infd[2];
  int errfd[2];

  // pipes for parent to write and read
  pipe(outfd);
  pipe(infd);
  pipe(errfd);

  if(!fork()) {
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    close(STDIN_FILENO);
    dup2(outfd[0], STDIN_FILENO);
    dup2(errfd[1], STDERR_FILENO);
    dup2(infd[1], STDOUT_FILENO);
    close(outfd[0]); /* Not required for the child */
    close(outfd[1]);
    close(errfd[0]);
    close(errfd[1]);
    close(infd[0]);
    close(infd[1]);

    char *argv[]={ "child", 0};
    execv(argv[0], argv);
  } else {
    char buffer[1024];
    int count;

    /* close fds not required by parent */
    close(outfd[0]);
    close(errfd[1]);
    close(infd[1]);

    // Write to child’s stdin
    write(outfd[1], "2^32\n", 5);

    // Read from child’s stderr
    count = read(errfd[0], buffer, sizeof(buffer)-1);
    if (count >= 0) {
      buffer[count] = 0;
      printf("%s", buffer);
    } else {
      printf("IO Error\n");
      printf("%d\n", count);
    }

    // Read from child’s stdout
    count = read(infd[0], buffer, sizeof(buffer)-1);
    if (count >= 0) {
      buffer[count] = 0;
      printf("%s", buffer);
    } else {
      printf("IO Error\n");
    }

  }
}

Testing it:

$ clang child.c -o child
$ ./child
this is from stdout
this is from stderr
$ clang parent.c -o parent
$ ./parent
this is from stderr
this is from stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment