Hi, I am new in linux programming, and going to build a bash shell with execvp().
My problem is when I execute my program with command in .txt file as input, the execvp will fail and the perror() shows no such file or directory. However, if I type command directly after executing program, the output is correct.
Correct case: ./tmp, type ls, and list files in current directory.
Incorrect case: ./tmp < testInput.txt(with ls in file), and execvp() fails with showing no such file or directory.
May I know how to fix execvp() fail when input as .txt file or is there any detail document should I study further? Any suggestion is appreciated. Thanks.
Some thoughts.
1. read() does not append a \0, which will cause str... functions to barf.
2. read() when redirected will see a stream in full buffer mode rather than line buffer mode.
3. You don't check read() for returning EOF.
fgets() would be a better substitute, since it's semantics don't change when you redirect from a file.
Because strtok() writes \0 into the string, all you really need to do is wholeCmd[i] = token;
There is no need to malloc and copy each token.
Hi, thanks for reply!
Yes, I didn't learn read() well, and that's the reason cause the program appears error. I switch to fgets() and it works.
And thanks for correcting my strtok() usage.
I read http://www.cplusplus.com/reference/cstring/strtok/ again and realize that my usage was wrong. Thanks for the help!