ISO_TIMESTAMP_COMMA_GT = ISO_TIMESTAMP_COMMA_G | ISO_T
};
+#define CTIME_BUFSIZ 26
#define ISO_BUFSIZ 42
int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz);
*/
static char *showdate(void)
{
- char *s = ctime(&lastdate);
+ static char s[CTIME_BUFSIZ];
+
+ ctime_r(&lastdate, s);
s[16] = 0;
return s;
}
break;
case LAST_TIMEFTM_HHMM:
{
- struct tm *tm = localtime(when);
- if (!snprintf(dst, dlen, "%02d:%02d", tm->tm_hour, tm->tm_min))
+ struct tm tm;
+
+ localtime_r(when, &tm);
+ if (!snprintf(dst, dlen, "%02d:%02d", tm.tm_hour, tm.tm_min))
ret = -1;
break;
}
case LAST_TIMEFTM_CTIME:
- snprintf(dst, dlen, "%s", ctime(when));
+ {
+ char buf[CTIME_BUFSIZ];
+
+ ctime_r(when, buf);
+ snprintf(dst, dlen, "%s", buf);
ret = rtrim_whitespace((unsigned char *) dst);
break;
+ }
case LAST_TIMEFTM_ISO8601:
ret = strtime_iso(when, ISO_TIMESTAMP_T, dst, dlen);
break;
#include "xalloc.h"
#include "all-io.h"
#include "fileutils.h"
+#include "timeutils.h"
#include "ttyutils.h"
#include "pwdutils.h"
if (!cxt->quiet) {
if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
ll.ll_time != 0) {
+ char time_string[CTIME_BUFSIZ];
+
time_t ll_time = (time_t) ll.ll_time;
- printf(_("Last login: %.*s "), 24 - 5, ctime(&ll_time));
+ ctime_r(&ll_time, time_string);
+ printf(_("Last login: %.*s "), 24 - 5, time_string);
if (*ll.ll_host != '\0')
printf(_("from %.*s\n"),
(int)sizeof(ll.ll_host), ll.ll_host);
int main(int argc, char **argv)
{
- struct tm *local_time;
+ struct tm local_time;
char *term;
time_t now;
int ch = 0, yflag = 0, Yflag = 0;
} else
cal_time(&now);
- local_time = localtime(&now);
+ localtime_r(&now, &local_time);
switch(argc) {
case 3:
errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), dm);
ctl.req.day = day_in_year(&ctl, ctl.req.day,
ctl.req.month, ctl.req.year);
- } else if ((int32_t) (local_time->tm_year + 1900) == ctl.req.year) {
- ctl.req.day = local_time->tm_yday + 1;
+ } else if ((int32_t) (local_time.tm_year + 1900) == ctl.req.year) {
+ ctl.req.day = local_time.tm_yday + 1;
}
if (!ctl.req.month && !ctl.req.week) {
- ctl.req.month = local_time->tm_mon + 1;
+ ctl.req.month = local_time.tm_mon + 1;
if (!ctl.num_months)
yflag = 1;
}
break;
case 0:
- ctl.req.day = local_time->tm_yday + 1;
- ctl.req.year = local_time->tm_year + 1900;
+ ctl.req.day = local_time.tm_yday + 1;
+ ctl.req.year = local_time.tm_year + 1900;
if (!ctl.req.month)
- ctl.req.month = local_time->tm_mon + 1;
+ ctl.req.month = local_time.tm_mon + 1;
break;
default:
warnx(_("bad usage"));
{
static char time[32];
struct timeval tv;
- struct tm *tm;
+ struct tm tm;
static char const * const monthnames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
};
logger_gettimeofday(&tv, NULL);
- tm = localtime(&tv.tv_sec);
+ localtime_r(&tv.tv_sec, &tm);
snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d",
- monthnames[tm->tm_mon], tm->tm_mday,
- tm->tm_hour, tm->tm_min, tm->tm_sec);
+ monthnames[tm.tm_mon], tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec);
return time;
}
#include "c.h"
#include "nls.h"
#include "closestream.h"
+#include "timeutils.h"
#include "ipcutils.h"
static void do_msg (char format, int unit);
static void print_msg (int id, int unit);
-/* we read time as int64_t from /proc, so cast... */
-#define xctime(_x) ctime((time_t *) (_x))
+static inline char *ctime64(int64_t *t)
+{
+ static char buf[CTIME_BUFSIZ];
+
+ /* we read time as int64_t from /proc, so cast... */
+ ctime_r((time_t *)t, buf);
+ return buf;
+}
static void __attribute__((__noreturn__)) usage(void)
{
printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid);
/* ctime uses static buffer: use separate calls */
printf(" %-20.16s", shmdsp->shm_atim
- ? xctime(&shmdsp->shm_atim) + 4 : _("Not set"));
+ ? ctime64(&shmdsp->shm_atim) + 4 : _("Not set"));
printf(" %-20.16s", shmdsp->shm_dtim
- ? xctime(&shmdsp->shm_dtim) + 4 : _("Not set"));
+ ? ctime64(&shmdsp->shm_dtim) + 4 : _("Not set"));
printf(" %-20.16s\n", shmdsp->shm_ctim
- ? xctime(&shmdsp->shm_ctim) + 4 : _("Not set"));
+ ? ctime64(&shmdsp->shm_ctim) + 4 : _("Not set"));
break;
case PID:
if (pw)
else
printf ("%-8d %-10u", semdsp->sem_perm.id, semdsp->sem_perm.uid);
printf (" %-26.24s", semdsp->sem_otime
- ? xctime(&semdsp->sem_otime) : _("Not set"));
+ ? ctime64(&semdsp->sem_otime) : _("Not set"));
printf (" %-26.24s\n", semdsp->sem_ctime
- ? xctime( &semdsp->sem_ctime) : _("Not set"));
+ ? ctime64( &semdsp->sem_ctime) : _("Not set"));
break;
case PID:
break;
else
printf ("%-8d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid);
printf (" %-20.16s", msgdsp->q_stime
- ? xctime(&msgdsp->q_stime) + 4 : _("Not set"));
+ ? ctime64(&msgdsp->q_stime) + 4 : _("Not set"));
printf (" %-20.16s", msgdsp->q_rtime
- ? xctime(&msgdsp->q_rtime) + 4 : _("Not set"));
+ ? ctime64(&msgdsp->q_rtime) + 4 : _("Not set"));
printf (" %-20.16s\n", msgdsp->q_ctime
- ? xctime(&msgdsp->q_ctime) + 4 : _("Not set"));
+ ? ctime64(&msgdsp->q_ctime) + 4 : _("Not set"));
break;
case PID:
if (pw)
shmdata->shm_lprid, shmdata->shm_cprid,
shmdata->shm_nattch);
printf(_("att_time=%-26.24s\n"),
- shmdata->shm_atim ? xctime(&(shmdata->shm_atim)) : _("Not set"));
+ shmdata->shm_atim ? ctime64(&(shmdata->shm_atim)) : _("Not set"));
printf(_("det_time=%-26.24s\n"),
- shmdata->shm_dtim ? xctime(&shmdata->shm_dtim) : _("Not set"));
- printf(_("change_time=%-26.24s\n"), xctime(&shmdata->shm_ctim));
+ shmdata->shm_dtim ? ctime64(&shmdata->shm_dtim) : _("Not set"));
+ printf(_("change_time=%-26.24s\n"), ctime64(&shmdata->shm_ctim));
printf("\n");
ipc_shm_free_info(shmdata);
msgdata->q_qnum,
msgdata->q_lspid, msgdata->q_lrpid);
printf(_("send_time=%-26.24s\n"),
- msgdata->q_stime ? xctime(&msgdata->q_stime) : _("Not set"));
+ msgdata->q_stime ? ctime64(&msgdata->q_stime) : _("Not set"));
printf(_("rcv_time=%-26.24s\n"),
- msgdata->q_rtime ? xctime(&msgdata->q_rtime) : _("Not set"));
+ msgdata->q_rtime ? ctime64(&msgdata->q_rtime) : _("Not set"));
printf(_("change_time=%-26.24s\n"),
- msgdata->q_ctime ? xctime(&msgdata->q_ctime) : _("Not set"));
+ msgdata->q_ctime ? ctime64(&msgdata->q_ctime) : _("Not set"));
printf("\n");
ipc_msg_free_info(msgdata);
semdata->sem_perm.mode, semdata->sem_perm.mode & 0777);
printf(_("nsems = %ju\n"), semdata->sem_nsems);
printf(_("otime = %-26.24s\n"),
- semdata->sem_otime ? xctime(&semdata->sem_otime) : _("Not set"));
- printf(_("ctime = %-26.24s\n"), xctime(&semdata->sem_ctime));
+ semdata->sem_otime ? ctime64(&semdata->sem_otime) : _("Not set"));
+ printf(_("ctime = %-26.24s\n"), ctime64(&semdata->sem_ctime));
printf("%-10s %-10s %-10s %-10s %-10s\n",
_("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
/* Unless the system uses UTC, either delta or tzone
* reflects a seconds offset from UTC. The value can
* help sort out problems like bugs in your C library. */
+ char s[64];
printf("\tdelta = %ld\n", ctl->sys_time - ctl->rtc_time);
printf("\ttzone = %ld\n", timezone);
printf("\ttzname = %s\n", tzname[daylight]);
gmtime_r(&ctl->rtc_time, &tm);
printf("\tsystime = %ld, (UTC) %s",
- (long) ctl->sys_time, asctime(gmtime(&ctl->sys_time)));
+ (long) ctl->sys_time, asctime_r(gmtime(&ctl->sys_time), s));
printf("\trtctime = %ld, (UTC) %s",
- (long) ctl->rtc_time, asctime(&tm));
+ (long) ctl->rtc_time, asctime_r(&tm, s));
}
return 0;
}
static int setup_alarm(struct rtcwake_control *ctl, int fd, time_t *wakeup)
{
- struct tm *tm;
+ struct tm tm;
struct rtc_wkalrm wake = { 0 };
/* The wakeup time is in POSIX time (more or less UTC). Ideally
*
* Else clock_mode == CM_LOCAL so the time given to the RTC will
* instead use the local time zone. */
- tm = localtime(wakeup);
- wake.time.tm_sec = tm->tm_sec;
- wake.time.tm_min = tm->tm_min;
- wake.time.tm_hour = tm->tm_hour;
- wake.time.tm_mday = tm->tm_mday;
- wake.time.tm_mon = tm->tm_mon;
- wake.time.tm_year = tm->tm_year;
+ localtime_r(wakeup, &tm);
+ wake.time.tm_sec = tm.tm_sec;
+ wake.time.tm_min = tm.tm_min;
+ wake.time.tm_hour = tm.tm_hour;
+ wake.time.tm_mday = tm.tm_mday;
+ wake.time.tm_mon = tm.tm_mon;
+ wake.time.tm_year = tm.tm_year;
/* wday, yday, and isdst fields are unused */
wake.time.tm_wday = -1;
wake.time.tm_yday = -1;
struct rtc_wkalrm wake;
struct tm tm = { 0 };
time_t alarm;
+ char s[CTIME_BUFSIZ];
if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) {
warn(_("read rtc alarm failed"));
}
/* 0 if both UTC, or expresses diff if RTC in local time */
alarm += ctl->sys_time - ctl->rtc_time;
- printf(_("alarm: on %s"), ctime(&alarm));
+ ctime_r(&alarm, s);
+ printf(_("alarm: on %s"), s);
return 0;
}
if (suspend != DISABLE_MODE && suspend != SHOW_MODE) {
/* perform alarm setup when the show or disable modes are not set */
if (alarm) {
- if (alarm < ctl.sys_time)
- errx(EXIT_FAILURE, _("time doesn't go backward to %s"),
- ctime(&alarm));
+ if (alarm < ctl.sys_time) {
+ char s[CTIME_BUFSIZ];
+
+ ctime_r(&alarm, s);
+ errx(EXIT_FAILURE, _("time doesn't go backward to %s"), s);
+ }
alarm -= ctl.sys_time - ctl.rtc_time;
} else
alarm = ctl.rtc_time + seconds + 1;
if (setup_alarm(&ctl, fd, &alarm) < 0)
exit(EXIT_FAILURE);
- if (suspend == NO_MODE || suspend == ON_MODE)
+ if (suspend == NO_MODE || suspend == ON_MODE) {
+ char s[CTIME_BUFSIZ];
+
+ ctime_r(&alarm, s);
printf(_("%s: wakeup using %s at %s"),
- program_invocation_short_name, devname,
- ctime(&alarm));
- else
+ program_invocation_short_name, devname, s);
+ } else {
+ char s[CTIME_BUFSIZ];
+
+ ctime_r(&alarm, s);
printf(_("%s: wakeup from \"%s\" using %s at %s"),
- program_invocation_short_name, ctl.mode_str, devname,
- ctime(&alarm));
+ program_invocation_short_name, ctl.mode_str, devname, s);
+ }
fflush(stdout);
xusleep(10 * 1000);
}
case 't':
{
time_t now;
- struct tm *tm;
+ struct tm tm;
time(&now);
- tm = localtime(&now);
-
- if (!tm)
- break;
+ localtime_r(&now, &tm);
if (c == 'd') /* ISO 8601 */
fprintf(ie->output, "%s %s %d %d",
- nl_langinfo(ABDAY_1 + tm->tm_wday),
- nl_langinfo(ABMON_1 + tm->tm_mon),
- tm->tm_mday,
- tm->tm_year < 70 ? tm->tm_year + 2000 :
- tm->tm_year + 1900);
+ nl_langinfo(ABDAY_1 + tm.tm_wday),
+ nl_langinfo(ABMON_1 + tm.tm_mon),
+ tm.tm_mday,
+ tm.tm_year < 70 ? tm.tm_year + 2000 :
+ tm.tm_year + 1900);
else
fprintf(ie->output, "%02d:%02d:%02d",
- tm->tm_hour, tm->tm_min, tm->tm_sec);
+ tm.tm_hour, tm.tm_min, tm.tm_sec);
break;
}
case 'l':
#include "cctype.h"
#include "fileutils.h"
#include "closestream.h"
+#include "timeutils.h"
#define TERM_WIDTH 79
#define WRITE_TIME_OUT 300 /* in seconds */
if (print_banner == TRUE) {
char *hostname = xgethostname();
- char *whom, *where, *date;
+ char *whom, *where, date[CTIME_BUFSIZ];
struct passwd *pw;
time_t now;
where += 5;
time(&now);
- date = xstrdup(ctime(&now));
+ ctime_r(&now, date);
date[strlen(date) - 1] = '\0';
/*
whom, hostname, where, date);
buf_printf(bs, "%-*.*s\007\007\r\n", TERM_WIDTH, TERM_WIDTH, lbuf);
free(hostname);
- free(date);
}
buf_printf(bs, "%*s\r\n", TERM_WIDTH, " ");