血的教训!希望还没备份自己重要文件的朋友们抓紧备份一份

个人觉得macOS Mojave 应该是这个世界上最不稳定的操作系统了,今天试着跑了跑自己写的shell 结果 直接编译过程中就把我的main文件直接杀掉了,真的不知道苹果是怎么想的?自己主打的OS都不尽善尽美,你卖NM电脑? 估计以后还是会转回Windos。奉劝各位一句 要么linux 要么windos !Macos 不易久留。
最后贴出一份自己写的shell 作为备份,以后得多多注意。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>


#define Miku_RL_BUFFERSIZE 1024
#define Miku_SEC_BUFFER    64
#define Miku_SEC_DELIM  " \t\r\n"
// \t 的意思是 横向跳到下一制表符位置 \r 的意思是 回车 \n 的意思是回车换行

char *Miku_read_line(void)
{
    int buffersize = Miku_RL_BUFFERSIZE ;                  //空间大小
    int position  =0 ;                                  //存入buffer的位置index
    
    int c ;                                             //每次所读取的 字符! 注意是字符!不是整数
                                                        //因为之后要判断EOF 这是一个int类型

    char * buffer = malloc(sizeof(char)* buffersize );  //创建一个数组 指向空间 的指针
    
    //检查缓冲区是否创建成功
    if(!buffer){
        printf("Miku: memoery allocation wrong !");
        exit(EXIT_FAILURE);
    }
    
    while(1){
        //字符获取
        c = getchar();
        //判断终止符EOF 和 换行符?
        if(c=='"'){
            continue;
        }
        else if(c == EOF || c =='\n'){
            buffer[position] = '\0';
            return buffer ;
        }
        else{
            buffer[position]= c ;
            position++;
        }
        
        //如果超出了我们限定的数组大小。
        if( position >= buffersize ){
            //扩大数组容量
            buffersize += Miku_RL_BUFFERSIZE;
            buffer = realloc(buffer, buffersize);
            
            if(!buffer ){
                printf("Miku: memoery allocation wrong !");
                exit(EXIT_FAILURE);
            }
        }
    }
}

char **Miku_split_line( char* line ){
    
    int bufsize = Miku_SEC_BUFFER;
    int position = 0;
    char **total_sec = malloc( sizeof(char*)*bufsize );
    char *sec ;
    
    if( !total_sec ){
        printf("Miku: memoery allocation wrong !");
        exit(EXIT_FAILURE);
    }
    
    sec =strtok( line , Miku_SEC_DELIM );
    while( sec!=NULL ){
        total_sec[position] = sec ;
        position++ ;
        //容量监控
        if(position >=bufsize){
            bufsize  +=Miku_SEC_BUFFER;
            total_sec = realloc(total_sec, bufsize);
            if(total_sec==NULL){
                printf("Miku: memoery allocation wrong !");
                exit(EXIT_FAILURE);
            }
        }
        sec = strtok(NULL ,Miku_SEC_DELIM );
        // 这个地方的参数NULL 表示接着上一个被找到的 位置继续往后查找。
    }
    total_sec[position] = NULL ; //封上
    return total_sec;
}



/*        内置函数         */
int Miku_cd(char **args);
int Miku_help(char **args);
int Miku_exit(char **args);
int Miku_info(char **args);


//一个数组 其中的元素类型为 char型指针(即指向字符串首字符)
char *builtin_str[] = {
  "cd",
  "help",
  "exit",
  "info"
};

int (*builtin_func[]) (char **) = {
  &Miku_cd,
  &Miku_help,
  &Miku_exit,
  &Miku_info
};


int number_of_bulidins (){
    return sizeof(builtin_str)/sizeof(char*);
}


int Miku_cd(char** args){
    if(args[1] == NULL ){
        printf("please enter a dirctory ");
    }
    else {
        if(chdir(args[1]) != 0){
              perror("Miku_shell"); //返回值:成功返回0 ,失败返回-1
        }
    }
    
    return 1;
}


int Miku_help(char **args){
    int i;
    printf("*********************Miku Shell*********************\n");
    printf("The bulidin functions are as follows\n");

    for (i = 0; i < number_of_bulidins(); i++) {
      printf("  %s\n", builtin_str[i]);
    }

    return 1;
}


int Miku_exit (char **args){
    return 0;
}


int Miku_info(char **args){
    printf("“COMP2211 Simplified Shell by Luo Ruidi [email protected].”\n");
    return 1;
}



// *************系统调用,  //


int Miku_launch(char** args){
    
    pid_t pid ;  //进程 ID
  
    int status;
    
    pid = fork();  /*
                    在父进程中返回 >0
                    在子进程中返回  0
                    错误返回。     <0   由其返回值 可判断实在 父 还是在子
                    */
    if(pid < 0 ){
        perror("Miku_shell");
    }
    else if (pid == 0){
        //返回-1 表示失败
        if(  execvp(args[0], args)  == -1 )
        {
             perror("Miku_shell");
        }
    }
    else {
        do {
            waitpid(pid ,&status ,WUNTRACED);
        }while(!WIFEXITED(status) && !WIFSIGNALED(status));
    }
    
    return 1;
    
}

/*
   输入ex + 可执行文件名 + 传递的参数
 */

int Miku_Ex(char** args){
    
    pid_t pid ;
    int status;
    pid = fork();
    if(pid < 0 ){
        perror("Miku_shell");
    }
    else if (pid == 0){
        if(  execvp(args[1], args)  == -1 )
        {
             perror("Miku_shell");
        }
    }
    else {
        do {
            waitpid(pid ,&status ,WUNTRACED);
        }while(!WIFEXITED(status) && !WIFSIGNALED(status));
    }
    return 1;
    
}



/*
 
 */

int Miku_grep(char** args){
    pid_t pid ;
    int status;
    pid = fork();
    if(pid < 0 ){
        perror("Miku_shell");
    }
    else if (pid == 0){
        if(  execlp("grep","grep" ,args[1],args[2],args[3])  == -1 )
        {
             perror("Miku_shell");
        }
    }
    else {
        do {
            waitpid(pid ,&status ,WUNTRACED);
        }while(!WIFEXITED(status) && !WIFSIGNALED(status));
    }
    return 1;

}






int Miku_execute(char **args){
    int i= 0 ;
    
    if(args[0] == NULL ){
        return 1;
    }
    
    for(i=0 ;i<number_of_bulidins() ;i++)
    {
        if (strcmp(args[0], builtin_str[i]) == 0 )
            return (*builtin_func[i])(args);
    }
    
    
    if( strcmp(args[0], "ex") == 0 ){
        return Miku_Ex(args);
    }
    else if (strcmp(args[0], "mygrep") == 0 ){
        return Miku_grep(args);
    }
    
    
    return Miku_launch(args);
    
}



void Miku_loop(void){
    
    
    //全都是 指针型。目的是啥?
    char *line  ;  //读取用户输入
    char **args ; //转译 输入?
    
    int  status = 0 ; //状态显示
    
    do{
        printf("Miku_Shell> "); //打出一个提示符 告知用户输入
        line = Miku_read_line();
        args = Miku_split_line(line);
        status = Miku_execute(args);
  
        free(line);
        free(args);
    }while(status);
    

}



int main(int argc, const char * argv[]) {


    Miku_loop();
    return 0;
    
}


/* if( c =='"' ){
           continue;
       }
       else*/

猜你喜欢

转载自www.cnblogs.com/LuoRuidiLoveMiku/p/11620349.html