This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
tango-tfe/common/src/tfe_utils.cpp

132 lines
3.1 KiB
C++
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tfe_utils.h>
#include <time.h>
int addr_sock_to_layer(struct sockaddr * sock_addr, int sockaddrlen, struct layer_addr * layer_addr)
{
return 0;
}
int addr_layer_to_sock(struct layer_addr * layer_addr, struct sockaddr * sock_addr)
{
int sockaddrlen=-1;
return sockaddrlen;
}
//functioned as strdup, for dictator compatible.
char* tfe_strdup(const char* s)
{
char*d=NULL;
if(s==NULL)
{
return NULL;
}
d=(char*)malloc(strlen(s)+1);
memcpy(d,s,strlen(s)+1);
return d;
}
char *tfe_thread_safe_ctime(const time_t *tp, char *buf, int len)
{
unsigned int year, month, day, weekday, hour, min, sec;
unsigned int year_days = 365;
unsigned int month_days[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static unsigned char weekday_str[7][4] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
sec = * tp;
min = sec / 60; sec = sec % 60;
hour = min / 60; min = min % 60; hour += 8;
day = hour / 24; hour = hour % 24;
weekday = day % 7; weekday = (weekday + 4) % 7;
for(year = 1970; day >= year_days;)
{
day -= year_days;
year ++;
if(0 == year % 4 && (0 != year % 100 || 0 == year % 400))
year_days = 366;
else year_days = 365;
}
if(366 == year_days) month_days[1] = 29;
//bug fix by yw 20120808
for(month = 0; day >= month_days[month];)
{
day -= month_days[month];
month ++;
}
/*
snprintf(buf, len, "%02d:%02d:%02d, %04d/%02d/%02d, %s",
hour, min, sec, year, month, day, weekday_str[week_day]);
*/
snprintf(buf, len, "%s %s %d %02d:%02d:%02d %d", weekday_str[weekday],
month_str[month], day + 1, hour, min, sec, year);
return buf;
}
//replacement of glibc scandir, to adapt dictator malloc wrap
int tfe_scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const void *, const void *))
{
DIR * od;
int n = 0;
int ENLARGE_STEP=1024;
int DIR_ENT_SIZE=ENLARGE_STEP;
struct dirent ** list = NULL;
struct dirent * p;
struct dirent entry,*result;
if((dir == NULL) || (namelist == NULL))
return -1;
od = opendir(dir);
if(od == NULL)
return -1;
list = (struct dirent **)malloc(DIR_ENT_SIZE*sizeof(struct dirent *));
while(0==readdir_r(od,&entry,&result))
{
if(result==NULL)
{
break;
}
if( filter && !filter(&entry))
continue;
p = (struct dirent *)malloc(sizeof(struct dirent));
memcpy((void *)p,(void *)(&entry),sizeof(struct dirent));
list[n] = p;
n++;
if(n >= DIR_ENT_SIZE)
{
DIR_ENT_SIZE+=ENLARGE_STEP;
list=(struct dirent **)realloc((void*)list,DIR_ENT_SIZE*sizeof(struct dirent *));
}
}
closedir(od);
*namelist = list;
if(compar)
qsort((void *)*namelist,n,sizeof(struct dirent *),compar);
return n;
}