130c2432des/* taken from ldns 1.6.1 */
230c2432des#include "config.h"
330c2432des#ifdef HAVE_TIME_H
430c2432des#include <time.h>
530c2432des#endif
630c2432des#include "util/locks.h"
730c2432des
830c2432des/** the lock for ctime buffer */
99ce1273desstatic lock_basic_type ctime_lock;
1030c2432des/** has it been inited */
1130c2432desstatic int ctime_r_init = 0;
1230c2432des
1330c2432des/** cleanup ctime_r on exit */
1430c2432desstatic void
1530c2432desctime_r_cleanup(void)
1630c2432des{
1730c2432des	if(ctime_r_init) {
1830c2432des		ctime_r_init = 0;
1930c2432des		lock_basic_destroy(&ctime_lock);
2030c2432des	}
2130c2432des}
2230c2432des
2330c2432deschar *ctime_r(const time_t *timep, char *buf)
2430c2432des{
2530c2432des	char* result;
2630c2432des	if(!ctime_r_init) {
2730c2432des		/* still small race where this init can be done twice,
2830c2432des		 * which is mostly harmless */
2930c2432des		ctime_r_init = 1;
3030c2432des		lock_basic_init(&ctime_lock);
3130c2432des		atexit(&ctime_r_cleanup);
3230c2432des	}
3330c2432des	lock_basic_lock(&ctime_lock);
3430c2432des	result = ctime(timep);
3530c2432des	if(buf && result) {
3630c2432des		if(strlen(result) > 10 && result[7]==' ' && result[8]=='0')
3730c2432des			result[8]=' '; /* fix error in windows ctime */
3830c2432des		strcpy(buf, result);
3930c2432des	}
4030c2432des	lock_basic_unlock(&ctime_lock);
4130c2432des	return result;
4230c2432des}
43