Redirect output of execvp to a buffer

W

ws.taylor

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

K-mart Cashier

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.
 
W

ws.taylor

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 said:
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.
 
K

Keith Thompson

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.programmer. 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.
 
K

Keith Thompson

K-mart Cashier said:
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.
 
S

SM Ryan

(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top