コマンドとその引数を使用して呼び出すexecvp
と、コマンドが正当でない場合があります。
たとえば、フォークしたSONプロセスを使用してシェル (bash シェル) でこれを行うと、次のようになります。
$ ls ffdfdfd
出力は次のとおりです。
$ ls: cannot access ffdfdfd: No such file or directory
ここで、その正確なメッセージをファイルに渡したいと思います。私はperror
この方法で試しました:
void directErrors(char * arg)
{
perror(arg); // execute the problem to screen
// now execute the problem to file
FILE* myFile = fopen("errors.log", "a");
if(myFile == NULL)
{
perror("fopen");
exit(-1);
}
fprintf(myFile, "%s: %s\n", arg, strerror(errno));
fclose(myFile);
}
ただし、コマンドX
が失敗したことを書き込むだけです。
execvp
呼び出し後に取得した正確な出力をどのように指示できますか?
私のコードでは、次のexecvp
ように呼び出します。
executeCurrentCommand = execvp(*(arg)[0], *arg);