In my C program, I am creating a child process and running execvp in the child. But I’m trying to change the error message to something else, for the execvp command (if there was an error).
I know that if it returned, then it was an error, then I can print my own custom error message on the next line. That’s one type of error that can occur, for example this happens if I give the command “sdfsd” to execvp. This part is working for me.
But if I type, “find sdfsd” then it does not return and prints “find: `sdfsd’: No such file or directory”.
I want to change this message (and essentially any kind of error message coming from exevcp) to my own custom one.
I believe I can use dup2 to do this, but I’m not sure how…
In the child process I tried
dup2(STDERR_FILENO, 1);
fclose(stderr);
But this just stops the child process from writing any error messages. I still can’t print my own message in all cases..
Does anyone know how to do this?
thanks
Since
execvpnever returns if it successfully starts the new program, you won’t be able to print your own error message in the child process after the program run byexecvpfails. One option would be to pipestderrto the parent process, intercept the error message there, and then print your own error message.For example: