Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用“execvp”;ls";不在自定义shell中工作_C_Unix - Fatal编程技术网

用“execvp”;ls";不在自定义shell中工作

用“execvp”;ls";不在自定义shell中工作,c,unix,C,Unix,我正在做什么:为类项目创建自定义shell。刚刚开始实现我的fork和exec特性 我的问题:ls工作正常,直到我用cd更改目录。在第一张cd之后,ls要求列出当前目录中的文件。之后,它似乎失去了所有功能 我在下面包含了我的代码。感谢任何帮助(包括指出明显的新手错误)。我正处于掌握这些概念的早期阶段 int main(){ char *input = malloc(sizeof(char*) * BUFFERSIZE); char *token; char **token_array

我正在做什么:为类项目创建自定义shell。刚刚开始实现我的fork和exec特性

我的问题
ls
工作正常,直到我用
cd
更改目录。在第一张
cd
之后,
ls
要求
列出当前目录中的文件。之后,它似乎失去了所有功能

我在下面包含了我的代码。感谢任何帮助(包括指出明显的新手错误)。我正处于掌握这些概念的早期阶段

int main(){
  char *input = malloc(sizeof(char*) * BUFFERSIZE);
  char *token;
  char **token_array;
  int i;
  int return_status;

  printf("%s", BOILERPLATE);  
  if (input == NULL){
    printf("JASH: Buffer Allocation Failed");
    return 1;
  }

  // Shell Loop
  while(1){
    i = 0;

    printf("JASH:>  ");
    fgets(input, BUFFERSIZE, stdin);
    token_array = malloc(sizeof(char)*strlen(input));

    token = strtok(input, " \n()<>|&;");
    while(token != NULL){
      token_array[i] = token;
      token = strtok(NULL, " \n()<>|&;");
      i++;
    }

    // print tokens to confirm correct parsing of input
    printf("%s : %s\n", token_array[0], token_array[1]);

    //printf("%s\n", token_array[0]);
    if(exec_args(token_array) == 0){
      // programm exit successfully
      free(token_array);
      free(input);
      return 0;;
    }

  } // Loop End




  // free input buffer before exit
  free(token_array);
  free(input);
  return 0;

}


int exec_args(char ** t){
  int i;

  if(strcmp(t[0], "exit") == 0
     || strcmp(t[0], "quit") == 0
     || strcmp(t[0], "logout") == 0){
    // call built in function for exit
    exit(0);
  }

  if (strcmp(t[0], "cd") == 0){
    // call built in function for chdir
    if(chdir(t[1]) == 0){
      printf("Successfully changed directories to %s\n", t[1]);
      return 1;
    } else {
      printf("-JASH: cd: %s: No such file or directory\n", t[1]);
      return 1;
    }
  } 

  if (strcmp(t[0], "pwd") == 0) {
    char * p = malloc(sizeof(char*) * 100);
    getcwd(p, 100);
    printf("%s\n", p);
    free(p);
    return 1;
  } 

  if(strcmp(t[0], "help") == 0){
    printf("\n%s\n Help Documentation Coming soon!\n\n", BOILERPLATE);
    return 1;
  } 


  if(fork() == 0){
   if(execvp(t[0], t)){
     perror("JASH");
   } 

  } else {
    int status;
    wait(&status);
    printf("%d", status);
  }
  return 1;

}
intmain(){
char*input=malloc(sizeof(char*)*BUFFERSIZE);
字符*令牌;
字符**令牌数组;
int i;
int返回_状态;
printf(“%s”,样板);
如果(输入==NULL){
printf(“JASH:缓冲区分配失败”);
返回1;
}
//壳环
而(1){
i=0;
printf(“JASH:>”);
fgets(输入、缓冲区大小、标准输入);
令牌数组=malloc(sizeof(char)*strlen(input));
token=strtok(输入“\n()|&;”);
while(令牌!=NULL){
令牌_数组[i]=令牌;
token=strtok(NULL,“\n()|&;”);
i++;
}
//打印标记以确认输入的正确解析
printf(“%s:%s\n”,令牌数组[0],令牌数组[1]);
//printf(“%s\n”,令牌数组[0]);
if(exec_参数(令牌数组)==0){
//程序退出成功
免费(令牌_数组);
免费(输入);
返回0;;
}
}//循环结束
//退出前释放输入缓冲区
免费(令牌_数组);
免费(输入);
返回0;
}
int exec_参数(字符**t){
int i;
如果(strcmp(t[0],“退出”)==0
||strcmp(t[0],“退出”)==0
||strcmp(t[0],“注销”)==0){
//调用用于退出的内置函数
出口(0);
}
if(strcmp(t[0],“cd”)==0){
//chdir的调用内置函数
if(chdir(t[1])==0){
printf(“已成功将目录更改为%s\n”,t[1]);
返回1;
}否则{
printf(“-JASH:cd:%s:No这样的文件或目录,\n”,t[1]);
返回1;
}
} 
如果(strcmp(t[0],“pwd”)==0){
char*p=malloc(sizeof(char*)*100);
getcwd(p,100);
printf(“%s\n”,p);
自由基(p);
返回1;
} 
if(strcmp(t[0],“help”)==0){
printf(“\n%s\n帮助文档即将发布!\n\n”,样板文件);
返回1;
} 
如果(fork()==0){
if(execvp(t[0],t)){
佩罗(“JASH”);
} 
}否则{
智力状态;
等待(&状态);
printf(“%d”,状态);
}
返回1;
}

注意:更新:已修复。。将“free(token_array)”移动到我的shell while循环之外。
token_array=malloc(sizeof(char)*strlen(input))
应该是
sizeof(char*)
,因为您正在分配指针数组。如果
chdir
由于权限错误而失败,则会显示一条格式为“无此类文件或目录”的错误消息(写入stdout!)这并没有什么帮助<代码>人或读取。还要学习使用,特别是了解现有Shell正在做什么。研究自由软件外壳的源代码,例如
sash
bash
,以获得灵感。您没有按照execvp的要求在令牌数组的末尾添加空值。