This is might be really silly, I;ve been doing some trial and error testing and I am kinda clueless here..
I perform a stat() on a file that is not present. As a result errno is set to 2.
After that i call a function to perform logging and suddenly in the leg i see the errno set to 21 (Is a directory). By using printfs at various points in the code, I finally tracked down to strftime causing the change in errno. I dont know much about this function but does this have any potential of setting the errno to 21?
Code:
if (clock_gettime(CLOCK_REALTIME, &time) == -1) { \
sprintf(timestamp, "%s", <something>); \
//errno is 2
} else { \
//errno is 2
ms_part = (time.tv_nsec/1000000L) % 1000L; \
//errno is 2 \
(void)strftime(timestamp, \
sizeof("Mon dd HH:MM:SS.msec : "), \
"%b %d %H:%M:%S", \
localtime(&time.tv_sec)); \
//errno is 21!!
}