Search code examples
integerbooleanposixsemantics

Why do certain POSIX system calls return an integer instead of a boolean?


In the lastest version of the POSIX.1-2017 specification, some system calls, like link()/unlink(), access() or chmod() return either 0 (successful) or -1 (unsuccessful).

In that case, wouldn't it have been more semantically correct to return a boolean ?

And if I change the return type from int to boolean, is it still POSIX-compliant ?


Solution

  • I think it has been done to have similar return type among all functions returning an integer value. Otherwise the programmer should check every time if that function is returning an int or a boolean.

    Originally bool was not even a type in C: I don't know it was also a choice for retro-compatibility.

    In some cases you don't have even choice: take as examples the following:

    int fflush(FILE *fp) Returns: 0 if OK, EOF on error

    int fclose(FILE *fp); Returns: 0 if OK, EOF on error

    int atexit(void (*func)(void)) Returns: 0 if OK, nonzero on error

    EOF is a macro generally defined as -1 but it is not guaranteed. In some other cases you want to specify the error