ws*******@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.