xref: /netbsd-src/external/bsd/unbound/dist/compat/ctime_r.c (revision 7a540f2bd4f5b968566c2607d6462c7f2fb452cf)
13b6c3722Schristos /* taken from ldns 1.6.1 */
23b6c3722Schristos #include "config.h"
33b6c3722Schristos #ifdef HAVE_TIME_H
43b6c3722Schristos #include <time.h>
53b6c3722Schristos #endif
63b6c3722Schristos #include "util/locks.h"
73b6c3722Schristos 
83b6c3722Schristos /** the lock for ctime buffer */
90cd9f4ecSchristos static lock_basic_type ctime_lock;
103b6c3722Schristos /** has it been inited */
113b6c3722Schristos static int ctime_r_init = 0;
123b6c3722Schristos 
133b6c3722Schristos /** cleanup ctime_r on exit */
143b6c3722Schristos static void
ctime_r_cleanup(void)153b6c3722Schristos ctime_r_cleanup(void)
163b6c3722Schristos {
173b6c3722Schristos 	if(ctime_r_init) {
183b6c3722Schristos 		ctime_r_init = 0;
193b6c3722Schristos 		lock_basic_destroy(&ctime_lock);
203b6c3722Schristos 	}
213b6c3722Schristos }
223b6c3722Schristos 
ctime_r(const time_t * timep,char * buf)233b6c3722Schristos char *ctime_r(const time_t *timep, char *buf)
243b6c3722Schristos {
253b6c3722Schristos 	char* result;
263b6c3722Schristos 	if(!ctime_r_init) {
273b6c3722Schristos 		/* still small race where this init can be done twice,
283b6c3722Schristos 		 * which is mostly harmless */
293b6c3722Schristos 		ctime_r_init = 1;
303b6c3722Schristos 		lock_basic_init(&ctime_lock);
313b6c3722Schristos 		atexit(&ctime_r_cleanup);
323b6c3722Schristos 	}
333b6c3722Schristos 	lock_basic_lock(&ctime_lock);
343b6c3722Schristos 	result = ctime(timep);
353b6c3722Schristos 	if(buf && result) {
363b6c3722Schristos 		if(strlen(result) > 10 && result[7]==' ' && result[8]=='0')
373b6c3722Schristos 			result[8]=' '; /* fix error in windows ctime */
383b6c3722Schristos 		strcpy(buf, result);
393b6c3722Schristos 	}
403b6c3722Schristos 	lock_basic_unlock(&ctime_lock);
41*7a540f2bSchristos 	return buf;
423b6c3722Schristos }
43