I'm writing a C library for a software project. I need to do some error reporting, but I'm a little bit too lazy to implement my own complex set of error-codes, variables and functions. Is it acceptable to use the errno facility provided by the libc for custom error reporting? All my errors fit into the categories given by the E... macros.
For instance, let's say my code includes a function that reads a SHA256 hash in hexdecimal notation and converts it into some sort of internal format. I want to use errnoto report errors:
#include <errno.h> int hash_fromstr(hash_t *out, const char *in) { /* ... */ if (strlen(in) != 65) { errno = EINVAL; return -1; } /* ... */ } Of course this example is ridiculously simplified, in reality much more errors may happen in other functions.
errnoin other parts of a users application that links against your library I don't see a problem with it. Perhaps you might use theerrnovariable but not alter it and return a copy of it?errnofor errors that occur inside the library itself - not necessarily in the function of the libc called by the library.errnoto report errors to the caller of your library? If so you might want to check out stackoverflow.com/questions/9856822/should-i-set-errno