Replace localtime() with localtime_r()
Since the result of localtime() is stored in a statically allocated structure, data was overwritten when a context switch occurred during (or shortly after) the execution of localtime(), potentially resulting in critical data corruption. BUG#7 and BUG#8 are likely related. This patch converts all usages of localtime() with localtime_r(), which is thread-safe. Reported-by: Baptiste Jonglez <baptiste@jonglez.org> Reported-by: Erik Saule <esaule@bmi.osu.edu> Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
parent
6b6067a53b
commit
e269f09438
29
src/apoint.c
29
src/apoint.c
@ -337,39 +337,39 @@ unsigned apoint_inday(struct apoint *i, long start)
|
|||||||
|
|
||||||
void apoint_sec2str(struct apoint *o, long day, char *start, char *end)
|
void apoint_sec2str(struct apoint *o, long day, char *start, char *end)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
if (o->start < day)
|
if (o->start < day)
|
||||||
strncpy(start, "..:..", 6);
|
strncpy(start, "..:..", 6);
|
||||||
else {
|
else {
|
||||||
t = o->start;
|
t = o->start;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
snprintf(start, HRMIN_SIZE, "%02u:%02u", lt->tm_hour, lt->tm_min);
|
snprintf(start, HRMIN_SIZE, "%02u:%02u", lt.tm_hour, lt.tm_min);
|
||||||
}
|
}
|
||||||
if (o->start + o->dur > day + DAYINSEC)
|
if (o->start + o->dur > day + DAYINSEC)
|
||||||
strncpy(end, "..:..", 6);
|
strncpy(end, "..:..", 6);
|
||||||
else {
|
else {
|
||||||
t = o->start + o->dur;
|
t = o->start + o->dur;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
snprintf(end, HRMIN_SIZE, "%02u:%02u", lt->tm_hour, lt->tm_min);
|
snprintf(end, HRMIN_SIZE, "%02u:%02u", lt.tm_hour, lt.tm_min);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void apoint_write(struct apoint *o, FILE * f)
|
void apoint_write(struct apoint *o, FILE * f)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
t = o->start;
|
t = o->start;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
fprintf(f, "%02u/%02u/%04u @ %02u:%02u", lt->tm_mon + 1, lt->tm_mday,
|
fprintf(f, "%02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt->tm_year, lt->tm_hour, lt->tm_min);
|
1900 + lt.tm_year, lt.tm_hour, lt.tm_min);
|
||||||
|
|
||||||
t = o->start + o->dur;
|
t = o->start + o->dur;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
fprintf(f, " -> %02u/%02u/%04u @ %02u:%02u ", lt->tm_mon + 1, lt->tm_mday,
|
fprintf(f, " -> %02u/%02u/%04u @ %02u:%02u ", lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt->tm_year, lt->tm_hour, lt->tm_min);
|
1900 + lt.tm_year, lt.tm_hour, lt.tm_min);
|
||||||
|
|
||||||
if (o->note != NULL)
|
if (o->note != NULL)
|
||||||
fprintf(f, ">%s ", o->note);
|
fprintf(f, ">%s ", o->note);
|
||||||
@ -386,10 +386,7 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end, char state,
|
|||||||
char *note)
|
char *note)
|
||||||
{
|
{
|
||||||
char buf[BUFSIZ], *newline;
|
char buf[BUFSIZ], *newline;
|
||||||
time_t tstart, tend, t;
|
time_t tstart, tend;
|
||||||
|
|
||||||
t = time(NULL);
|
|
||||||
localtime(&t);
|
|
||||||
|
|
||||||
/* Read the appointment description */
|
/* Read the appointment description */
|
||||||
if (!fgets(buf, sizeof buf, f))
|
if (!fgets(buf, sizeof buf, f))
|
||||||
|
10
src/args.c
10
src/args.c
@ -271,11 +271,11 @@ static void arg_print_date(long date)
|
|||||||
{
|
{
|
||||||
char date_str[BUFSIZ];
|
char date_str[BUFSIZ];
|
||||||
time_t t;
|
time_t t;
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
|
|
||||||
t = date;
|
t = date;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
strftime(date_str, BUFSIZ, conf.output_datefmt, lt);
|
strftime(date_str, BUFSIZ, conf.output_datefmt, <);
|
||||||
fputs(date_str, stdout);
|
fputs(date_str, stdout);
|
||||||
fputs(":\n", stdout);
|
fputs(":\n", stdout);
|
||||||
}
|
}
|
||||||
@ -469,7 +469,7 @@ date_arg(const char *ddate, int add_line, const char *fmt_apt,
|
|||||||
* to format the output correctly.
|
* to format the output correctly.
|
||||||
*/
|
*/
|
||||||
timer = time(NULL);
|
timer = time(NULL);
|
||||||
t = *localtime(&timer);
|
localtime_r(&timer, &t);
|
||||||
display_app(&t, numdays, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev,
|
display_app(&t, numdays, add_line, fmt_apt, fmt_rapt, fmt_ev, fmt_rev,
|
||||||
regex);
|
regex);
|
||||||
} else { /* a date was entered */
|
} else { /* a date was entered */
|
||||||
@ -515,7 +515,7 @@ date_arg_extended(const char *startday, const char *range, int add_line,
|
|||||||
numdays = atoi(range);
|
numdays = atoi(range);
|
||||||
}
|
}
|
||||||
timer = time(NULL);
|
timer = time(NULL);
|
||||||
t = *localtime(&timer);
|
localtime_r(&timer, &t);
|
||||||
if (startday != NULL) {
|
if (startday != NULL) {
|
||||||
if (parse_date(startday, conf.input_datefmt, (int *)&t.tm_year,
|
if (parse_date(startday, conf.input_datefmt, (int *)&t.tm_year,
|
||||||
(int *)&t.tm_mon, (int *)&t.tm_mday, NULL)) {
|
(int *)&t.tm_mon, (int *)&t.tm_mday, NULL)) {
|
||||||
|
@ -140,15 +140,15 @@ void calendar_stop_date_thread(void)
|
|||||||
void calendar_set_current_date(void)
|
void calendar_set_current_date(void)
|
||||||
{
|
{
|
||||||
time_t timer;
|
time_t timer;
|
||||||
struct tm *tm;
|
struct tm tm;
|
||||||
|
|
||||||
timer = time(NULL);
|
timer = time(NULL);
|
||||||
tm = localtime(&timer);
|
localtime_r(&timer, &tm);
|
||||||
|
|
||||||
pthread_mutex_lock(&date_thread_mutex);
|
pthread_mutex_lock(&date_thread_mutex);
|
||||||
today.dd = tm->tm_mday;
|
today.dd = tm.tm_mday;
|
||||||
today.mm = tm->tm_mon + 1;
|
today.mm = tm.tm_mon + 1;
|
||||||
today.yyyy = tm->tm_year + 1900;
|
today.yyyy = tm.tm_year + 1900;
|
||||||
pthread_mutex_unlock(&date_thread_mutex);
|
pthread_mutex_unlock(&date_thread_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -684,16 +684,16 @@ void calendar_move(enum move move, int count)
|
|||||||
long calendar_start_of_year(void)
|
long calendar_start_of_year(void)
|
||||||
{
|
{
|
||||||
time_t timer;
|
time_t timer;
|
||||||
struct tm *tm;
|
struct tm tm;
|
||||||
|
|
||||||
timer = time(NULL);
|
timer = time(NULL);
|
||||||
tm = localtime(&timer);
|
localtime_r(&timer, &tm);
|
||||||
tm->tm_mon = 0;
|
tm.tm_mon = 0;
|
||||||
tm->tm_mday = 1;
|
tm.tm_mday = 1;
|
||||||
tm->tm_hour = 0;
|
tm.tm_hour = 0;
|
||||||
tm->tm_min = 0;
|
tm.tm_min = 0;
|
||||||
tm->tm_sec = 0;
|
tm.tm_sec = 0;
|
||||||
timer = mktime(tm);
|
timer = mktime(&tm);
|
||||||
|
|
||||||
return (long)timer;
|
return (long)timer;
|
||||||
}
|
}
|
||||||
@ -701,17 +701,17 @@ long calendar_start_of_year(void)
|
|||||||
long calendar_end_of_year(void)
|
long calendar_end_of_year(void)
|
||||||
{
|
{
|
||||||
time_t timer;
|
time_t timer;
|
||||||
struct tm *tm;
|
struct tm tm;
|
||||||
|
|
||||||
timer = time(NULL);
|
timer = time(NULL);
|
||||||
tm = localtime(&timer);
|
localtime_r(&timer, &tm);
|
||||||
tm->tm_mon = 0;
|
tm.tm_mon = 0;
|
||||||
tm->tm_mday = 1;
|
tm.tm_mday = 1;
|
||||||
tm->tm_hour = 0;
|
tm.tm_hour = 0;
|
||||||
tm->tm_min = 0;
|
tm.tm_min = 0;
|
||||||
tm->tm_sec = 0;
|
tm.tm_sec = 0;
|
||||||
tm->tm_year++;
|
tm.tm_year++;
|
||||||
timer = mktime(tm);
|
timer = mktime(&tm);
|
||||||
|
|
||||||
return (long)(timer - 1);
|
return (long)(timer - 1);
|
||||||
}
|
}
|
||||||
|
@ -730,7 +730,7 @@ static void update_rept(struct rpt **rpt, const long start)
|
|||||||
newuntil = 0;
|
newuntil = 0;
|
||||||
date_entered = 1;
|
date_entered = 1;
|
||||||
} else {
|
} else {
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
struct date new_date;
|
struct date new_date;
|
||||||
int newmonth, newday, newyear;
|
int newmonth, newday, newyear;
|
||||||
@ -738,11 +738,11 @@ static void update_rept(struct rpt **rpt, const long start)
|
|||||||
if (parse_date(timstr, conf.input_datefmt, &newyear, &newmonth,
|
if (parse_date(timstr, conf.input_datefmt, &newyear, &newmonth,
|
||||||
&newday, calendar_get_slctd_day())) {
|
&newday, calendar_get_slctd_day())) {
|
||||||
t = start;
|
t = start;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
new_date.dd = newday;
|
new_date.dd = newday;
|
||||||
new_date.mm = newmonth;
|
new_date.mm = newmonth;
|
||||||
new_date.yyyy = newyear;
|
new_date.yyyy = newyear;
|
||||||
newuntil = date2sec(new_date, lt->tm_hour, lt->tm_min);
|
newuntil = date2sec(new_date, lt.tm_hour, lt.tm_min);
|
||||||
if (newuntil < start) {
|
if (newuntil < start) {
|
||||||
status_mesg(msg_wrong_time, msg_enter);
|
status_mesg(msg_wrong_time, msg_enter);
|
||||||
wgetch(win[STA].p);
|
wgetch(win[STA].p);
|
||||||
|
13
src/event.c
13
src/event.c
@ -112,13 +112,13 @@ unsigned event_inday(struct event *i, long start)
|
|||||||
/* Write to file the event in user-friendly format */
|
/* Write to file the event in user-friendly format */
|
||||||
void event_write(struct event *o, FILE * f)
|
void event_write(struct event *o, FILE * f)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
t = o->day;
|
t = o->day;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
fprintf(f, "%02u/%02u/%04u [%d] ", lt->tm_mon + 1, lt->tm_mday,
|
fprintf(f, "%02u/%02u/%04u [%d] ", lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt->tm_year, o->id);
|
1900 + lt.tm_year, o->id);
|
||||||
if (o->note != NULL)
|
if (o->note != NULL)
|
||||||
fprintf(f, ">%s ", o->note);
|
fprintf(f, ">%s ", o->note);
|
||||||
fprintf(f, "%s\n", o->mesg);
|
fprintf(f, "%s\n", o->mesg);
|
||||||
@ -128,10 +128,7 @@ void event_write(struct event *o, FILE * f)
|
|||||||
struct event *event_scan(FILE * f, struct tm start, int id, char *note)
|
struct event *event_scan(FILE * f, struct tm start, int id, char *note)
|
||||||
{
|
{
|
||||||
char buf[BUFSIZ], *nl;
|
char buf[BUFSIZ], *nl;
|
||||||
time_t tstart, t;
|
time_t tstart;
|
||||||
|
|
||||||
t = time(NULL);
|
|
||||||
localtime(&t);
|
|
||||||
|
|
||||||
/* Read the event description */
|
/* Read the event description */
|
||||||
if (!fgets(buf, sizeof buf, f))
|
if (!fgets(buf, sizeof buf, f))
|
||||||
|
6
src/io.c
6
src/io.c
@ -454,7 +454,7 @@ void io_load_app(void)
|
|||||||
{
|
{
|
||||||
FILE *data_file;
|
FILE *data_file;
|
||||||
int c, is_appointment, is_event, is_recursive;
|
int c, is_appointment, is_event, is_recursive;
|
||||||
struct tm start, end, until, *lt;
|
struct tm start, end, until, lt;
|
||||||
llist_t exc;
|
llist_t exc;
|
||||||
time_t t;
|
time_t t;
|
||||||
int id = 0;
|
int id = 0;
|
||||||
@ -463,8 +463,8 @@ void io_load_app(void)
|
|||||||
char note[MAX_NOTESIZ + 1], *notep;
|
char note[MAX_NOTESIZ + 1], *notep;
|
||||||
|
|
||||||
t = time(NULL);
|
t = time(NULL);
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
start = end = until = *lt;
|
start = end = until = lt;
|
||||||
|
|
||||||
data_file = fopen(path_apts, "r");
|
data_file = fopen(path_apts, "r");
|
||||||
EXIT_IF(data_file == NULL, _("failed to open appointment file"));
|
EXIT_IF(data_file == NULL, _("failed to open appointment file"));
|
||||||
|
@ -310,18 +310,18 @@ static void *notify_main_thread(void *arg)
|
|||||||
const unsigned check_app = MININSEC;
|
const unsigned check_app = MININSEC;
|
||||||
int elapse = 0;
|
int elapse = 0;
|
||||||
int got_app;
|
int got_app;
|
||||||
struct tm *ntime;
|
struct tm ntime;
|
||||||
time_t ntimer;
|
time_t ntimer;
|
||||||
|
|
||||||
elapse = 0;
|
elapse = 0;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ntimer = time(NULL);
|
ntimer = time(NULL);
|
||||||
ntime = localtime(&ntimer);
|
localtime_r(&ntimer, &ntime);
|
||||||
pthread_mutex_lock(¬ify.mutex);
|
pthread_mutex_lock(¬ify.mutex);
|
||||||
pthread_mutex_lock(&nbar.mutex);
|
pthread_mutex_lock(&nbar.mutex);
|
||||||
strftime(notify.time, NOTIFY_FIELD_LENGTH, nbar.timefmt, ntime);
|
strftime(notify.time, NOTIFY_FIELD_LENGTH, nbar.timefmt, &ntime);
|
||||||
strftime(notify.date, NOTIFY_FIELD_LENGTH, nbar.datefmt, ntime);
|
strftime(notify.date, NOTIFY_FIELD_LENGTH, nbar.datefmt, &ntime);
|
||||||
pthread_mutex_unlock(&nbar.mutex);
|
pthread_mutex_unlock(&nbar.mutex);
|
||||||
pthread_mutex_unlock(¬ify.mutex);
|
pthread_mutex_unlock(¬ify.mutex);
|
||||||
notify_update_bar();
|
notify_update_bar();
|
||||||
|
@ -64,7 +64,7 @@ foreach_date_dump(const long date_end, struct rpt *rpt, llist_t * exc,
|
|||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
t = item_first_date;
|
t = item_first_date;
|
||||||
lt = *localtime(&t);
|
localtime_r(&t, <);
|
||||||
lt.tm_hour = lt.tm_min = lt.tm_sec = 0;
|
lt.tm_hour = lt.tm_min = lt.tm_sec = 0;
|
||||||
lt.tm_isdst = -1;
|
lt.tm_isdst = -1;
|
||||||
date = mktime(<);
|
date = mktime(<);
|
||||||
|
60
src/recur.c
60
src/recur.c
@ -318,17 +318,17 @@ int recur_char2def(char type)
|
|||||||
static void recur_write_exc(llist_t * lexc, FILE * f)
|
static void recur_write_exc(llist_t * lexc, FILE * f)
|
||||||
{
|
{
|
||||||
llist_item_t *i;
|
llist_item_t *i;
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
int st_mon, st_day, st_year;
|
int st_mon, st_day, st_year;
|
||||||
|
|
||||||
LLIST_FOREACH(lexc, i) {
|
LLIST_FOREACH(lexc, i) {
|
||||||
struct excp *exc = LLIST_GET_DATA(i);
|
struct excp *exc = LLIST_GET_DATA(i);
|
||||||
t = exc->st;
|
t = exc->st;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
st_mon = lt->tm_mon + 1;
|
st_mon = lt.tm_mon + 1;
|
||||||
st_day = lt->tm_mday;
|
st_day = lt.tm_mday;
|
||||||
st_year = lt->tm_year + 1900;
|
st_year = lt.tm_year + 1900;
|
||||||
fprintf(f, " !%02u/%02u/%04u", st_mon, st_day, st_year);
|
fprintf(f, " !%02u/%02u/%04u", st_mon, st_day, st_year);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -415,27 +415,27 @@ struct recur_event *recur_event_scan(FILE * f, struct tm start, int id,
|
|||||||
/* Writting of a recursive appointment into file. */
|
/* Writting of a recursive appointment into file. */
|
||||||
void recur_apoint_write(struct recur_apoint *o, FILE * f)
|
void recur_apoint_write(struct recur_apoint *o, FILE * f)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
t = o->start;
|
t = o->start;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
fprintf(f, "%02u/%02u/%04u @ %02u:%02u", lt->tm_mon + 1, lt->tm_mday,
|
fprintf(f, "%02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt->tm_year, lt->tm_hour, lt->tm_min);
|
1900 + lt.tm_year, lt.tm_hour, lt.tm_min);
|
||||||
|
|
||||||
t = o->start + o->dur;
|
t = o->start + o->dur;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
fprintf(f, " -> %02u/%02u/%04u @ %02u:%02u", lt->tm_mon + 1, lt->tm_mday,
|
fprintf(f, " -> %02u/%02u/%04u @ %02u:%02u", lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt->tm_year, lt->tm_hour, lt->tm_min);
|
1900 + lt.tm_year, lt.tm_hour, lt.tm_min);
|
||||||
|
|
||||||
t = o->rpt->until;
|
t = o->rpt->until;
|
||||||
if (t == 0) { /* We have an endless recurrent appointment. */
|
if (t == 0) { /* We have an endless recurrent appointment. */
|
||||||
fprintf(f, " {%d%c", o->rpt->freq, recur_def2char(o->rpt->type));
|
fprintf(f, " {%d%c", o->rpt->freq, recur_def2char(o->rpt->type));
|
||||||
} else {
|
} else {
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
fprintf(f, " {%d%c -> %02u/%02u/%04u", o->rpt->freq,
|
fprintf(f, " {%d%c -> %02u/%02u/%04u", o->rpt->freq,
|
||||||
recur_def2char(o->rpt->type), lt->tm_mon + 1, lt->tm_mday,
|
recur_def2char(o->rpt->type), lt.tm_mon + 1, lt.tm_mday,
|
||||||
1900 + lt->tm_year);
|
1900 + lt.tm_year);
|
||||||
}
|
}
|
||||||
recur_write_exc(&o->exc, f);
|
recur_write_exc(&o->exc, f);
|
||||||
fputs("} ", f);
|
fputs("} ", f);
|
||||||
@ -451,25 +451,25 @@ void recur_apoint_write(struct recur_apoint *o, FILE * f)
|
|||||||
/* Writting of a recursive event into file. */
|
/* Writting of a recursive event into file. */
|
||||||
void recur_event_write(struct recur_event *o, FILE * f)
|
void recur_event_write(struct recur_event *o, FILE * f)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
int st_mon, st_day, st_year;
|
int st_mon, st_day, st_year;
|
||||||
int end_mon, end_day, end_year;
|
int end_mon, end_day, end_year;
|
||||||
|
|
||||||
t = o->day;
|
t = o->day;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
st_mon = lt->tm_mon + 1;
|
st_mon = lt.tm_mon + 1;
|
||||||
st_day = lt->tm_mday;
|
st_day = lt.tm_mday;
|
||||||
st_year = lt->tm_year + 1900;
|
st_year = lt.tm_year + 1900;
|
||||||
t = o->rpt->until;
|
t = o->rpt->until;
|
||||||
if (t == 0) { /* We have an endless recurrent event. */
|
if (t == 0) { /* We have an endless recurrent event. */
|
||||||
fprintf(f, "%02u/%02u/%04u [%d] {%d%c", st_mon, st_day, st_year, o->id,
|
fprintf(f, "%02u/%02u/%04u [%d] {%d%c", st_mon, st_day, st_year, o->id,
|
||||||
o->rpt->freq, recur_def2char(o->rpt->type));
|
o->rpt->freq, recur_def2char(o->rpt->type));
|
||||||
} else {
|
} else {
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
end_mon = lt->tm_mon + 1;
|
end_mon = lt.tm_mon + 1;
|
||||||
end_day = lt->tm_mday;
|
end_day = lt.tm_mday;
|
||||||
end_year = lt->tm_year + 1900;
|
end_year = lt.tm_year + 1900;
|
||||||
fprintf(f, "%02u/%02u/%04u [%d] {%d%c -> %02u/%02u/%04u", st_mon,
|
fprintf(f, "%02u/%02u/%04u [%d] {%d%c -> %02u/%02u/%04u", st_mon,
|
||||||
st_day, st_year, o->id, o->rpt->freq,
|
st_day, st_year, o->id, o->rpt->freq,
|
||||||
recur_def2char(o->rpt->type), end_mon, end_day, end_year);
|
recur_def2char(o->rpt->type), end_mon, end_day, end_year);
|
||||||
@ -581,10 +581,10 @@ recur_item_find_occurrence(long item_start, long item_dur, llist_t * item_exc,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
t = day_start;
|
t = day_start;
|
||||||
lt_day = *localtime(&t);
|
localtime_r(&t, <_day);
|
||||||
|
|
||||||
t = item_start;
|
t = item_start;
|
||||||
lt_item = *localtime(&t);
|
localtime_r(&t, <_item);
|
||||||
|
|
||||||
lt_item_day = lt_item;
|
lt_item_day = lt_item;
|
||||||
lt_item_day.tm_sec = lt_item_day.tm_min = lt_item_day.tm_hour = 0;
|
lt_item_day.tm_sec = lt_item_day.tm_min = lt_item_day.tm_hour = 0;
|
||||||
@ -632,7 +632,7 @@ recur_item_find_occurrence(long item_start, long item_dur, llist_t * item_exc,
|
|||||||
if (rpt_until != 0 && t > rpt_until)
|
if (rpt_until != 0 && t > rpt_until)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
lt_item_day = *localtime(&t);
|
localtime_r(&t, <_item_day);
|
||||||
diff = diff_days(lt_item_day, lt_day);
|
diff = diff_days(lt_item_day, lt_day);
|
||||||
|
|
||||||
if (diff <= span) {
|
if (diff <= span) {
|
||||||
@ -791,7 +791,7 @@ recur_apoint_erase(long start, unsigned num, unsigned delete_whole,
|
|||||||
*/
|
*/
|
||||||
void recur_repeat_item(void)
|
void recur_repeat_item(void)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
int date_entered = 0;
|
int date_entered = 0;
|
||||||
int year = 0, month = 0, day = 0;
|
int year = 0, month = 0, day = 0;
|
||||||
@ -876,11 +876,11 @@ void recur_repeat_item(void)
|
|||||||
if (parse_date(user_input, conf.input_datefmt,
|
if (parse_date(user_input, conf.input_datefmt,
|
||||||
&year, &month, &day, calendar_get_slctd_day())) {
|
&year, &month, &day, calendar_get_slctd_day())) {
|
||||||
t = p->start;
|
t = p->start;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
until_date.dd = day;
|
until_date.dd = day;
|
||||||
until_date.mm = month;
|
until_date.mm = month;
|
||||||
until_date.yyyy = year;
|
until_date.yyyy = year;
|
||||||
until = date2sec(until_date, lt->tm_hour, lt->tm_min);
|
until = date2sec(until_date, lt.tm_hour, lt.tm_min);
|
||||||
if (until < p->start) {
|
if (until < p->start) {
|
||||||
status_mesg(mesg_older, wrong_type_2);
|
status_mesg(mesg_older, wrong_type_2);
|
||||||
wgetch(win[STA].p);
|
wgetch(win[STA].p);
|
||||||
|
81
src/utils.c
81
src/utils.c
@ -337,18 +337,26 @@ long get_item_time(long date)
|
|||||||
|
|
||||||
int get_item_hour(long date)
|
int get_item_hour(long date)
|
||||||
{
|
{
|
||||||
return (localtime((time_t *) & date))->tm_hour;
|
struct tm lt;
|
||||||
|
|
||||||
|
localtime_r((time_t *)&date, <);
|
||||||
|
return lt.tm_hour;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_item_min(long date)
|
int get_item_min(long date)
|
||||||
{
|
{
|
||||||
return (localtime((time_t *) & date))->tm_min;
|
struct tm lt;
|
||||||
|
|
||||||
|
localtime_r((time_t *)&date, <);
|
||||||
|
return lt.tm_min;
|
||||||
}
|
}
|
||||||
|
|
||||||
long date2sec(struct date day, unsigned hour, unsigned min)
|
long date2sec(struct date day, unsigned hour, unsigned min)
|
||||||
{
|
{
|
||||||
time_t t = now();
|
time_t t = now();
|
||||||
struct tm start = *(localtime(&t));
|
struct tm start;
|
||||||
|
|
||||||
|
localtime_r(&t, &start);
|
||||||
|
|
||||||
start.tm_mon = day.mm - 1;
|
start.tm_mon = day.mm - 1;
|
||||||
start.tm_mday = day.dd;
|
start.tm_mday = day.dd;
|
||||||
@ -367,14 +375,14 @@ long date2sec(struct date day, unsigned hour, unsigned min)
|
|||||||
/* Return a string containing the date, given a date in seconds. */
|
/* Return a string containing the date, given a date in seconds. */
|
||||||
char *date_sec2date_str(long sec, const char *datefmt)
|
char *date_sec2date_str(long sec, const char *datefmt)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
char *datestr = (char *)mem_calloc(BUFSIZ, sizeof(char));
|
char *datestr = (char *)mem_calloc(BUFSIZ, sizeof(char));
|
||||||
|
|
||||||
if (sec == 0)
|
if (sec == 0)
|
||||||
strncpy(datestr, "0", BUFSIZ);
|
strncpy(datestr, "0", BUFSIZ);
|
||||||
else {
|
else {
|
||||||
lt = localtime((time_t *) & sec);
|
localtime_r((time_t *)&sec, <);
|
||||||
strftime(datestr, BUFSIZ, datefmt, lt);
|
strftime(datestr, BUFSIZ, datefmt, <);
|
||||||
}
|
}
|
||||||
|
|
||||||
return datestr;
|
return datestr;
|
||||||
@ -389,8 +397,9 @@ void date_sec2date_fmt(long sec, const char *fmt, char *datef)
|
|||||||
setlocale (LC_ALL, "C");
|
setlocale (LC_ALL, "C");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct tm *lt = localtime((time_t *)&sec);
|
struct tm lt;
|
||||||
strftime(datef, BUFSIZ, fmt, lt);
|
localtime_r((time_t *)&sec, <);
|
||||||
|
strftime(datef, BUFSIZ, fmt, <);
|
||||||
|
|
||||||
#if ENABLE_NLS
|
#if ENABLE_NLS
|
||||||
setlocale (LC_ALL, locale_old);
|
setlocale (LC_ALL, locale_old);
|
||||||
@ -403,15 +412,15 @@ void date_sec2date_fmt(long sec, const char *fmt, char *datef)
|
|||||||
*/
|
*/
|
||||||
long date_sec_change(long date, int delta_month, int delta_day)
|
long date_sec_change(long date, int delta_month, int delta_day)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
t = date;
|
t = date;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
lt->tm_mon += delta_month;
|
lt.tm_mon += delta_month;
|
||||||
lt->tm_mday += delta_day;
|
lt.tm_mday += delta_day;
|
||||||
lt->tm_isdst = -1;
|
lt.tm_isdst = -1;
|
||||||
t = mktime(lt);
|
t = mktime(<);
|
||||||
EXIT_IF(t == -1, _("failure in mktime"));
|
EXIT_IF(t == -1, _("failure in mktime"));
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
@ -423,14 +432,14 @@ long date_sec_change(long date, int delta_month, int delta_day)
|
|||||||
*/
|
*/
|
||||||
long update_time_in_date(long date, unsigned hr, unsigned mn)
|
long update_time_in_date(long date, unsigned hr, unsigned mn)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t t, new_date;
|
time_t t, new_date;
|
||||||
|
|
||||||
t = date;
|
t = date;
|
||||||
lt = localtime(&t);
|
localtime_r(&t, <);
|
||||||
lt->tm_hour = hr;
|
lt.tm_hour = hr;
|
||||||
lt->tm_min = mn;
|
lt.tm_min = mn;
|
||||||
new_date = mktime(lt);
|
new_date = mktime(<);
|
||||||
EXIT_IF(new_date == -1, _("error in mktime"));
|
EXIT_IF(new_date == -1, _("error in mktime"));
|
||||||
|
|
||||||
return new_date;
|
return new_date;
|
||||||
@ -442,7 +451,7 @@ long update_time_in_date(long date, unsigned hr, unsigned mn)
|
|||||||
*/
|
*/
|
||||||
long get_sec_date(struct date date)
|
long get_sec_date(struct date date)
|
||||||
{
|
{
|
||||||
struct tm *ptrtime;
|
struct tm ptrtime;
|
||||||
time_t timer;
|
time_t timer;
|
||||||
long long_date;
|
long long_date;
|
||||||
char current_day[] = "dd ";
|
char current_day[] = "dd ";
|
||||||
@ -451,10 +460,10 @@ long get_sec_date(struct date date)
|
|||||||
|
|
||||||
if (date.yyyy == 0 && date.mm == 0 && date.dd == 0) {
|
if (date.yyyy == 0 && date.mm == 0 && date.dd == 0) {
|
||||||
timer = time(NULL);
|
timer = time(NULL);
|
||||||
ptrtime = localtime(&timer);
|
localtime_r(&timer, &ptrtime);
|
||||||
strftime(current_day, strlen(current_day), "%d", ptrtime);
|
strftime(current_day, strlen(current_day), "%d", &ptrtime);
|
||||||
strftime(current_month, strlen(current_month), "%m", ptrtime);
|
strftime(current_month, strlen(current_month), "%m", &ptrtime);
|
||||||
strftime(current_year, strlen(current_year), "%Y", ptrtime);
|
strftime(current_year, strlen(current_year), "%Y", &ptrtime);
|
||||||
date.mm = atoi(current_month);
|
date.mm = atoi(current_month);
|
||||||
date.dd = atoi(current_day);
|
date.dd = atoi(current_day);
|
||||||
date.yyyy = atoi(current_year);
|
date.yyyy = atoi(current_year);
|
||||||
@ -518,16 +527,16 @@ item_in_popup(const char *saved_a_start, const char *saved_a_end,
|
|||||||
/* Returns the beginning of current day in seconds from 1900. */
|
/* Returns the beginning of current day in seconds from 1900. */
|
||||||
long get_today(void)
|
long get_today(void)
|
||||||
{
|
{
|
||||||
struct tm *lt;
|
struct tm lt;
|
||||||
time_t current_time;
|
time_t current_time;
|
||||||
long current_day;
|
long current_day;
|
||||||
struct date day;
|
struct date day;
|
||||||
|
|
||||||
current_time = time(NULL);
|
current_time = time(NULL);
|
||||||
lt = localtime(¤t_time);
|
localtime_r(¤t_time, <);
|
||||||
day.mm = lt->tm_mon + 1;
|
day.mm = lt.tm_mon + 1;
|
||||||
day.dd = lt->tm_mday;
|
day.dd = lt.tm_mday;
|
||||||
day.yyyy = lt->tm_year + 1900;
|
day.yyyy = lt.tm_year + 1900;
|
||||||
current_day = date2sec(day, 0, 0);
|
current_day = date2sec(day, 0, 0);
|
||||||
|
|
||||||
return current_day;
|
return current_day;
|
||||||
@ -541,10 +550,12 @@ long now(void)
|
|||||||
|
|
||||||
char *nowstr(void)
|
char *nowstr(void)
|
||||||
{
|
{
|
||||||
|
struct tm lt;
|
||||||
static char buf[BUFSIZ];
|
static char buf[BUFSIZ];
|
||||||
time_t t = now();
|
time_t t = now();
|
||||||
|
|
||||||
strftime(buf, sizeof buf, "%a %b %d %T %Y", localtime(&t));
|
localtime_r(&t, <);
|
||||||
|
strftime(buf, sizeof buf, "%a %b %d %T %Y", <);
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
@ -1183,15 +1194,17 @@ static void print_date(long date, long day, const char *extformat)
|
|||||||
printf("%ld", date);
|
printf("%ld", date);
|
||||||
else {
|
else {
|
||||||
time_t t = date;
|
time_t t = date;
|
||||||
struct tm *lt = localtime((time_t *) & t);
|
struct tm lt;
|
||||||
|
|
||||||
|
localtime_r((time_t *)&t, <);
|
||||||
|
|
||||||
if (extformat[0] == '\0' || !strcmp(extformat, "default")) {
|
if (extformat[0] == '\0' || !strcmp(extformat, "default")) {
|
||||||
if (date >= day && date <= day + DAYINSEC)
|
if (date >= day && date <= day + DAYINSEC)
|
||||||
strftime(buf, BUFSIZ, "%H:%M", lt);
|
strftime(buf, BUFSIZ, "%H:%M", <);
|
||||||
else
|
else
|
||||||
strftime(buf, BUFSIZ, "..:..", lt);
|
strftime(buf, BUFSIZ, "..:..", <);
|
||||||
} else {
|
} else {
|
||||||
strftime(buf, BUFSIZ, extformat, lt);
|
strftime(buf, BUFSIZ, extformat, <);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s", buf);
|
printf("%s", buf);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user