ctime_r.c revision 238106
1238106Sdes/* taken from ldns 1.6.1 */ 2238106Sdes#include "config.h" 3238106Sdes#ifdef HAVE_TIME_H 4238106Sdes#include <time.h> 5238106Sdes#endif 6238106Sdes#include "util/locks.h" 7238106Sdes 8238106Sdes/** the lock for ctime buffer */ 9238106Sdesstatic lock_basic_t ctime_lock; 10238106Sdes/** has it been inited */ 11238106Sdesstatic int ctime_r_init = 0; 12238106Sdes 13238106Sdes/** cleanup ctime_r on exit */ 14238106Sdesstatic void 15238106Sdesctime_r_cleanup(void) 16238106Sdes{ 17238106Sdes if(ctime_r_init) { 18238106Sdes ctime_r_init = 0; 19238106Sdes lock_basic_destroy(&ctime_lock); 20238106Sdes } 21238106Sdes} 22238106Sdes 23238106Sdeschar *ctime_r(const time_t *timep, char *buf) 24238106Sdes{ 25238106Sdes char* result; 26238106Sdes if(!ctime_r_init) { 27238106Sdes /* still small race where this init can be done twice, 28238106Sdes * which is mostly harmless */ 29238106Sdes ctime_r_init = 1; 30238106Sdes lock_basic_init(&ctime_lock); 31238106Sdes atexit(&ctime_r_cleanup); 32238106Sdes } 33238106Sdes lock_basic_lock(&ctime_lock); 34238106Sdes result = ctime(timep); 35238106Sdes if(buf && result) { 36238106Sdes if(strlen(result) > 10 && result[7]==' ' && result[8]=='0') 37238106Sdes result[8]=' '; /* fix error in windows ctime */ 38238106Sdes strcpy(buf, result); 39238106Sdes } 40238106Sdes lock_basic_unlock(&ctime_lock); 41238106Sdes return result; 42238106Sdes} 43