示例#1
0
int 
main(void)
{
  char* cmdLine;
  while(1){
    
    printf("JXYShell$ -");
    cmdLine = readline("> ");
    history(cmdLine);
    //scanf("%s", cmdLine);
    //printf("%s",cmdLine);
    //int num = piping(cmdLine);
    //printf("NUM:%d ", num);
    //parse(cmdLine);
    executeCommand(piping(cmdLine));
    //if (!strcmp(cmdLine, "exit")){
    //  exit(SUCCESS);
    //}
    /*if (cmdLine != NULL){
      history(cmdLine);
      executeCommand(cmdLine);
    }else{*/
      
    //}
  }
  return SUCCESS;
}
示例#2
0
文件: dshell.c 项目: sapid/Minix-Mods
/* Fork and execute _args[cmds[z]] */
int fexec(int z, int* oldPipe, int* newPipe){
   pid = fork();
   piping(pid, oldPipe, newPipe);
   if(!pid) {
      /* In child */
      execvp(_args[cmds[z]], &_args[cmds[z]]);
      fprintf(stderr, "Child: Execution of %s failed.\n", _args[cmds[z]]);
      switch(errno){
         case ENOTDIR:
            fprintf(stderr, "\tA component of the path prefix is not a directory.\n"); break;
         case ENOENT:
            fprintf(stderr, "\tThe process file does not exist.\n"); break;
         case EFAULT:
            fprintf(stderr, "\tBad path, argv or envp.\n"); break;
         case E2BIG:
            fprintf(stderr, "\tArg list is too long.\n"); break;
         case ENOMEM:
            fprintf(stderr, "\tOut of memory.\n"); break;
         case EIO:
            fprintf(stderr, "\tIO error.\n"); break;
         case EACCES:
            fprintf(stderr, "\tPermission error or file is not +x.\n"); break;
         default:
            fprintf(stderr, "\tMisc error.\n"); break;
      } exit(1);
   }
   return pid;
}
示例#3
0
void exec_cmdline(struct command *L){
    int i, in, out, status;
    pid_t pid;
    
    if (L->pipe == 1)
        piping(L);
    else {
        pid = fork();
        if (pid == 0){
        
            if (L->infile || L->outfile)
                io_redirect(L);
            
            i = execvp(L->cmd, L->argv);
            printf("errno is %d\n", errno);
            if (i < 0){
                printf("%s: command not found\n", L->cmd);
                exit(1);
            }
        }
        else if (pid < 0){
            printf("fork failed\n");
            exit(1);
        }
        else {
            //pid of child
            if (L->pid == 0)
                L->pid = pid;
            //printf("pid: %d\n", L->pid);
            if (L->pcpu > 0){
                LALARM = L;
                alarm(5);
                //efficiency_enforcement(L, pid);
            }
            if (L->background == 0)
                wait(NULL);
        }
    }
}
示例#4
0
void runshell(){
    int flag, inp_flag, out_flag, ifd, ofd, stdin_copy, stdout_copy;
    char pth[BUFFER];
    char fileIn[BUFFER], fileOut[BUFFER];
    strcpy(pth,"");
    while(1){
        printf("myshell@%s> ", getcwd(line,BUFFER));
        line[0] = '\0';
        fgets(line, BUFFER, stdin);
        clearTabs(line);
        removeLastEnter(line);
        // printf("\nLINE = {%s}\n",line);

        int pipeCount = isPipingNeeded(line);
        // printf("\npipeCount = %d\n",pipeCount);
        if(pipeCount>0){
        	piping(line,pipeCount);
        	continue;
        }
        

        /************ NO PIPING IS REQUIRED ***********/
        inp_flag = extractWord(line, '<' ,fileIn);
        out_flag = extractWord(line,'>', fileOut);
        
        if(inp_flag != -1 && out_flag != -1){
        	if(inp_flag == 1){
        		stdin_copy = dup(0);
        		close(0);
        		ifd = open(fileIn, O_RDONLY);
				if (ifd < 0) {
		        	fprintf(stderr, "Unable to open input file in read mode...\n");
		        	continue;
				} 
        	}
        	if(out_flag == 1){
        		stdout_copy = dup(1);
        		close(1);
        		ofd = open(fileOut, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
				if (ofd < 0) {
    			    fprintf(stderr, "Unable to open output file in write mode...\n");
		        	continue;
				} 
        	}
        }
        
        if(feof(stdin)){
        	printf("\n");
        	exit(0);
        }
        separateWord(line);
        //printf("%s",words[0]);
        if(!strcmp(words[0],"cd")){
            strcpy(pth,"");
            strcat(pth,getcwd(line,BUFFER));
            //if(words[1][0] != '/')
            strcat(pth,"/");
            strcat(pth,words[1]);
            flag = chdir(pth);
            if(flag == -1){
            	flag = chdir(words[1]);
            	if(flag == -1) printf("\nNo Such file or directory !!\n");
                	// runProg(words[0],"",words[1]);
            }       
        }
        else if(!strcmp(words[0],"pwd")){
            printf("%s\n",getcwd(line,BUFFER));
        }
        else if(!strcmp(words[0],"mkdir")){
            strcpy(pth,"");
            strcat(pth,getcwd(line,BUFFER));
            strcat(pth,"/");
            strcat(pth,words[1]);
            flag = mkdir(pth,0777);
            if(flag == -1){
            	flag = mkdir(words[1],0777);
            	if(flag == -1)
                	runProg(words[0],"",words[1],0);
            }               
        }
        else if(!strcmp(words[0],"rmdir")){
            strcpy(pth,"");
            strcat(pth,getcwd(line,BUFFER));
            strcat(pth,"/");
            strcat(pth,words[1]);
            flag = rmdir(pth);
            if(flag == -1){
            	flag = rmdir(words[1]);
            	if(flag == -1)
                	runProg(words[0],"",words[1],0);
            }
        }
        else if(!strcmp(words[0],"ls") && !(words[1][0] == '-' && words[1][1] == 'l')){
            DIR *dp;
            struct dirent *ep;
            dp = opendir ("./");
            if (dp != NULL)
            {
                while (ep = readdir (dp))
                puts (ep->d_name);
                    (void) closedir (dp);
            }
            else
                puts ("Couldn't open the directory.");
        }
        else if(!strcmp(words[0],"ls") && words[1][0] == '-' && words[1][1] == 'l'){
            DIR *d;
            struct dirent *de;
            struct stat buf;
            int i,j;
            char P[10]="rwxrwxrwx",AP[10]=" ";
            struct passwd *p;
            struct group *g;
            struct tm *t;
            char time[26];
            d=opendir(".");
            readdir(d);
            readdir(d);
            while((de=readdir(d))!=NULL)
            {
                stat(de->d_name,&buf);

                // File Type
                if(S_ISDIR(buf.st_mode))
                printf("d");
                else if(S_ISREG(buf.st_mode))
                printf("-");
                else if(S_ISCHR(buf.st_mode))
                printf("c");
                else if(S_ISBLK(buf.st_mode))
                printf("b");
                else if(S_ISLNK(buf.st_mode))
                printf("l");
                else if(S_ISFIFO(buf.st_mode))
                printf("p");
                else if(S_ISSOCK(buf.st_mode))
                printf("s");
                //File Permissions P-Full Permissions AP-Actual Permissions
                for(i=0,j=(1<<8);i<9;i++,j>>=1)
                AP[i]= (buf.st_mode & j ) ? P[i] : '-' ;
                printf("%s",AP);
                //No. of Hard Links
                printf("%lu",buf.st_nlink);
                //User Name
                p=getpwuid(buf.st_uid);
                printf(" %.8s",p->pw_name);
                //Group Name
                g=getgrgid(buf.st_gid);
                printf(" %-8.8s",g->gr_name);
                //File Size
                printf(" %lu",buf.st_size);
                //Date and Time of modification
                t=localtime(&buf.st_mtime);
                strftime(time,sizeof(time),"%b %d %H:%M",t);
                printf(" %s",time);
                //File Name
                printf(" %s\n",de->d_name);
            }
        }
        else if(!strcmp(words[0],"exit")){