What is conventional return values for applications in Windows and GNU/Linux respectivly. 0 means success. But what should be used on user-requested abort. When I abort on Windows, it returns 3, but this value is not in the list of system error codes if it is not an ERROR_PATH_NOT_FOUND. GNU binutils uses 1. From a user perspective, returning GetLastError or errno would be good since they are documented, but is these only seems to cover lower level status codes. I am looking for a value that represents "Application terminated unsuccessfully"

The reason I wounder is that I want to

exit(errcode)

from a signal handler that catches some Access Violation/SIGSEGV (i.e programming errors) after printing a message about where it occured. Then the error code should be destiguishable from user input errors.

有帮助吗?

解决方案

This might help, http://tldp.org/LDP/abs/html/exitcodes.html those are the standard exit code. The rest of them I think are program dependant. basically you need to verify the documentation of the specific software you are looking for. As @devnull said, any exit code that is not zero implies an unsuccessful termination

其他提示

This is only few conventions about exit codes. Let's see what some manuals say:

The GNU C Library Reference Manual

  1. There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure ...

  2. A general convention reserves status values 128 and up for special purposes

  3. Some non-POSIX systems use different conventions for exit status values
  4. For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure.

ISO/IEC 9899:2011 (C11 standard)

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

That means if you want (and for most cases that's enough) just indicate success or failure you definitely should use EXIT_SUCCESS and EXIT_FAILURE. If you want indicate other errors, you should reinvent your own exit statuses. For example:

#define HEX_FILE_CREATE 2
#define HEX_FILE_CREATE 3
...

There is additional tips on what and how you should return:

  1. Warning: Don’t try to use the number of errors as the exit status. This is actually not very useful; a parent process would generally not care how many errors occurred. Worse than that, it does not work, because the status value is truncated to eight bits. Thus, if the program tried to report 256 errors, the parent would receive a report of 0 errors—that is, success
  2. For the same reason, it does not work to use the value of errno as the exit status—these can exceed 255

Conclusion:

  1. For success always use EXIT_SUCCESS
  2. Your failure exit status should be beetween 1 and 127
  3. Don't use errno error code as exit status
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top