1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 19:50:24 +02:00

(scm_gmtime): Use gmtime_r when available, for thread safety.

This commit is contained in:
Kevin Ryde 2004-03-20 23:18:06 +00:00
parent fa525c8a68
commit 45198ffbfe

View file

@ -397,6 +397,12 @@ SCM_DEFINE (scm_localtime, "localtime", 1, 1, 0,
} }
#undef FUNC_NAME #undef FUNC_NAME
/* tm_zone is normally a pointer, not an array within struct tm, so we might
have to worry about the lifespan of what it points to. The posix specs
don't seem to say anything about this, let's assume here that tm_zone
will be a constant and therefore no protection or anything is needed
until we copy it in filltime(). */
SCM_DEFINE (scm_gmtime, "gmtime", 1, 0, 0, SCM_DEFINE (scm_gmtime, "gmtime", 1, 0, 0,
(SCM time), (SCM time),
"Return an object representing the broken down components of\n" "Return an object representing the broken down components of\n"
@ -405,26 +411,33 @@ SCM_DEFINE (scm_gmtime, "gmtime", 1, 0, 0,
#define FUNC_NAME s_scm_gmtime #define FUNC_NAME s_scm_gmtime
{ {
timet itime; timet itime;
struct tm *bd_time; struct tm bd_buf, *bd_time;
SCM result;
const char *zname; const char *zname;
itime = SCM_NUM2LONG (1, time); itime = SCM_NUM2LONG (1, time);
SCM_DEFER_INTS;
/* POSIX says gmtime sets errno, but C99 doesn't say that. /* POSIX says gmtime sets errno, but C99 doesn't say that.
Give a sensible default value in case gmtime doesn't set it. */ Give a sensible default value in case gmtime doesn't set it. */
errno = EINVAL; errno = EINVAL;
#if HAVE_GMTIME_R
bd_time = gmtime_r (&itime, &bd_buf);
#else
SCM_DEFER_INTS;
bd_time = gmtime (&itime); bd_time = gmtime (&itime);
if (bd_time != NULL)
bd_buf = *bd_time;
SCM_ALLOW_INTS;
#endif
if (bd_time == NULL) if (bd_time == NULL)
SCM_SYSERROR; SCM_SYSERROR;
#if HAVE_STRUCT_TM_TM_ZONE #if HAVE_STRUCT_TM_TM_ZONE
zname = bd_time->tm_zone; zname = bd_buf.tm_zone;
#else #else
zname = "GMT"; zname = "GMT";
#endif #endif
result = filltime (bd_time, 0, zname); return filltime (&bd_buf, 0, zname);
SCM_ALLOW_INTS;
return result;
} }
#undef FUNC_NAME #undef FUNC_NAME