The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
implementing ftp damn_bkb IP Networking 4 01-04-2007 02:35 PM
Implementing the redirection mobile01 High Level Programming 0 11-30-2006 08:39 AM
Implementing a shell in C jacques83 High Level Programming 1 11-13-2006 07:24 PM
need help in implementing simple interactive shell in C nix1209 High Level Programming 2 03-15-2006 06:26 AM
Implementing Concatenation(cat) toughguy2handle High Level Programming 2 09-21-2005 11:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-12-2005
Registered User
 

Join Date: Nov 2005
Posts: 5
Implementing a shell

I'm implementing a shell in C that supports piping, output redirection, and background processing, and a few other commands. I was wondering how I'd go about implementing the output redirection. So, I'd open a file and I'd fork and execute the command. But how would I get stdout into the file? Any help is greatly appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 11-13-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,657
Close stdin and then open some other file.
close()
open()
Both have man pages.
Reply With Quote
  #3  
Old 11-13-2005
fdarkangel's Avatar
Registered User
 

Join Date: Apr 2005
Location: /dev/null
Posts: 42
i too have a question on the topic.
how do i read from keyboard if stdin is actually stdout of another program. an example is less/more: keyboard input still works when they're piped.
Reply With Quote
  #4  
Old 11-13-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,657
Quote:
Originally Posted by fdarkangel
i too have a question on the topic.
how do i read from keyboard if stdin is actually stdout of another program. an example is less/more: keyboard input still works when they're piped.
This is more of a separate question. You probably should have started a new thread.

If you type "tty", you will see the name of your controlling terminal. If it is, say, /dev/pty/j2, then you could simply open that file to gain access to the keyboard. But it is a lot of work to get the name of your particular terminal, so there is a second way. In the unix environment, a process with a controlling terminal can open the special file /dev/tty and get their own controlling terminal. This won't work if there is no crontrolling terminal such as with a cron job. Even though there is only one file on the system called /dev/tty, each user that opens it will get his or her own keyboard.

However, this may not be a great move to make. Suppose that I have a complicated pipeline of 4 processes. If they all do this at the same time it will be pretty hopeless to get it all straight. And look at all the threads we get where folks need to script the su command and must "expect" to do it. Users tend to dislike programs that do this.
Reply With Quote
  #5  
Old 11-13-2005
Registered User
 

Join Date: Nov 2005
Posts: 5
Closing stdin gives me an infinite loop.
Reply With Quote
  #6  
Old 11-13-2005
Registered User
 

Join Date: Nov 2005
Posts: 5
Is there a way to maybe modify the file descriptor table so that I replace stdout with the file I've just opened? How would I access the file descriptor table though?
Reply With Quote
  #7  
Old 11-13-2005
fdarkangel's Avatar
Registered User
 

Join Date: Apr 2005
Location: /dev/null
Posts: 42
Quote:
Originally Posted by Perderabo
This is more of a separate question. You probably should have started a new thread.
well, i've came across with that problem when i attemped to write a shell. i thought, programs' output should be filtered/processed by shell interpreter first, and then sent to shell screen-output program (in case they're seperate programs) and still be able to catch keyboard input to send programs ctrl+c etc. i've given less & more as examples since they're more common to Unix folks...

however, thanks for the reply.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0