LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-25-2005, 07:34 PM   #1
amitbern
LQ Newbie
 
Registered: Nov 2005
Posts: 12

Rep: Reputation: 0
Implementing Input Redirection


I was trying to implement I/O redirection in unix simple shell.
The output (>) redirection is working well but I can't seem to use the input redirection (<) in the shell i created.
any ideas?



Code:
#include  <stdio.h>
#include  <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>

#include <fcntl.h>
#include <unistd.h>



void  execute(char **argv,int background_flag,char output_file[30],char input_file[30])
{
	pid_t  pid;
	int    status;
	int j=0;
	int exe_process;
	int fdin,fdout;

     	if ((pid = fork()) < 0)       /* fork a child process           */
	{
          	printf("ERROR: forking child process failed\n");
          	exit(1);
     	}
     	else if (pid == 0)           /* child process:         */
	{
		printf("\033[35m\033[1m[ %d ]\033[m\033[m\n",getpid());
		

		if(strcmp(output_file,"\0"))
		{
                	if ((fdout = open(output_file,O_WRONLY|O_CREAT|O_TRUNC)) == -1)
			{
				fprintf(stderr," Could not open file %s for writing.\n",output_file);
				exit(1);
			}
	               	if ( dup2(fdout,STDOUT_FILENO)== -1)
			{
				fprintf(stderr,"dup2 Error");
			}
	                close(fdout);
		}



                if(strcmp(input_file,"\0"))
                {
                        if ((fdin = open(input_file,O_WRONLY)) == -1)
                        {
                                fprintf(stderr," Could not open file %s for reading.\n",input_file);
                                exit(1);
                        }
			printf("fdin=%d\n",fdin);
                        if ( dup2(fdin,STDIN_FILENO)== -1)
                        {
                                fprintf(stderr,"dup2 Error");
                        }
                        close(fdin);
                }




		if (execvp(*argv, argv) < 0)       /* execute the command  */
		{
               		printf("ERROR: exec failed\n");
               		exit(1);
        	}	
     	}
     	else 	                                   /* parent:      */
	{
       		if (background_flag==0)   
		{
		           if (strcmp(output_file,"\0"))	close(fdout);
		           if (strcmp(input_file,"\0")) close(fdin);
			while (wait(&status) != pid);
			if (!WIFEXITED (status))
			{
				printf ("the child process exited abnormally from signal %d\n",WTERMSIG(status ) );
			}
		}


	}
}

int  main(void)
{
char  read_line[1024];
char output_file[30];
char input_file[30];
char* arg_list[10];
int i,background_flag,status;
pid_t pid;


while (1) 
{                  
	i=0;
	strcpy(output_file,"\0");
	strcpy(input_file,"\0");
	background_flag=0;
	printf("\033[44m\033[1mUser_Shell:\033[m\033[m");	


        gets(read_line);            

	//clean zombie proccess
	while (((pid=waitpid(-1,&status,WNOHANG))!=-1)&&(pid!=0))	
		printf("\033[32m\033[1m[ %d ]:Done\033[m\033[m\n",pid);


	if (read_line[0]!=NULL)
{
        arg_list[i] = strtok(read_line," ");
	while (arg_list[i]!= NULL)
	{
		if (!strcmp(arg_list[i],"&")) background_flag=1;
		else if (!strcmp(arg_list[i],">"))
		{	
			arg_list[i]=strtok(NULL," ");
			strcpy(output_file,arg_list[i]);
		}
		else if (!strcmp(arg_list[i],"<"))
		{
			arg_list[i]=strtok(NULL," ");
			strcpy(input_file,arg_list[i]);
		}
		else ++i;
		arg_list[i]=strtok(NULL," ");
	}	


          if (strcmp(arg_list[0], "exit") == 0) 
             exit(0);   



          execute(arg_list,background_flag,output_file,input_file);          
}

     }
return 0;
}

Last edited by amitbern; 11-25-2005 at 07:39 PM.
 
Old 11-26-2005, 12:01 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Variables fdin and stdout are not defined in the parent process. Probably you
should use pipe(2) and dup2(2).
 
Old 11-26-2005, 02:46 AM   #3
amitbern
LQ Newbie
 
Registered: Nov 2005
Posts: 12

Original Poster
Rep: Reputation: 0
But i don't need to pipe/dup2 in the parent area. all the redirections are staying with the child process.
and why does the output redirection (>) is working well here,
and the input (<) redirection not...
I am trying to run in my shell : wc < testfile
but i get:

0 0 0

anyone?

Last edited by amitbern; 11-26-2005 at 03:24 AM.
 
Old 11-06-2006, 12:04 PM   #4
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
i know it s one year ago,but did it work with you??
 
  


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
Ctrl+Shift Unicode input gone, after installing Japanese Input Methodes polemon Linux - Newbie 1 09-20-2005 05:17 PM
Sendmail: timeout waiting for input from local during Draining Input andrewstr Linux - Software 0 07-14-2004 01:43 PM
Output & input redirection serotonincy Programming 3 04-12-2004 08:28 AM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
implementing a graph bprasanth_20 Programming 4 10-24-2003 11:44 PM

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

All times are GMT -5. The time now is 10:48 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