Ticket #134: mc-4.6.1-timefmt.diff

File mc-4.6.1-timefmt.diff, 4.8 KB (added by Enrico Weigelt, 15 years ago)

Added by email2trac

  • edit/edit.c

    #
    # Adds an safe(r) strftime()/localtime() handling, as replacement
    # for the "mc-4.6.1-invalid-mtime.diff" patch
    #
    # Source:	metux
    # Reference:	4.6.1
    # Submit-By:	Enrico Weigelt, metux IT service <weigelt@metux.de>
    # Submit-Date:	2008-12-30
    # Obsoletes:	mc-4.6.1-invalid-mtime.diff
    # 
    diff -ruN mc-4.6.1.orig/edit/edit.c mc-4.6.1/edit/edit.c
    old new  
    2929#include "../src/cmd.h"         /* view_other_cmd() */ 
    3030#include "../src/user.h"        /* user_menu_cmd() */ 
    3131#include "../src/wtools.h"      /* query_dialog() */ 
     32#include "../src/timefmt.h"     /* time formatting */ 
    3233 
    3334/* 
    3435   what editor are we going to emulate? one of EDIT_KEY_EMULATION_NORMAL 
     
    25122513        break; 
    25132514 
    25142515    case CK_Date:{ 
    2515             time_t t; 
    2516 #ifdef HAVE_STRFTIME 
    25172516            char s[1024]; 
    25182517            /* fool gcc to prevent a Y2K warning */ 
    25192518            char time_format[] = "_c"; 
    25202519            time_format[0] = '%'; 
    2521 #endif 
    2522             time (&t); 
    2523 #ifdef HAVE_STRFTIME 
    2524             strftime (s, sizeof (s), time_format, localtime (&t)); 
     2520 
     2521            FMT_LOCALTIME_CURRENT(s, sizeof(s), time_format); 
    25252522            edit_print_string (edit, s); 
    2526 #else 
    2527             edit_print_string (edit, ctime (&t)); 
    2528 #endif 
    25292523            edit->force |= REDRAW_PAGE; 
    25302524            break; 
    25312525        } 
  • src/Makefile.am

    diff -ruN mc-4.6.1.orig/src/Makefile.am mc-4.6.1/src/Makefile.am
    old new  
    5757        popt.c poptconfig.c popt.h popthelp.c poptint.h poptparse.c     \ 
    5858        profile.c profile.h regex.c rxvt.c screen.c setup.c setup.h     \ 
    5959        slint.c subshell.c subshell.h textconf.c textconf.h             \ 
    60         tree.c tree.h treestore.c treestore.h tty.h user.c user.h       \ 
    61         util.c util.h utilunix.c view.c view.h vfsdummy.h widget.c      \ 
    62         widget.h win.c win.h wtools.c wtools.h                          \ 
     60        tree.c tree.h treestore.c treestore.h timefmt.h tty.h user.c    \ 
     61        user.h util.c util.h utilunix.c view.c view.h vfsdummy.h        \ 
     62        widget.c widget.h win.c win.h wtools.c wtools.h                 \ 
    6363        x11conn.h x11conn.c 
    6464 
    6565if CHARSET 
  • src/timefmt.h

    diff -ruN mc-4.6.1.orig/src/timefmt.h mc-4.6.1/src/timefmt.h
    old new  
     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 -ruN mc-4.6.1.orig/src/util.c mc-4.6.1/src/util.c
    old new  
    3939#include "cmd.h"                /* guess_message_value */ 
    4040#include "mountlist.h" 
    4141#include "win.h"                /* xterm_flag */ 
     42#include "timefmt.h" 
    4243 
    4344#ifdef HAVE_CHARSET 
    4445#include "charsets.h" 
     
    694695short-month-name sizes for different locales */  
    695696size_t i18n_checktimelength (void) 
    696697{ 
    697     size_t length, a, b; 
    698     char buf [MAX_I18NTIMELENGTH + 1]; 
    699698    time_t testtime = time (NULL); 
    700      
    701     a = strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), localtime(&testtime)); 
    702     b = strftime (buf, sizeof(buf)-1, _("%b %e  %Y"), localtime(&testtime)); 
    703      
    704     length = max (a, b); 
    705      
     699    struct tm* lt = localtime(&testtime); 
     700    size_t length; 
     701 
     702    if (lt == NULL) 
     703    { 
     704        // huh, localtime() doesnt seem to work ... falling back to "(invalid)" 
     705        length = strlen(INVALID_TIME_TEXT); 
     706    } 
     707    else 
     708    { 
     709        char buf [MAX_I18NTIMELENGTH + 1]; 
     710        size_t a, b; 
     711        a = strftime (buf, sizeof(buf)-1, _("%b %e %H:%M"), lt); 
     712        b = strftime (buf, sizeof(buf)-1, _("%b %e  %Y"), lt); 
     713        length = max (a, b); 
     714    } 
     715 
    706716    /* Don't handle big differences. Use standard value (email bug, please) */ 
    707717    if ( length > MAX_I18NTIMELENGTH || length < MIN_I18NTIMELENGTH ) 
    708718        length = STD_I18NTIMELENGTH; 
    709      
     719 
    710720    return length; 
    711721} 
    712722 
     
    740750    else 
    741751        fmt = fmttime; 
    742752     
    743     strftime (timebuf, i18n_timelength, fmt, localtime(&when)); 
     753    FMT_LOCALTIME(timebuf, i18n_timelength, fmt, when); 
    744754    return timebuf; 
    745755} 
    746756