llcong 发表于 2018-8-24 06:43:28

写自己的shell解释器

  今天研究了简单的shell解释器,里面就虽然只可以解释几条简单的命令,但是看了一些资料,可以调用外部命令,这样的话简单的shell就不简单 了呵呵,我现在还在继续研究,尽量完善,争取写出属于自己的“不简单”的shell解释器。源代码过些时日再发布,就发在此空间,望高手指教,目前为止, 我自己写的shell(minish)可以获得用户名、主机名、路径,包括cd echo help jobs pwdquit等命令,还正在慢慢添加命令。经过一段时间学习,由于鄙人初学,有些命令的加入还是有些难度的,现在把源代码发布,望朋友们指正。
  虽然代码短小、功能简单,但毕竟是本人花一段时间,参考一些资料写的,假如转载请注明出处,谢谢!
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #include
  #define CMDNUM 100
  struct PRO{
  int p_num;
  char p_name;
  pid_t p;
  int state;
  } process;
  int Analyse(char cmd[],char *arg[],char buf[]);
  int func_choice(char cmd[],char *arg[]);
  int Help();
  int Echo(char *arg[]);
  int Cd(char *arg[]);
  int Pwd(char *arg[]);
  int JobList();
  int Record(char cmd[],pid_t p);
  int Now=0;
  int main()
  {
  int i;
  char path,*username,host,tmp;
  struct group *data;
  char *arg,cmd,buf;
  data=getgrgid(getgid());
  username=data->gr_name;
  strcat(tmp,username);
  for(i=0;i 0)
  {
  Record(cmd,pid);
  }
  if( pid < 0 )
  {
  printf(&quot;fork error\n&quot;);
  }
  if(pid == 0)
  {
  if( (execvp(cmd,arg)) < 0 )
  printf(&quot; 文件或目录不存在!\n&quot;);
  else
  {
  Record(cmd,pid);
  }
  }
  }
  if(Now)
  waitpid(pid,0,WNOHANG);
  else
  waitpid(pid,0,0);
  return 0;
  }
  //cd:修改当前的工作目录到另一个目录
  int Cd(char *arg[])
  {
  if( chdir(arg))
  printf(&quot;目录不存在!&quot;);
  return 0;
  }
  //pwd:显示当前的所在的工作目录
  int Pwd(char *arg[])
  {
  char path;
  if( getcwd(path,1024) == NULL)
  printf(&quot;获取路径失败!\n&quot;);
  printf(&quot;**%s**\n&quot;,path);
  return 0;
  }
  //help
  int Help()
  {
  printf(&quot;\n\n\n\t*****************************************************\n&quot;);
  printf(&quot;\t            Welcome to The MiniShell            \n&quot;);
  printf(&quot;\t-----------------------------------------------------\n&quot;);
  printf(&quot;\t欢迎使用简易版Shell程序,本程序具备以下功能:                  \n&quot;);
  printf(&quot;\t       cd:   修改当前的工作目录到另一个目录                  \n&quot;);
  printf(&quot;\t      pwd:   显示当前的所在的工作目录                  \n&quot;);
  printf(&quot;\t   echo:   显示echo后的内容且换行                         \n&quot;);
  printf(&quot;\t   help:   简要介绍Shell的使用方法以及功能                   \n&quot;);
  printf(&quot;\t   jobs:   输出当前Shell下都一系列子进程                  \n&quot;);
  printf(&quot;\t   quit:   退出Shell                              \n&quot;);
  printf(&quot;\t------------------------------------------------------\n&quot;);
  printf(&quot;\t                               制作时间:2012年5月20日\n&quot;);
  printf(&quot;\t                               版权所有:      prince \n&quot;);
  printf(&quot;\t*****************************************************\n\n\n&quot;);
  return 0;
  }
  //Echo:显示echo后的内容且换行
  int Echo(char *arg[])
  {
  int i=1;
  while(arg != NULL)
  {
  printf(&quot;%s &quot;,arg);
  }
  printf(&quot;\n&quot;);
  return 0;
  }
  //env
  int Environ()
  {
  if( (execl(&quot;/bin/env&quot;,&quot;env&quot;,(char *)0)) < 0 )//功能ENV实现
  printf(&quot;execl error\n&quot;);
  return 0;
  }
  //Jobs:输出当前Shell下都一系列子进程
  int JobList()
  {
  int i;
  printf(&quot;PID\t进程\t状态\n&quot;);
  for(i=0;i
页: [1]
查看完整版本: 写自己的shell解释器