ryo-manba / minishell

A small shell like bash.
MIT License
1 stars 0 forks source link

[executer] カレントディレクトリを試す5つのパターン #46

Closed ryo-manba closed 3 years ago

ryo-manba commented 3 years ago

現象

% ./a.out 
hello world!
% a.out
./minishell: a.out: command not found
% unset PATH
% a.out
./minishell: a.out: No such file or directory
% export PATH
% a.out
./minishell: a.out: No such file or directory
% export PATH=
% a.out
./minishell: a.out: No such file or directory
% export PATH=xxx
% a.out
./minishell: a.out: command not found
% export PATH=.
% a.out
hello world!
% export PATH=:
% a.out
hello world!
% 

あるべき状態

bash-3.2$ ./a.out 
hello world!
bash-3.2$ a.out
bash: a.out: command not found
bash-3.2$ unset PATH
bash-3.2$ a.out
hello world!
bash-3.2$ export PATH
bash-3.2$ a.out
hello world!
bash-3.2$ export PATH=
bash-3.2$ a.out
hello world!
bash-3.2$ export PATH=xxx
bash-3.2$ a.out
bash: a.out: command not found
bash-3.2$ 
bash-3.2$ export PATH=.
bash-3.2$ a.out
hello world!
bash-3.2$ export PATH=:
bash-3.2$ a.out
hello world!

PATHの状態が以下のどれかならカレントディレクトリを試す。

  1. PATHがunsetされている場合
  2. valueがNULLの場合
  3. valueが空文字列の場合
  4. valueが. の場合
  5. valueが: の場合

4, 5は行えているので残り3パターンに対応する。

corvvs commented 3 years ago
--- r1  2021-09-25 15:38:23.000000000 +0900
+++ r2  2021-09-25 15:35:15.000000000 +0900
@@ -2444,56 +2444,56 @@
 # minishell: exit status = 126
 # bash     : exit status = 126
 ---------------------------------
-[OK] case: export PATH=""; nosuchfile
+[KO] case: export PATH=""; nosuchfile
 ---------------------------------
 # minishell: stdout
 # bash     : stdout
 # minishell: stderr
-/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
+/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: command not found
 # bash     : stderr
 /Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
 # minishell: exit status = 127
 # bash     : exit status = 127
 ---------------------------------
-[OK] case: export PATH=''; nosuchfile
+[KO] case: export PATH=''; nosuchfile
 ---------------------------------
 # minishell: stdout
 # bash     : stdout
 # minishell: stderr
-/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
+/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: command not found
 # bash     : stderr
 /Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
 # minishell: exit status = 127
 # bash     : exit status = 127
 ---------------------------------
-[OK] case: unset PATH; nosuchfile
+[KO] case: unset PATH; nosuchfile
 ---------------------------------
 # minishell: stdout
 # bash     : stdout
 # minishell: stderr
-/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
+/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: command not found
 # bash     : stderr
 /Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
 # minishell: exit status = 127
 # bash     : exit status = 127
 ---------------------------------
-[OK] case: unset PATH; export PATH; nosuchfile
+[KO] case: unset PATH; export PATH; nosuchfile
 ---------------------------------
 # minishell: stdout
 # bash     : stdout
 # minishell: stderr
-/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
+/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: command not found
 # bash     : stderr
 /Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
 # minishell: exit status = 127
 # bash     : exit status = 127
 ---------------------------------
-[OK] case: unset PATH; export PATH=; nosuchfile
+[KO] case: unset PATH; export PATH=; nosuchfile
 ---------------------------------
 # minishell: stdout
 # bash     : stdout
 # minishell: stderr
-/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
+/Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: command not found
 # bash     : stderr
 /Users/corvvs/reps/42cursus/minishell/mainflow/minishell: nosuchfile: No such file or directory
 # minishell: exit status = 127

5ケースKOが増えてます。

corvvs commented 3 years ago

なぜデグレード(OK->KOケースの出現)が起きたか

関数exec_create_split_pathに到達する場合、実行対象はコマンドであると判定されている。 しかしPATHがない場合にカレントディレクトリを見るなら、実行対象はコマンドではないことになる。 このズレがその後のコードでフォローされていないことが原因。

対応

ryo-manba commented 3 years ago

すみませんありがとうございます!