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
if -z not working in SH shell rijeshpp Shell Programming and Scripting 2 11-08-2007 02:09 AM
Variable not working correctly. walsh_j Shell Programming and Scripting 3 05-30-2007 03:50 PM
if not working correctly 2dumb Shell Programming and Scripting 3 05-03-2007 03:38 PM
Script not working correctly elchalateco UNIX for Dummies Questions & Answers 2 10-11-2002 05:09 PM
Shell Implementation clickonline1 UNIX for Dummies Questions & Answers 3 10-02-2001 05:52 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 11-04-2007
AirBronto AirBronto is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 6
Shell Implementation not working correctly


Code:

	//save in/out
	int tmpin = dup(0);
	int tmpout = dup(1);
	
	//set initial input
	int fdin;
	if(_inputFile)
	{
	   fdin = open(_inputFile, O_RDONLY | O_CREAT, S_IREAD | S_IWRITE);
	}
	else
	{
	   //use default input
	   fdin = dup(tmpin);
	}

	int ret;
	int fdout;
	for(int i = 0; i < _numberOfSimpleCommands; i++)
	{
	   //redirect input
	   dup2(fdin, 0);
	   close(fdin);
	   //setup output
	   if(i == _numberOfSimpleCommands-1) 
	   {// last command must be set so output redirection is set

	      if(_outFile)
	      {
	         fdout = open(_outFile, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
	      }
	      else
	      {  
	         fdout = dup(tmpout);
	      }
	   }
	   else
	   {
                   //simple command
	      //create pipe
	      int fdpipe[2];
	      pipe(fdpipe);
                  fdout = fdpipe[0];
                  fdin = fdpipe[1];
	    }

	    //redirect output
	    dup2(fdout, 1);
	    close(fdout);

	    //create child process
	    ret = fork();
	    if(ret == 0)
	    {
	       execvp((*_simpleCommands)[i]._arguments[0],(*_simpleCommands)[i]._arguments);
	       perror("execvp");
	       exit(1);
	     }//if ret
	}//for

	//restore in/out defaults
	dup2(tmpin, 0);
	dup2(tmpout, 1);
	close(tmpin);
	close(tmpout);
	
	if(!_background)
	{
		//wait for last command
		waitpid(ret, 0, 0);
	}

This is the code i have for the implementation of a shell that supports pipes and i/o redirection

So far this code has worked for one input and one outout redirection.
But will not work for pipeing, and i cannot for the life of me figure out why.
Ex. cat file1.cc | grep malloc

A simple command in my implementation is just a command with its arguments.

Any help would be great. Also please tell me if i need to clarify anything.

Last edited by AirBronto; 11-05-2007 at 03:12 PM..
 

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0