Ticket #96: fix-timefmt-rev2.patch

File fix-timefmt-rev2.patch, 4.5 KB (added by winnie, 15 years ago)

Patch rediffed to apply on latest git. (Also created a 96_segfault_invalid_mtime branch with this patch applied inside)

  • edit/edit.c

    diff --git a/edit/edit.c b/edit/edit.c
    index ff2b2e7..9beb2c8 100644
    a b  
    4444#include "../src/cmd.h"         /* view_other_cmd() */ 
    4545#include "../src/user.h"        /* user_menu_cmd() */ 
    4646#include "../src/wtools.h"      /* query_dialog() */ 
     47#include "../src/timefmt.h"     /* time formatting */ 
    4748 
    4849 
    4950/* 
    edit_execute_cmd (WEdit *edit, int command, int char_for_insertion) 
    25192520        break; 
    25202521 
    25212522    case CK_Date:{ 
    2522             time_t t; 
    2523 #ifdef HAVE_STRFTIME 
    25242523            char s[1024]; 
    25252524            /* fool gcc to prevent a Y2K warning */ 
    25262525            char time_format[] = "_c"; 
    25272526            time_format[0] = '%'; 
    2528 #endif 
    2529             time (&t); 
    2530 #ifdef HAVE_STRFTIME 
    2531             strftime (s, sizeof (s), time_format, localtime (&t)); 
     2527 
     2528            FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format); 
    25322529            edit_print_string (edit, s); 
    2533 #else 
    2534             edit_print_string (edit, ctime (&t)); 
    2535 #endif 
    25362530            edit->force |= REDRAW_PAGE; 
    25372531            break; 
    25382532        } 
  • src/Makefile.am

    diff --git a/src/Makefile.am b/src/Makefile.am
    index ccf0f8b..a4713a6 100644
    a b SRCS = achown.c achown.h background.c background.h boxes.c boxes.h \ 
    6060        popt.c poptconfig.c popt.h popthelp.c poptint.h poptparse.c     \ 
    6161        profile.c profile.h regex.c rxvt.c screen.c setup.c setup.h     \ 
    6262        slint.c subshell.c subshell.h textconf.c textconf.h             \ 
    63         tree.c tree.h treestore.c treestore.h tty.c tty.h user.c user.h \ 
    64         util.c util.h utilunix.c view.c view.h vfsdummy.h widget.c      \ 
     63        tree.c tree.h treestore.c treestore.h timefmt.h tty.c tty.h user.c      \ 
     64        user.h util.c util.h utilunix.c view.c view.h vfsdummy.h widget.c       \ 
    6565        widget.h win.c win.h wtools.c wtools.h unixcompat.h             \ 
    6666        x11conn.h x11conn.c ecs.h ecs.c 
    6767 
  • new file src/timefmt.h

    diff --git a/src/timefmt.h b/src/timefmt.h
    new file mode 100644
    index 0000000..2b8d52f
    - +  
     1#ifndef __UTIL_TIMEFMT_H 
     2#define __UTIL_TIMEFMT_H 
     3 
     4#include <sys/types.h> 
     5 
     6#define INVALID_TIME_TEXT       "(invalid)" 
     7 
     8#ifdef HAVE_STRFTIME 
     9 
     10/* safe localtime formatting - strftime()-using version */ 
     11#define FMT_LOCALTIME(buffer, bufsize, fmt, when)                       \ 
     12    {                                                                   \ 
     13        struct tm *whentm;                                              \ 
     14        whentm = localtime(&when);                                      \ 
     15        if (whentm == NULL)                                             \ 
     16        {                                                               \ 
     17            strncpy(buffer, INVALID_TIME_TEXT, bufsize);                \ 
     18            buffer[bufsize-1] = 0;                                      \ 
     19        }                                                               \ 
     20        else                                                            \ 
     21        {                                                               \ 
     22            strftime(buffer, bufsize, fmt, whentm);                     \ 
     23        }                                                               \ 
     24    }                                                                   \ 
     25 
     26#else 
     27 
     28/* fallback when strftime/localtime not available */ 
     29#define FMT_LOCALTIME(buffer,bufsize,fmt,when)                  \ 
     30    {                                                           \ 
     31        ctime_r(when,buffer);                                   \ 
     32    }                                                           \ 
     33 
     34#endif 
     35 
     36#define FMT_LOCALTIME_CURRENT(buffer, bufsize, fmt)             \ 
     37    {                                                           \ 
     38        time_t __current_time;                                  \ 
     39        time(&__current_time);                                  \ 
     40        FMT_LOCALTIME(buffer,bufsize,fmt,__current_time);       \ 
     41    } 
     42 
     43#endif                          /* !__UTIL_H */ 
  • src/util.c

    diff --git a/src/util.c b/src/util.c
    index da6d1b2..2e4eabb 100644
    a b  
    4141#include "cmd.h"                /* guess_message_value */ 
    4242#include "mountlist.h" 
    4343#include "win.h"                /* xterm_flag */ 
     44#include "timefmt.h" 
    4445 
    4546#ifdef HAVE_CHARSET 
    4647#include "charsets.h" 
    short-month-name sizes for different locales */ 
    724725size_t 
    725726i18n_checktimelength (void) 
    726727{ 
    727     size_t length, a, b; 
    728     char buf [MAX_I18NTIMELENGTH + 1]; 
    729728    time_t testtime = time (NULL); 
    730      
    731     a = strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), localtime(&testtime)); 
    732     b = strftime (buf, sizeof(buf)-1, _("%b %e  %Y"), localtime(&testtime)); 
    733      
    734     length = max (a, b); 
    735     length = max (strlen (_("(invalid)")), length); 
    736      
     729    struct tm* lt = localtime(&testtime); 
     730    size_t length; 
     731 
     732    if (lt == NULL) { 
     733            // huh, localtime() doesnt seem to work ... falling back to "(invalid)" 
     734            length = strlen(INVALID_TIME_TEXT); 
     735    } else { 
     736            char buf [MAX_I18NTIMELENGTH + 1]; 
     737            size_t a, b; 
     738            a = strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), lt); 
     739            b = strftime (buf, sizeof(buf)-1, _("%b %e  %Y"), lt); 
     740            length = max (a, b); 
     741    } 
     742 
    737743    /* Don't handle big differences. Use standard value (email bug, please) */ 
    738744    if ( length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH ) 
    739         length = STD_I18NTIMELENGTH; 
     745            length = STD_I18NTIMELENGTH; 
    740746     
    741747    return length; 
    742748} 
    file_date (time_t when) 
    773779    else 
    774780        fmt = fmttime; 
    775781     
    776     whentm = localtime(&when); 
    777     if (whentm == NULL) 
    778         g_snprintf (timebuf, i18n_timelength, "%s", _("(invalid)")); 
    779     else 
    780         strftime (timebuf, i18n_timelength, fmt, whentm); 
     782    FMT_LOCALTIME(timebuf, i18n_timelength, fmt, when); 
     783 
    781784    return timebuf; 
    782785} 
    783786