C言語です
fork(),execv(),wait()などの関数を用いて簡易的なシェルを実装して見たいのですが、
execv()を呼び出すところで上手くいきません。
入力はパス名とコマンドを空白区切で実行します。
入力された1行を空白文字で区切り、文字列配列に格納。
その配列を用いてexecvを呼び出します。
コマンドラインの"0>>"の0はコマンドの実行結果に応じて変化します。
個人的な考察としては、execvに渡す配列の末尾にNULL文字が上手く格納されてないのが原因だと思いますが、詳しいところがよくわかりません。
ご教授のほどよろしくお願いします。
###発生している問題・エラーメッセージ
$ ./test 0>> /bin/ls -l execv : No such file or directory 0>> /bin/ls execv : No such file or directory 0>>
###該当のソースコード
C
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <unistd.h> 5#include <sys/types.h> 6#include <sys/wait.h> 7 8char* gettoken(char **ptr) { 9 char *tmp = *ptr; /* tmp[idx] の形で使用する */ 10 int idx = 0; 11 while(tmp[idx]==' '){/* 先頭のスペースを読み飛ばす */ 12 idx++; 13 } 14 int start=idx; //start:start index of string 15 while(tmp[idx]!=' ' && tmp[idx]!='\0'){/* 単語の終わりを見つける */ 16 idx++; 17 } 18 //単語の終りはこの行でのt-1 19 if(start==idx){ 20 return NULL; 21 } 22 int n = idx-start; //idx-start:string size 23 char *token = malloc(n+1); //+1:null character malloc で単語の領域を token に確保 24 strncpy(token,&tmp[start],n);/* 単語の文字を token の指す領域にコピー */ 25 *ptr = &tmp[idx]; /* 次の呼び出し用に引数を更新 */ 26 return token; /* 単語を取り出せた場合 (他で NULL を返す) */ 27} 28 29char **buildup_arg(char *str) { 30 const int N = 10; 31 int i=0,sz = 0; 32 char **ptr = malloc(sizeof(char*)*N); 33 if(ptr==NULL){perror("malloc\n"); return 0;} 34 char *token = str; 35 while (token != NULL) { 36 token = gettoken(&str); 37 if(i>=sz){ 38 sz+=N; 39 ptr = realloc(ptr,sizeof(char*)*sz); 40 if(ptr==NULL){ perror("realloc"); return 0; } 41 } 42 ptr[i++] = token; 43 } 44 ptr[i++]=(char*)NULL; 45 return ptr; 46} 47int main() { 48 size_t sz=0,n; 49 char* str=NULL; 50 int cret=0; 51 while(printf("%d>> ",cret) &&(n=getline(&str,&sz,stdin)) != -1){ 52 if(str[0] == '\n'){ 53 continue; 54 } 55 char **arg = buildup_arg(str); 56 pid_t pid = fork(); 57 if(pid<0){ 58 perror("fork\n"); 59 return 1; 60 }else if(pid==0){ 61 execv(arg[0],arg); 62 perror("execv\n"); 63 //return -1; 64 //exit(-1); 65 }else{ 66 if(wait(&cret)==-1){ 67 perror("wait\n"); 68 } 69 printf("child exits with %d\n",WEXITSTATUS(cret)); 70 } 71 } 72 return 0; 73}
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
コマンドの最後に\nが含まれています。ですので/bin/echo abcは行けるかも