Detect and change System() error message

Asked

Viewed 147 times

2

I have a question about the use of function system. For example, if we execute the following command:

(system"");

If the example command is not found on the system the function prints the following error message:

'example' is not recognized as an internal or external command, a operable program or a batch file.

How do I exchange this error message that returns to a custom message?

  • 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

1 answer

2


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!

  • Instead of 32512 the correct would not be 0?? The Sytem function returns a non-zero value only if the command exists.

  • @user72726 No. Only returned 0 when doing system(NULL) and there is no shell or when, succeeding in executing any command, this command returns 0 which in turn is passed on to system return to your code. For example, a system("ls") returns 0. See more in the manual (type man system at your penguin’s terminal) in the Return Value (or Return Value). Alternatively, run some tests with the code to find a localized error code in the answer. Maybe you need to modify c/ a GUI to be able to observe certain behaviors.

  • Yes you’re right. I had not understood the text of cppreference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.