As far as I know*, this is not possible in an official way. This message is of the Windows shell/OF for when you provide a command that does not exist on the system. And as Windows is a closed system, probably this is an immutable default name, possessing no configuration file or Windows Registry entry.
There are some possible solutions, so much realists as unrealistic. One realistic solution would be by redirecting the error message. A unrealistic solution would be through reverse engineering of the Windows shell to change the messages. This last case is something that is beyond my control and that is illegal if you do not have Microsoft authorization.
I will deal with the realistic solution on Windows and Linux.
Linux
When calling some command in the shell through system
, you should initially redirect the error message to the "limbo" (/dev/null
) with 2>
. The operator 2>
redirects shell error messages to somewhere defined by you - in this case, the limbo.
The following example is to try using the directory listing command ls
:
int cod_erro;
cod_erro = system("ls 2> /dev/null");
Then check whether the command was Incorrectly executed. If It Is Incorrectly Executed,system
return the error code 32512
(or 0x7F00
in hexadecimal).
if( cod_erro == 32512 ) printf("Minha frase!\n");
If the command is invalid, the message will be printed. Otherwise, the program follows correctly without any invocation error message. It can show errors of the program itself being executed through system
but that’s beyond the scope of the question.
Windows
Follow the same steps followed for a Linux system - with some differences. In Windows, limbo is NUL
. Thus, using the command DIR
listing of files as an example, we have:
int cod_erro;
cod_erro = system("DIR 2> NUL");
What can change is the error code. As I don’t work with Windows and I noticed lack of documentation (in relation to the specific case of this question), I’ll teach you to find the error code.
Turn the following program to find the error code on your Windows:
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("Cod: %d\n", system("DIRa"));
system("PAUSE"); /* Para Windows. Substituir pelo equivalente em outro S.O. */
return(0);
}
Like the command DIRa
does not exist, an error code will be generated. Use this code to place on conditional if
. And just like that!
Other systems
Changing little or even none, it is possible to do the same in other systems. It is only necessary to know:
- how to redirect error messages in the system shell being used;
- what is the limbo directory of the system;
- what is the error code if the program does not exist;
Having this information, just replace what is needed in the sample code for Windows or Linux. It has no secret.
* If I’m wrong, correct me!
Maybe you have a beautiful and direct way of doing it that I don’t know about, but at least you can do it using
popen
to execute the command and store the result in a string, and compare the result with this error text. In windows if the result starts in'comando'
then it is a sign that it does not exist. It has an example of capturing the result of executing a command in the console in this answer– Isac