Fix segfault in mail folder (SF: 3007493)
[monky] / src / timeinfo.c
index 1cb7628..0513e9b 100644 (file)
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
 #include <errno.h>
 #include <logging.h>
 
+char print_times_in_seconds = 0;
+
 struct tztime_s {
        char *tz;       /* timezone variable */
        char *fmt;      /* time display formatting */
 };
 
+void set_times_in_seconds(char val)
+{
+       print_times_in_seconds = val;
+}
+
+char times_in_seconds(void)
+{
+       return print_times_in_seconds;
+}
+
 void scan_time(struct text_object *obj, const char *arg)
 {
        obj->data.opaque = strndup(arg ? arg : "%F %T", text_buffer_size);
@@ -150,6 +162,11 @@ void print_format_time(struct text_object *obj, char *p, unsigned int p_max_size
        int minutes, hours, days, weeks;
        char show_minutes = 0, show_hours = 0, show_days = 0, show_weeks = 0, hidestring;
 
+       if (!times_in_seconds()) {
+               NORM_ERR("Enable \"times_in_seconds\" to use $format_time");
+               return;
+       }
+
        errno = 0;
        seconds = strtod(obj->data.s, &currentchar);
        if(errno == 0 && obj->data.s != currentchar) {
@@ -218,11 +235,14 @@ void print_format_time(struct text_object *obj, char *p, unsigned int p_max_size
                                                        if(*currentchar >= '0' && *currentchar <= '9') {
                                                                asprintf(&temp, "%.*f", (*currentchar) - '0', seconds);
                                                        } else if(*currentchar == 'x') {
-                                                               asprintf(&temp, "%.9f", seconds);
-                                                               while(*(temp + strlen(temp) - 1) == '0' || *(temp + strlen(temp) - 1) == '.') {
-                                                                       *(temp + strlen(temp) - 1) = 0;
+                                                               if(seconds == (int) seconds ) {
+                                                                       asprintf(&temp, "%d", (int) seconds);
+                                                               } else {
+                                                                       asprintf(&temp, "%.9f", seconds);
+                                                                       while(*(temp + strlen(temp) - 1) == '0' || *(temp + strlen(temp) - 1) == '.') {
+                                                                               *(temp + strlen(temp) - 1) = 0;
+                                                                       }
                                                                }
-                                                               if(*temp == 0) *temp = '0';
                                                        }else{
                                                                currentchar--;
                                                                NORM_ERR("$format_time needs a digit behind 'S' to specify precision")