예제 #1
0
void execute()
{
    if(!back)//perform user commands given
    {
	if(strcmp(command[0],"quit")==0)
	    exit(0);
	else if(strcmp(command[0],"cd")==0)
	    cd();
	else if(strcmp(command[0],"fg")==0)
	{
	    int com=(int)(command[1][0]-'0');
	    fg(com);
	}
	else if(strcmp(command[0],"overkill")==0) 
	    overkill();
	else if(strcmp(command[0],"pinfo")==0)
	{
	    if(!ch)
	    {
		strcpy(list[p5].process,"pinfo");
		list[p5].back=0;
	    }
	    if(command[1][0]==0)//if only single argument given with pinfo command
		pinfo();
	    else
	    {
		int i=0,comm1,number=0,num1;
		int len1=strlen(command[1]);
		if(len1==4)
		    num1=10*10*10;
		else if(len1==3)
		    num1=10*10;
		else if(len1==2)
		    num1=10;
		else if(len1==1)
		    num1=1;
		while(i<len1)
		{
		    comm1=(int)(command[1][i]-'0');
		    number=num1*comm1+number;
		    num1=num1/10;
		    i++;
		}
		pid1(number);
	    }
	}
	else if(strcmp(command[0],"jobs")==0)
	{
	    if(!ch)
	    {
		strcpy(list[p5].process,"jobs");
		list[p5].back=0;
	    }
	    jobs();
	}
        else if(strcmp(command[0],"grep")==0)
             grep();      
	else if(strcmp(command[0],"kjob")==0)
	{
	    int comm=command[1][0]-'0';
	    int comm2=(int)(command[2][0]-'0');
            char *c1=command[1][0];
	    char *c2=command[2][0];
	    kjob((c1),(c2));
	}
	else if(strcmp(command[0],"exit")==0) 
	    fprintf(stderr,"The command is quit\n");
	else//for system defined commands
	{
	    if(!ch)
	    {
		strcpy(list[p5].process,command[0]);
		list[p5].back=0;
	    }
	    int returnval=fork();
	    if(returnval)
	    {
		list[p5++].pid=returnval;
		waitpid(returnval,&returnval,0);
	    }
	    else
	    {
		int i2=0;
		char arguments[101];
		char *args[argvals+1];
		while(i2<argvals)
		{
		    args[i2]=(char*)malloc(101*sizeof(char));
		    strcpy(args[i2],command[i2]);
		    i2++;
		}
		args[i2]=NULL;
		int flag2=1;
                if(redir_in)//incase of redirection
                {
                    if(redir_in<argvals -1)
                     {  
                      freopen(command[redir_in + 1],"r",stdin);
                      args[redir_in]=NULL;
                     }  
                    else
                     {
                     fprintf(stderr,"Give filename for redirection\n");
                     exit(0);
                     }
                }
                if(redir_out)
                {
                    if(redir_out < argvals -1)
                    {
                        freopen(command[redir_out + 1],"w",stdout);
                        args[redir_out]=NULL;
                    }
                    else
                    {
                      fprintf(stderr,"Give filename for redirection\n");
                      exit(0);
                    }
                 }                
		printf("%s",*args);
		flag2=0;
		execvp(args[0],args);//execute the command in arg
		fprintf(stderr,"Not a valid shell command\n");
		exit(0);
	    }
	}
    }
    else
    {
	if(!ch)
	{
	    strcpy(list[p5].process,command[0]);
	    list[p5].back=1;
	}
	int returnval=fork();
	if(returnval)
	{
	    if(!ch)
		list[p5++].pid=returnval;
	    printf("Command %s\t Pid %d\n",command[0],returnval);
	}
	else
	{
	    int i2=0;
	    int flag3=0;
	    char *args[argvals];
	    while(i2<back) 
	    {
		args[i2]=(char*)malloc(101*sizeof(char));
		strcpy(args[i2],command[i2]);
		i2++;
		flag3=1;
	    }
	    args[i2]=NULL;
	    execvp(args[0],args);//execute the command in args
	    fprintf(stderr,"Not a valid Shell command\n");
	    exit(0);
	}
    }
}
예제 #2
0
파일: shell.c 프로젝트: sarthak-0415/shell
int  main(void)
{	
	char  line[1024];           
	
	int mode;
	process * root;
	
	char  curDir[100];
	getcwd(curDir,100);
	
	pinfo *head;
	head=NULL;
	while (1)
	{     
		global_pipes=0;
		initials(curDir); 
		mode=0;            
		
		fflush(stdin);
		fflush(stdout);
		
		gets(line);              /*   read in the command line     */
		//printf("line ==%s\n",line);
		
		if (*line == '\0') 
			continue;

		printf("\n");
		
		int i=0;
		
		while((line[i]) != '\0')
		{
			if(line[i] == '&')
			{
				mode++;
				//line[i]='\0';
			}	
			i++;
		}
		
		//jobs
		if(strcmp(line, "jobs") == 0)
		{
			head=display(head);
			continue;
		}
			
		root=parse(line); 
		
		/*
		int p=0;
		while(argv[p] != NULL)
		{
			printf("argv = %s\n",argv[p]);
			p++;
		}    
		
		p=0	;
		while(out[p] !=NULL)
		{
			printf("out = %s\n",root->out);
			p++;
		} 
		p=0	;
		while(in[p] !=NULL)
		{
			printf("in = %s\n",root->in);
			p++;
		}
		*/
		//pinfo 
		if(strcmp(line, "pinfo") == 0)
		{	
			fflush(stdin);
			fflush(stdout);
			ppinfo(root-> argv);
			continue;	
		}
		
		//overkill
		if(strcmp(root->argv[0],"overkill")==0)
		{
			head=overkill(head);
			continue;
		}
		
		//fg
		else if(strcmp(root->argv[0],"fg")==0)
		{
			head=fg(head,root->argv);
			continue;
		}
		
		//kjob
		else if(strcmp(root->argv[0],"kjob")==0)
		{
			head=kjob(head,root->argv);
			continue;
		}	
		
		//exit
		else if (strcmp(root->argv[0], "quit") == 0) 
			exit(0);           			  
		
		//cd		
		if (strcmp(root->argv[0], "cd") == 0)
		{
				chdir(root->argv[1]);
				continue;
		}
		else
		{	if(global_pipes != 0)
				head=executepipes(root,head,mode);  
			else
				head=execute(root,head,mode);
		}
	}
}