I have a function,that open X11 display, do some stuff, and closes it. I use errno to catch errors throughout the code. After sometime debuffing I learned, that even though XOpenDisplay return the actual pointer (not NULL), errno is set to 11 in this function.
int
take_screenshot()
{
/* Get display */
Display *display = XOpenDisplay(NULL);
/* XOpenDisplay may set errno to 11
* even though display is not NULL
*/
if (!display)
{
fprintf(stderr, "Error opening display: %s", strerror(errno));
return 1;
}
XCloseDisplay(display);
return 0;
}
It works fine, if add just errno = 0
after if statement
, but this looks like a bad idea. Also in man page it said that errno initial state is platform-dependent, but i check and it's value is 0 before calling XOpenDisplay
.
Thank you in advance!
As @Some_programmer_dude metioned, I have mistread errno
, which I used as an error indicator, so I should simply check docs and use errno
only after NULL
check