Writing a new helper function

Suppose I have a helper function f() that is called from main().
In f() I return 0 on success. What value shoudl I return on error 1 or -1. What is BSD convention ?

Thanks.
bsd_newbie
 
You normally assign different numbers for different errors. And most people simply use 1, 2, 3, .... You should document what each number means. -1 will simply be read 255, because the shell only expects an unsigned byte.

This is from one of my manual pages:
Code:
EXIT CODES
     1       An unknown parameter has been supplied.

     2       An unknown mode command has been supplied.

     3       More than one mode commands have been supplied.

Often return values are documented with symbolic names. This is good for C-coders, but it's an annoyance in shell-scripting.
 
bsd_newbie said:
Suppose I have a helper function f() that is called from main().
In f() I return 0 on success. What value shoudl I return on error 1 or -1. What is BSD convention ?

From <stdlib.h>

#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
 
Also check <sysexits.h>.

I personally include this header, then use my own exit status macros like:
#define EX_MYPROGBARFED EX__MAX+1
#define EX_MYPROGDIDABOOBOO EX__MAX+2

etc.
 
Back
Top