Search code examples
cposixreturn-valuestandards-compliance

Return values of POSIX functions


I try to write POSIX compatible code that is shared between two POSIX compliant operating systems, namely QNX and a Linux variant.

I just found that there are small differences regarding the specification of function return values e.g. of pthread_mutex_trylock(). One documentation says it returns EOK (which evaluates to 0) the other says it returns plain 0 in case of success.

I assume I can safely check return values == 0 or != 0 and avoid the QNX EOK macro.

My questions:

  1. Does POSIX explicitely define which values have to be returned by functions in case of success and/or error?
  2. Strictly spoken, is a function that is specified to return EOK (not 0) POSIX compliant?
  3. Is EOK part of some standard?

Solution

    1. There are POSIX manual pages, consult them to find out what the POSIX definition of a function is. For example: http://www.unix.com/man-page/all/3posix/pthread_mutex_trylock/. In general, POSIX is very consistent, and in all cases where the return value is only used to signal success or failure, 0 means success.
    2. If EOK equals 0, then the QNX implementation is POSIX compliant. However, as you already said, use 0 in your own code to ensure it compiles on all platforms.
    3. As far as I can tell, it's not part of a platform-independent standard. Either you avoid using it, or if you believe EOK is more clear, then you can write this in your program:

    .

    #ifndef EOK
    #define EOK 0
    #endif