ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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");
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
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.