473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.

Sep 6 '06 #1
5 21014

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.

Sep 6 '06 #2
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*******@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.
Sep 6 '06 #3
ws*******@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.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.

--
Keith Thompson (The_Other_Keith) ks***@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.
Sep 6 '06 #4
"K-mart Cashier" <cd*****@gmail.comwrites:
ws*******@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_Keith) ks***@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.
Sep 6 '06 #5
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.

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.
Sep 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mikeSpindler | last post by:
I've seen it addressed in searching, but not specifically what is recommended to get around it. On Linux where I did my development all was fine. But then going to Solaris for the target I found...
7
by: Noozer | last post by:
Is it possible to redirect a user to another web page using ASP code located in page body? I have some ASP code that runs after generating some output to the browser. It checks for a condition...
2
by: Ivan Lam | last post by:
Hi all, Thanks for reading my post!!! I am facing a problem that I cannot redirect StandartOutput and StandardInput at the same time without closing the executive. Actually, I have a...
0
by: Alison | last post by:
Hi, all, I started an application by using process and wanted to redirect the output to a textbox in VB .Net. I used newprocess.standoutput.readline since I wanted the display to be timely . The...
6
by: cmk128 | last post by:
Hi here is my c file, compile in gcc 3.X in linux: #include <stdio.h> int main() { printf("Hello\n"); if (fork() == 0) printf("world! \n"); }
1
by: nettleby | last post by:
The Microsoft site has confused me on this issue: http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/iisbook/c06_redirection.mspx?mfr=true Says that: "ASP automatically discards...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
6
by: JohnF | last post by:
I'd like to capture the stdout output from system("command") in a buffer. Although system("command >tmpnam") and then open,read,remove tmpnam works, it's a bit more messy than I'd like. Is there...
2
by: thanhnh | last post by:
Hi. I have to write a simple Shell with History feature. First, I get a string from command line. The command line is split into tokens. And then, I call fork ( ) to create a new process. The child...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.