48

I have two calls to two different methods :

void func1() 
{
  // do something 
  if (fail) 
  {
    // then set errno to EEXIST

  }

}

And the second method :

void func2() 
{
  // do something 
  if (fail) 
  {
    // then set errno to ENOENT

  }

}
  1. When I set the errno to some value , what does it do ? just error checking ?

  2. How can I set errno in the above methods func1 and func2 to EEXIST and ENOENT

Thanks

JAN
  • 21,236
  • 66
  • 181
  • 318

3 Answers3

77

For all practical purposes, you can treat errno like a global variable (although it's usually not). So include errno.h and just use it:

errno = ENOENT;

You should ask yourself if errno is the best error-reporting mechanism for your purposes. Can the functions be engineered to return the error code themselves ?

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 20
    Conventionally, `errno` is used for reporting *system errors*. Do you feel thay your library or your code is part of the system? If not, use another mechanism to report errors. – Basile Starynkevitch Jul 28 '12 at 12:02
  • 8
    Not necessarily, utility functions like strtol() also seem to return ERANGE, for example, that is not really a _system error_. – proteus Jan 03 '15 at 22:50
  • 3
    An example of place it may be appropriate to set errno would be in a read or write function callback passed to funopen(). In this case your implementation is expected to match the contract of a system call (read() or write()) so it would make sense to return -1 in case of error and set errno with the specific error. – Corbell Jun 23 '15 at 18:25
  • 1
    MSVS defines `errno_t __cdecl _set_errno(_In_ int _Value);` and `_get_errno` – are they of any value? – PJTraill Nov 23 '16 at 22:58
  • 3
    @BasileStarynkevitch, it's usual practic to set errno to zero before some library functions call (for example see [readdir](http://man7.org/linux/man-pages/man3/readdir.3.html) or [getpriority](http://man7.org/linux/man-pages/man2/getpriority.2.html)). There nothing bad to use errno in your runtime library or right in program functions. This is standard mechanism, why you should not use errno if your function is wrapper that extend some library function implementation that already use errno. – imbearr May 15 '20 at 13:16
  • @imbearr I've had a discussion on another question on [stackexchange codereview](https://codereview.stackexchange.com/questions/284105/tar-implementation/284146?noredirect=1#comment565202_284146) and as I now thinking about it, it can be missused which leads to an unlikely error that has nothing to do with the real error. I.e. if you set it "OBJECT_CREATE_ERROR" (an error defined by your library) and errno was set to "ENOMEM" then you lost the real source of error. Otherwise returning errno to show what failed could be better or enough. – mortytheshorty Mar 27 '23 at 15:36
14

IMO, the standard errno designed for system level. My experience is do not pollute them. If you want to simulate the C standard errno mechanism, you can do some definition like:

/* your_errno.c */
__thread int g_your_error_code;

/* your_errno.h */
extern __thread int g_your_error_code
#define set_your_errno(err) (g_your_error_code = (err))
#define your_errno (g_your_error_code)

and also you can still implement your_perror(err_code). More information, please refer to glibc's implementation.

coanor
  • 3,746
  • 4
  • 50
  • 67
5
#include <errno.h>
void func1() 
{
  // do something 
  if (fail) 
  {
    errno = ENOENT;
  }
}
perreal
  • 94,503
  • 21
  • 155
  • 181