LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2006, 08:24 PM   #1
Francinoman
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Rep: Reputation: 0
Redirect Output from execvp( ) call


Hi guys,

I am writing a shell in C and it is going well. I am now up to additing redirection into the shell but have run into some dramas.

I am trying to redirect the output from the call execvp()

I have spent quite some time researching this and basically i know I have to open a file, and redirect the stdio using dup2(). Im just not sure which open command to use (open/fopen/others)...

Im guessing that i have to remove the "> file.txt" from the command array for use in execvp()?

if someone could help me out that would be great! Even some psuedo code would be a great help.

Last edited by Francinoman; 10-17-2006 at 08:33 PM.
 
Old 10-17-2006, 08:40 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Look at popen() first, if that isn't what you want then look at one of my older responses for the same thing.

http://www.linuxquestions.org/questi...60#post2456660
 
Old 10-18-2006, 12:12 AM   #3
Francinoman
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Original Poster
Rep: Reputation: 0
hmmm, ok, i had a bit of a look at popen and not too sure if its exactly what i need...This is pretty much what im doing in psuedocode:

1) Prompt for a command
2) Accept a command and parse it into an array or args (first array entry is command, followed by any arguements)
3) If command has ">" send to redirect output function
4) Save file to be written to as a string
5) Remove ">" and "filename" from array of args
6) Open a file for writing
7) Dup2() and redirect the stdio
8) Execute the command and any args using execvp()
9) Close file
10) back to step 1
 
Old 10-18-2006, 08:13 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
If you're doing this as a class exercise to learn about dup2() then look at the unix version of my pipe_open() example from the forkexec.c file.

Otherwise popen() will do all of that for you.
example: popen("echo hello >/tmp/somefile.txt");

Edit, actually if you are not capturing stdout use system() instead.

Last edited by randyding; 10-18-2006 at 08:15 PM.
 
Old 10-21-2006, 08:26 PM   #5
_john_i_
Member
 
Registered: Aug 2003
Location: Austin, TX
Distribution: Linux from Scratch
Posts: 52

Rep: Reputation: 15
I would do something like this:

mystdout=open("filename", O_CREAT | O_WRONLY);
close(1); /* Close original stdout */
dup2(mystdout, 1); /* Move mystdout to FD 1 */
execvp( ..... )

Of course you do no, execvp will never return unless it cannot execute the program, right? The exec family overwrites the current process, moving all of the file descriptors to the new program. If you want to not end the calling program you will need to fork() first.

If all you want to so is a simple execute redirected to a file just do system("program >filename");
 
Old 11-06-2006, 11:47 AM   #6
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
can u help more pls
 
Old 11-06-2006, 12:00 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
studentlb -

In this and your other thread:... even if you get the answer to your homework assignment, it really isn't helping you.

Please have a chat with your instructor, let him know that he's going too fast for you, and see if you can get some help (perhaps tutoring).

Honestly. It'll help you with the class; it might ALSO help your fellow students with the class ... and you'll learn a LOT more in the long run.

IMHO .. PSM
 
Old 11-06-2006, 12:15 PM   #8
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
u r right
i 100% agree with u
All i wish is that u ll never take a course with such kind of instructors.
If i were able to talk with him , i will b doing it the first
Can u believe me that all he said is 2lines and one ex on fork and the same for execv and wrote the project on z board ???
he explained nthg
till now i ve spent more than 30 hours researching and that's why i m asking help from ur lovely and helful forum
Anyway thanks for all ur help, it was really the only place that from where i was able to collect useful and exact information
 
Old 11-06-2006, 01:04 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, studentlb -

One other suggestion - it sounds like you might want to organize a "study group" with some of your fellow classmates.

Just a thought .. PSM

PS:
This link should contain all the sample code you need to finish this assignment:
http://www.cs.utk.edu/~plank/plank/c...p/lecture.html

a) Read the article
b) Copy the examples to your Linux PC
c) Compile and run the examples. Understand what they're doing.
d) Feel free to post any questions - to a new thread - here at LQ.
e) At this point, you should be able to complete your assignment. Honest!

Last edited by paulsm4; 11-06-2006 at 01:07 PM.
 
Old 11-06-2006, 02:26 PM   #10
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
Thanks a lot for ur help , i ll check the site
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect Output to remote PC possible?? wilkija Linux - Newbie 4 10-03-2006 12:43 PM
redirect output to device christina_rules Linux - General 4 05-23-2006 02:46 AM
Redirect output jimwelc Linux - Newbie 3 05-11-2006 11:10 AM
How to redirect output to a file? mus1402 Linux - Newbie 2 02-05-2006 09:42 AM
Ambiguous output redirect XFox Linux - Newbie 5 10-17-2003 07:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration