Redirect output of execvp to a buffer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ws.taylor@gmail.com

    Redirect output of execvp to a buffer

    I am in a systems programming class, using C on a Solaris 10
    development server. Part of the first programming assignment calls for
    the output of a command executed by a child process with execvp to be
    redirected to a buffer so that it can be sent through a named pipe back
    to the parent process. The only part I'm having problems with is
    capturing execvp's output to a buffer. I can't find anything about this
    from our book, or from searching the web. Would I use dup2 for this? Is
    it even possible to use dup2 to redirect stdout to a buffer? I can
    redirect stdout to the named pipe, so it seems like that's possible.
    Any help would be greatly appreciated.

  • K-mart Cashier

    #2
    Re: Redirect output of execvp to a buffer


    ws.taylor@gmail .com wrote:
    I am in a systems programming class, using C on a Solaris 10
    development server. Part of the first programming assignment calls for
    the output of a command executed by a child process with execvp to be
    redirected to a buffer so that it can be sent through a named pipe back
    to the parent process. The only part I'm having problems with is
    capturing execvp's output to a buffer. I can't find anything about this
    from our book, or from searching the web. Would I use dup2 for this? Is
    it even possible to use dup2 to redirect stdout to a buffer? I can
    redirect stdout to the named pipe, so it seems like that's possible.
    Any help would be greatly appreciated.
    For whatever reasons, this thread reminds me of a sick thought I had
    when stocking the bra section at k-mart. Anyhow, the assistant manager
    was being more of a homo than normal. Then i started to think of other
    homos in the world. I realized my local ISP admin was also a homo. Then
    I started to think how to annoy fatboy at my isp. So I like drew up
    this cheap DOS attack. Anyhow, I like used execl() along with dup2()
    because it looked sexier at the time. Here is part of the code snippet:

    User *
    getuser(char *user)
    {
    User *ulist, *up;
    int fds[2];
    pid_t pid;
    char buf[1024], *p;

    if (pipe(fds) < 0)
    error("pipe failed");
    pid = fork();
    if (pid < 0)
    error("fork failed");
    if (pid == 0) {
    close(fds[0]);
    dup2(fds[1], STDOUT_FILENO);
    if (pipe(fds) < 0)
    error("pipe failed in child");
    pid = fork();
    if (pid < 0)
    error("second fork failed");
    if (pid == 0) { /* This is the grandchild. */
    close(fds[0]);
    dup2(fds[1], STDOUT_FILENO);
    execl("/usr/bin/who", "who", (char *)0);
    _error("execl failed");
    }
    /* Otherwise, this is the child. */
    close(fds[1]);
    dup2(fds[0], STDIN_FILENO);
    snprintf(buf, sizeof(buf), SCRIPT, user);
    execl("/usr/bin/nawk", "nawk", buf, (char *)0);
    _error("execl failed");
    }
    /* This is in the parent. */
    close(fds[1]);
    dup2(fds[0], STDIN_FILENO);
    ulist = NULL;
    while (fgets(buf, sizeof(buf), stdin)) {
    p = strrchr(buf, '\n');
    if (p != NULL)
    *p = '\0';
    p = strchr(buf, ':');
    if (p == NULL || *(p + 1) == '\0') {
    fprintf(stderr, "Bad input record from
    pipe.\n");
    continue;
    }
    *p++ = '\0';
    up = newuser(buf, p);
    up->next = ulist;
    ulist = up;
    }
    if (ferror(stdin))
    error("pipe error");

    return(ulist);
    }

    Okay now I must go, because my time at the family computer is just
    about up. Also, it is almost my bedtime.

    Comment

    • ws.taylor@gmail.com

      #3
      Re: Redirect output of execvp to a buffer

      Wow. That was entertaining.

      Also very helpful. I didn't even think of using fgets. Thanks a bunch.

      Have fun at K-Mart...

      K-mart Cashier wrote:
      ws.taylor@gmail .com wrote:
      I am in a systems programming class, using C on a Solaris 10
      development server. Part of the first programming assignment calls for
      the output of a command executed by a child process with execvp to be
      redirected to a buffer so that it can be sent through a named pipe back
      to the parent process. The only part I'm having problems with is
      capturing execvp's output to a buffer. I can't find anything about this
      from our book, or from searching the web. Would I use dup2 for this? Is
      it even possible to use dup2 to redirect stdout to a buffer? I can
      redirect stdout to the named pipe, so it seems like that's possible.
      Any help would be greatly appreciated.
      >
      For whatever reasons, this thread reminds me of a sick thought I had
      when stocking the bra section at k-mart. Anyhow, the assistant manager
      was being more of a homo than normal. Then i started to think of other
      homos in the world. I realized my local ISP admin was also a homo. Then
      I started to think how to annoy fatboy at my isp. So I like drew up
      this cheap DOS attack. Anyhow, I like used execl() along with dup2()
      because it looked sexier at the time. Here is part of the code snippet:
      >
      User *
      getuser(char *user)
      {
      User *ulist, *up;
      int fds[2];
      pid_t pid;
      char buf[1024], *p;
      >
      if (pipe(fds) < 0)
      error("pipe failed");
      pid = fork();
      if (pid < 0)
      error("fork failed");
      if (pid == 0) {
      close(fds[0]);
      dup2(fds[1], STDOUT_FILENO);
      if (pipe(fds) < 0)
      error("pipe failed in child");
      pid = fork();
      if (pid < 0)
      error("second fork failed");
      if (pid == 0) { /* This is the grandchild. */
      close(fds[0]);
      dup2(fds[1], STDOUT_FILENO);
      execl("/usr/bin/who", "who", (char *)0);
      _error("execl failed");
      }
      /* Otherwise, this is the child. */
      close(fds[1]);
      dup2(fds[0], STDIN_FILENO);
      snprintf(buf, sizeof(buf), SCRIPT, user);
      execl("/usr/bin/nawk", "nawk", buf, (char *)0);
      _error("execl failed");
      }
      /* This is in the parent. */
      close(fds[1]);
      dup2(fds[0], STDIN_FILENO);
      ulist = NULL;
      while (fgets(buf, sizeof(buf), stdin)) {
      p = strrchr(buf, '\n');
      if (p != NULL)
      *p = '\0';
      p = strchr(buf, ':');
      if (p == NULL || *(p + 1) == '\0') {
      fprintf(stderr, "Bad input record from
      pipe.\n");
      continue;
      }
      *p++ = '\0';
      up = newuser(buf, p);
      up->next = ulist;
      ulist = up;
      }
      if (ferror(stdin))
      error("pipe error");
      >
      return(ulist);
      }
      >
      Okay now I must go, because my time at the family computer is just
      about up. Also, it is almost my bedtime.

      Comment

      • Keith Thompson

        #4
        Re: Redirect output of execvp to a buffer

        ws.taylor@gmail .com writes:
        I am in a systems programming class, using C on a Solaris 10
        development server. Part of the first programming assignment calls for
        the output of a command executed by a child process with execvp to be
        redirected to a buffer so that it can be sent through a named pipe back
        to the parent process. The only part I'm having problems with is
        capturing execvp's output to a buffer. I can't find anything about this
        from our book, or from searching the web. Would I use dup2 for this? Is
        it even possible to use dup2 to redirect stdout to a buffer? I can
        redirect stdout to the named pipe, so it seems like that's possible.
        Any help would be greatly appreciated.
        Please limit followups to comp.unix.progr ammer. What the OP is trying
        to do requires features not supported in standard C, which is what we
        discuss in comp.lang.c.

        Followups set.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Keith Thompson

          #5
          Re: Redirect output of execvp to a buffer

          "K-mart Cashier" <cdalten@gmail. comwrites:
          ws.taylor@gmail .com wrote:
          >I am in a systems programming class, using C on a Solaris 10
          >development server.
          [...]
          >
          For whatever reasons, this thread reminds me of a sick thought I had
          when stocking the bra section at k-mart. Anyhow, the assistant manager
          was being more of a homo than normal. Then i started to think of other
          homos in the world. I realized my local ISP admin was also a homo.
          [...]

          Please never post here again. Thank you.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • SM Ryan

            #6
            Re: Redirect output of execvp to a buffer

            ws.taylor@gmail .com wrote:
            # I am in a systems programming class, using C on a Solaris 10
            # development server. Part of the first programming assignment calls for
            # the output of a command executed by a child process with execvp to be
            # redirected to a buffer so that it can be sent through a named pipe back
            # to the parent process. The only part I'm having problems with is
            # capturing execvp's output to a buffer. I can't find anything about this
            # from our book, or from searching the web. Would I use dup2 for this? Is
            # it even possible to use dup2 to redirect stdout to a buffer? I can
            # redirect stdout to the named pipe, so it seems like that's possible.
            # Any help would be greatly appreciated.

            Not sure exactly what you want, however in general to redirect
            stdout to a pipe so you can read it into your parent process

            create pipe
            pid = fork
            if error,
            plugh away
            if pid=0,
            this is the child
            close read end of pipe
            close file descriptor 1
            dup write end of pipe so it becomes fd 1
            close write end of pipe
            (leaving only the write end dupped to 1 open)
            exec the child binary file
            exec failed if this returns
            if pid>0,
            this is the parent
            close the write end of the pipe
            read the read end of the pipe until eof
            wait for the child exit status



            --
            SM Ryan http://www.rawbw.com/~wyrmwif/
            The whole world's against us.

            Comment

            Working...