Hi all.
This is my first post here, and i need urgent help with a programming assignment.
I am working on an assignment which requires me to build a simple shell. My shell must handle simple commands with arguements and ones with piping and I/O redirection.
The problem is i have no idea how to implement this redirection. The shell does alot of other things. I'm only stuck at this!
To further explain, my program reads the user's input such as: "ls -1 > text.txt" and does what a normal shell does. The problem is redirecting one's output as an input and the reverse. I have to execute two of the commands by forking two children and execve-ing. Then i need to do the redirection thing.
I have no idea were to start. I suppose it has to do with piping? I'm not sure.

Thanks alot for your help!

Interesting.. I don't know the answer to your question.
In Java though you have a way to get a handle to the Input and Output streams of a child process you've forked.. So I'm sure it would be possible in C as well.. will check.. Do post the answer if you get it..

I think you could use freopen to do it somehow... I'm not sure that it'll work, but hopefully it'll at least be a starting point.

The freopen() function opens the file whose name is the string pointed to by path and associates the stream
pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as
in the fopen() function. The primary use of the freopen() function is to change the file associated with a
standard text stream (stderr, stdin, or stdout).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.