Use localtime_t() instead of localtime()

This commit is contained in:
Christophe Grenier 2017-04-15 12:00:24 +02:00
parent 1cba494e47
commit a1419a0806
10 changed files with 45 additions and 94 deletions

View file

@ -6,7 +6,7 @@ AC_INIT([testdisk],[7.1-WIP],[grenier@cgsecurity.org])
AC_LANG(C)
sinclude(acx_pthread.m4)
sinclude(mkdir.m4)
TESTDISKDATE="August 2016"
TESTDISKDATE="April 2017"
AC_SUBST(TESTDISKDATE)
AC_DEFINE_UNQUOTED([TESTDISKDATE],"$TESTDISKDATE",[Date of release])
AC_CONFIG_AUX_DIR(config)
@ -872,7 +872,7 @@ case "$target" in
;;
esac
AC_CHECK_FUNCS([ atexit atoll chdir chmod delscreen dirname dup2 execv fdatasync fseeko fsync ftello ftruncate getcwd geteuid getpwuid libewf_handle_read_buffer_at_offset libewf_handle_write_buffer_at_offset lstat memalign memchr memset mkdir posix_fadvise posix_memalign pwrite readlink setenv setlocale sigaction signal sleep snprintf strcasecmp strcasestr strchr strdup strerror strncasecmp strptime strrchr strstr strtol strtoul strtoull touchwin uname utime vsnprintf wctomb ])
AC_CHECK_FUNCS([ atexit atoll chdir chmod delscreen dirname dup2 execv fdatasync fseeko fsync ftello ftruncate getcwd geteuid getpwuid libewf_handle_read_buffer_at_offset libewf_handle_write_buffer_at_offset localtime_r lstat memalign memchr memset mkdir posix_fadvise posix_memalign pwrite readlink setenv setlocale sigaction signal sleep snprintf strcasecmp strcasestr strchr strdup strerror strncasecmp strptime strrchr strstr strtol strtoul strtoull touchwin uname utime vsnprintf wctomb ])
if test "$ac_cv_func_mkdir" = "no"; then
AC_MSG_ERROR(No mkdir function detected)
fi

View file

@ -79,7 +79,6 @@ char *get_default_location(void)
}
#ifdef HAVE_NCURSES
extern const char *monstr[];
#ifdef __MINGW32__
#define SPATH_SEP "\\"
@ -575,18 +574,7 @@ static void dir_aff_entry(WINDOW *window, file_info_t *file_info)
{
char str[11];
char datestr[80];
{
const struct tm *tm_p;
if(file_info->td_mtime!=0 && (tm_p= localtime(&file_info->td_mtime))!=NULL)
{
snprintf(datestr, sizeof(datestr),"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
} else {
strncpy(datestr, " ",sizeof(datestr));
}
}
set_datestr((char *)&datestr, sizeof(datestr), file_info->td_mtime);
mode_string(file_info->st_mode, str);
wprintw(window, "%s %5u %5u ",
str, (unsigned int)file_info->st_uid, (unsigned int)file_info->st_gid);

View file

@ -166,6 +166,14 @@ char * strcasestr (const char *haystack, const char *needle)
return NULL;
}
#endif
#ifndef HAVE_LOCALTIME_R
struct tm *localtime_r(const time_t *timep, struct tm *result)
{
return localtime(timep);
}
#endif
unsigned int up2power(const unsigned int number)
{
/* log_trace("up2power(%u)=>%u\n",number, (1<<up2power_aux(number-1))); */
@ -239,7 +247,8 @@ int date_dos2unix(const unsigned short f_time, const unsigned short f_date)
void set_secwest(void)
{
const time_t t = time(NULL);
const struct tm *tmptr = localtime(&t);
struct tm tmp;
const struct tm *tmptr = localtime_r(&t,&tmp);
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
if(tmptr)
secwest = -1 * tmptr->tm_gmtoff;

View file

@ -496,6 +496,9 @@ int strncasecmp(const char * s1, const char * s2, size_t len);
#ifndef HAVE_STRCASESTR
char * strcasestr (const char *haystack, const char *needle);
#endif
#ifndef HAVE_LOCALTIME_R
struct tm *localtime_r(const time_t *timep, struct tm *result);
#endif
/*
* td_min()/td_max() macros that also do
* strict type-checking.. See the

View file

@ -253,7 +253,8 @@ void xml_add_DFXML_creator(const char *package, const char *version)
{
char outstr[200];
const time_t t = time(NULL);
const struct tm *tmp = localtime(&t);
struct tm tm_tmp;
const struct tm *tmp = localtime_r(&t,&tm_tmp);
if (tmp != NULL &&
strftime(outstr, sizeof(outstr), "%Y-%m-%dT%H:%M:%S%z", tmp) != 0)
{

View file

@ -153,6 +153,27 @@ void mode_string (const unsigned int mode, char *str)
#endif
}
int set_datestr(char *datestr, size_t n, const time_t timev)
{
int test_date=0;
struct tm tmp;
const struct tm *tm_p;
if(timev!=0 && (tm_p= localtime_r(&timev, &tmp))!=NULL)
{
snprintf(datestr, n,"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
if(1900+tm_p->tm_year>=2000 && 1900+tm_p->tm_year<=2014)
{
test_date=1;
}
} else {
strncpy(datestr, " ", n);
}
return test_date;
}
int dir_aff_log(const dir_data_t *dir_data, const file_info_t *dir_list)
{
int test_date=0;
@ -166,23 +187,7 @@ int dir_aff_log(const dir_data_t *dir_data, const file_info_t *dir_list)
const file_info_t *current_file=td_list_entry_const(file_walker, const file_info_t, list);
char datestr[80];
char str[11];
{
const struct tm *tm_p;
if(current_file->td_mtime && (tm_p = localtime(&current_file->td_mtime))!=NULL)
{
snprintf(datestr, sizeof(datestr),"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
/* FIXME: a check using current_file->name will be better */
if(1900+tm_p->tm_year>=2000 && 1900+tm_p->tm_year<=2014)
{
test_date=1;
}
} else {
strncpy(datestr, " ",sizeof(datestr));
}
}
test_date=set_datestr((char *)&datestr, sizeof(datestr), current_file->td_mtime);
mode_string(current_file->st_mode, str);
if((current_file->status&FILE_STATUS_DELETED)!=0)
log_info("X");
@ -225,23 +230,7 @@ int log_list_file(const disk_t *disk, const partition_t *partition, const dir_da
log_info("X");
else
log_info(" ");
{
const struct tm *tm_p;
if(current_file->td_mtime && (tm_p = localtime(&current_file->td_mtime))!=NULL)
{
snprintf(datestr, sizeof(datestr),"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
/* FIXME: a check using current_file->name will be better */
if(1900+tm_p->tm_year>=2000 && 1900+tm_p->tm_year<=2014)
{
test_date=1;
}
} else {
strncpy(datestr, " ",sizeof(datestr));
}
}
test_date=set_datestr((char *)&datestr, sizeof(datestr), current_file->td_mtime);
mode_string(current_file->st_mode, str);
log_info("%7lu ",(unsigned long int)current_file->st_ino);
log_info("%s %5u %5u ",

View file

@ -74,6 +74,7 @@ struct dir_data
#define FILE_STATUS_MARKED 2
#define FILE_STATUS_ADS 4
int set_datestr(char *datestr, size_t n, const time_t timev);
int dir_aff_log(const dir_data_t *dir_data, const file_info_t*dir_list);
int log_list_file(const disk_t *disk_car, const partition_t *partition, const dir_data_t *dir_data, const file_info_t*list);
unsigned int delete_list_file(file_info_t *list);

View file

@ -45,8 +45,6 @@
#include "askloc.h"
#include "setdate.h"
extern const char *monstr[];
static int copy_dir(WINDOW *window, disk_t *disk, const partition_t *partition, dir_data_t *dir_data, const file_info_t *dir, unsigned int *copy_ok, unsigned int *copy_bad);
static int copy_selection(file_info_t*dir_list, WINDOW *window, disk_t *disk, const partition_t *partition, dir_data_t *dir_data, unsigned int *copy_ok, unsigned int *copy_bad);
@ -152,19 +150,7 @@ static long int dir_aff_ncurses(disk_t *disk, const partition_t *partition, dir_
else if((current_file->status&FILE_STATUS_DELETED)!=0)
wbkgdset(window,' ' | COLOR_PAIR(1));
}
{
const struct tm *tm_p;
if(current_file->td_mtime!=0 && (tm_p = localtime(&current_file->td_mtime))!=NULL)
{
snprintf(datestr, sizeof(datestr),"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
/* May have to use %d instead of %e */
} else {
strncpy(datestr, " ",sizeof(datestr));
}
}
set_datestr((char *)&datestr, sizeof(datestr), current_file->td_mtime);
mode_string(current_file->st_mode, str);
wprintw(window, "%s %5u %5u ",
str, (unsigned int)current_file->st_uid, (unsigned int)current_file->st_gid);

View file

@ -62,7 +62,6 @@
#define INTER_FAT_ASK_Y 23
#define INTER_FATBS_X 0
#define INTER_FATBS_Y 22
extern const char *monstr[];
typedef struct info_offset_struct info_offset_t;
@ -217,18 +216,7 @@ static int ask_root_directory(disk_t *disk_car, const partition_t *partition, co
}
else
waddstr(window, " ");
{
const struct tm *tm_p;
if(current_file->td_mtime!=0 && (tm_p=localtime(&current_file->td_mtime))!=NULL)
{
snprintf(datestr, sizeof(datestr),"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
} else {
strncpy(datestr, " ",sizeof(datestr));
}
}
set_datestr((char *)&datestr, sizeof(datestr), current_file->td_mtime);
mode_string(current_file->st_mode, str);
wprintw(window, "%s %3u %3u ",
str, (unsigned int)current_file->st_uid, (unsigned int)current_file->st_gid);

View file

@ -110,8 +110,6 @@
#include "askloc.h"
#include "setdate.h"
extern const char *monstr[];
struct options {
char *dest; /* Save file to this directory */
};
@ -1265,19 +1263,7 @@ static void ntfs_undelete_menu_ncurses(disk_t *disk_car, const partition_t *part
waddstr(window, " ");
if((file_info->status&FILE_STATUS_MARKED)!=0 && has_colors())
wbkgdset(window,' ' | COLOR_PAIR(2));
{
const struct tm *tm_p;
if(file_info->td_mtime!=0 && (tm_p=localtime(&file_info->td_mtime))!=NULL)
{
snprintf(datestr, sizeof(datestr),"%2d-%s-%4d %02d:%02d",
tm_p->tm_mday, monstr[tm_p->tm_mon],
1900 + tm_p->tm_year, tm_p->tm_hour,
tm_p->tm_min);
/* May have to use %d instead of %e */
} else {
strncpy(datestr, " ",sizeof(datestr));
}
}
set_date((char *)&datestr, sizeof(datestr), file_info->td_mtime);
if(COLS <= 1+17+1+9+1)
wprintw(window, "%s", file_info->name);
else