自己写一个简单的unix外壳

//enter cmd

//analyse cmd

//exec cmd

mark here

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         #include 
        
          #define DEBUG #define TRUE ((int)1) #define FALSE ((int)0) #define MAX 40 extern char** environ; /**************** STATIC FUNCTION ******/ static void fs_exev(char*); static char* fs_cmd_analyse( char *buf, char **argv ); static int fs_cmd_check(char **argv); /************MAIN FUNCTION*******************/ int main() { char cmd[100]; while(1) { printf("$:"); fgets(cmd,MAX,stdin); fs_exev(cmd); } return 0; } /*********************************/ /****** execute the cmd ******/ /*********************************/ static void fs_exev( char* cmd ) { //1)analyse cmd //2)check the cmd is avraible //3)use fork and execve function to create a new process char *argv[100]; char buf[100]; strcpy(buf,cmd); (void)fs_cmd_analyse(buf,argv); if( (pid = fork()) == 0 ) { if( execve(argv[0],&argv[1],environ) < 0) fprintf(stderr,":execve :%s\n",strerror(errno)); } int status; waitpid(pid,&status,0); } /*********************************************/ /*** anaylse the cmd,create a aviraible cmd***/ /*********************************************/ static char* fs_cmd_analyse(char *buf, char **argv) { char* t_cmd; char* t_cmd_head; int count; int endflag = 0; int argv_count; int start_flag = TRUE; argv_count = 0; t_cmd = buf; t_cmd_head = buf; count = 1; while( !endflag ) { if( (*t_cmd == '\0') ) { endflag = 1; } if( ((*t_cmd == ' ')||(*t_cmd == '\0') ) ||( *t_cmd == '\n') ) { if(start_flag == TRUE) { argv[argv_count] = t_cmd_head; argv[argv_count][count] = '\0'; printf("argv[%d] = %s\n",argv_count,argv[argv_count]); argv_count++; start_flag = FALSE; } if( ( *(t_cmd+1) != ' ' ) &&( *(t_cmd+1) != '\n') &&( *(t_cmd+1) != '\0') ) { t_cmd_head = t_cmd+1; count = 1; start_flag = TRUE; } } if( (*(t_cmd+1) !=' ') && ( *(t_cmd+1) != '\n') ) count++; t_cmd++; } argv[argv_count] = NULL; return *argv; } /*********************************************/ /**** check the cmd is ok or not**************/ /*********************************************/ static int fs_cmd_check(char **argv) { int status; return status; } 
         
       
      
      
     
     
    
    
   
   

这里只是最简单的雏形,当然要完善的有很多,又时间再来改把。大笑

不知道为什么一定要刷行一下才能看到代码疑问


建造者(Builder)模式是一种创建型设计模式,它允许你创建复杂对象,而无需直接实例化它们的内部部件。以下是一个简单的建造者模式的示例代码: ``` public class Pizza { private String crust; private String sauce; private String topping; public static class Builder { private String crust; private String sauce; private String topping; public Builder() { } public Builder crust(String crust) { this.crust = crust; return this; } public Builder sauce(String sauce) { this.sauce = sauce; return this; } public Builder topping(String topping) { this.topping = topping; return this; } public Pizza build() { return new Pizza(this); } } private Pizza(Builder builder) { this.crust = builder.crust; this.sauce = builder.sauce; this.topping = builder.topping; } public String getCrust() { return crust; } public String getSauce() { return sauce; } public String getTopping() { return topping; } public static void main(String[] args) { Pizza pizza = new Pizza.Builder() .crust("thin") .sauce("tomato") .topping("cheese") .build(); System.out.println("Crust: " + pizza.getCrust()); System.out.println("Sauce: " + pizza.getSauce()); System.out.println("Topping: " + pizza.getTopping()); } } ``` 在这个示例代码中,我们定义了一个 `Pizza` 类,它包括了披萨的外壳、酱料和配料等属性。我们使用建造者模式来创建 `Pizza` 对象,它包括一个静态内部类 `Builder` 和一个私有的构造函数。`Builder` 类中包含了 `Pizza` 类中的属性,以及一个 `build` 方法,用来返回一个 `Pizza` 对象。在 `Pizza` 类的构造函数中,我们将 `Builder` 中的属性赋值给 `Pizza` 中的属性。我们还通过一个 `main` 方法来演示如何使用建造者模式创建 `Pizza` 对象。 通过这个示例代码,你可以在你的 Java 项目中使用建造者模式来创建复杂的对象,而无需直接实例化它们的内部部件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值