A question about pipes

This is a discussion on A question about pipes within the Linux Programming forums, part of the Platform Specific Boards category; The following code is taken from page 500 - 501 in the book "Advanced Programming in the Unix Environment" by ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    A question about pipes

    The following code is taken from page 500 - 501 in the book "Advanced
    Programming in the Unix Environment" by Stevens and Rago.

    Code:
    #include "apue.h"
    #include <sys/wait.h>
    
    #define DEF_PAGER   "/bin/more"     /* default pager program */
    
    int
    main(int argc, char *argv[])
    {
        int    n;
        int    fd[2];
        pid_t  pid;
        char   *pager, *argv0;
        char   line[MAXLINE];
        FILE   *fp;
    
        if (argc != 2)
            err_quit("usage: a.out <pathname>");
    
        if ((fp = fopen(argv[1], "r")) == NULL)
            err_sys("can't open %s", argv[1]);
        if (pipe(fd) < 0)
            err_sys("pipe error");
    
        if ((pid = fork()) < 0) {
            err_sys("fork error");
        } else if (pid > 0) {                              /* parent */
            close(fd[0]);       /* close read end */
    
            /* parent copies argv[1] to pipe */
            while (fgets(line, MAXLINE, fp) != NULL) {
                n = strlen(line);
                if (write(fd[1], line, n) != n)
                    err_sys("write error to pipe");
            }
            if (ferror(fp))
                err_sys("fgets error");
    
            close(fd[1]);   /* close write end of pipe for reader */
    
            if (waitpid(pid, NULL, 0) < 0)
                err_sys("waitpid error");
            exit(0);
        } else {                                        /* child */
            close(fd[1]);   /* close write end */
            if (fd[0] != STDIN_FILENO) {
                if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
                    err_sys("dup2 error to stdin");
                close(fd[0]);   /* don't need this after dup2 */
            }
    
            /* get arguments for execl() */
            if ((pager = getenv("PAGER")) == NULL)
                pager = DEF_PAGER;
            if ((argv0 = strrchr(pager, '/')) != NULL)
                argv0++;        /* step past rightmost slash */
            else
                argv0 = pager;  /* no slash in pager */
    
            if (execl(pager, argv0, (char *)0) < 0)
                err_sys("execl error for %s", pager);
        }
        exit(0);
    
    }
    Couldn't a process call this more than once like with popen()?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'd need to do something pretty similar using fork and such to use popen() - I haven't thought MUCH about it, but my immediate reaction is that you'd get pretty much the same code, except you'd use popen() instead of pipe().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes sometimes fail
    By EVOEx in forum Linux Programming
    Replies: 2
    Last Post: 05-02-2009, 01:47 PM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Services and Pipes
    By nickname_changed in forum Windows Programming
    Replies: 0
    Last Post: 07-16-2003, 06:46 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21