summaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c98
1 files changed, 75 insertions, 23 deletions
diff --git a/src/common.c b/src/common.c
index 3b1f45b..04ed1aa 100644
--- a/src/common.c
+++ b/src/common.c
@@ -24,6 +24,11 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
+
+#ifdef __FRAMAC__
+#undef HAVE_POSIX_MEMALIGN
+#undef HAVE_MEMALIGN
+#endif
#include <stdio.h>
#include <ctype.h>
@@ -51,8 +56,6 @@
static int32_t secwest=0;
-static unsigned int up2power_aux(const unsigned int number);
-
/* coverity[+alloc] */
void *MALLOC(size_t size)
{
@@ -167,21 +170,16 @@ char * strcasestr (const char *haystack, const char *needle)
}
#endif
-#if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__)
+#if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__) && !defined(__FRAMAC__)
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))); */
- if(number==0)
- return 1;
- return (1<<up2power_aux(number-1));
-}
-
+/*@
+ @ assigns \nothing;
+ @*/
static unsigned int up2power_aux(const unsigned int number)
{
if(number==0)
@@ -190,6 +188,13 @@ static unsigned int up2power_aux(const unsigned int number)
return(1+up2power_aux(number/2));
}
+unsigned int up2power(const unsigned int number)
+{
+ if(number==0)
+ return 1;
+ return (1<<up2power_aux(number-1));
+}
+
void set_part_name(partition_t *partition, const char *src, const unsigned int max_size)
{
unsigned int i;
@@ -225,22 +230,53 @@ char* strip_dup(char* str)
}
/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
+/*
+ * The epoch of FAT timestamp is 1980.
+ * : bits : value
+ * date: 0 - 4: day (1 - 31)
+ * date: 5 - 8: month (1 - 12)
+ * date: 9 - 15: year (0 - 127) from 1980
+ * time: 0 - 4: sec (0 - 29) 2sec counts
+ * time: 5 - 10: min (0 - 59)
+ * time: 11 - 15: hour (0 - 23)
+ */
+#define SECS_PER_MIN 60
+#define SECS_PER_HOUR (60 * 60)
+#define SECS_PER_DAY (SECS_PER_HOUR * 24)
+/* days between 1.1.70 and 1.1.80 (2 leap days) */
+#define DAYS_DELTA (365 * 10 + 2)
+/* 120 (2100 - 1980) isn't leap year */
+#define YEAR_2100 120
+#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
-int date_dos2unix(const unsigned short f_time, const unsigned short f_date)
+time_t date_dos2unix(const unsigned short f_time, const unsigned short f_date)
{
- static const int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
+ static const unsigned int days_in_year[] = { 0, 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0 };
/* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
- int month,year,secs;
+ unsigned long int day,leap_day,month,year,days;
+ unsigned long int secs;
+ year = f_date >> 9;
+ month = td_max(1, (f_date >> 5) & 0xf);
+ day = td_max(1, f_date & 0x1f) - 1;
+
+ leap_day = (year + 3) / 4;
+ if (year > YEAR_2100) /* 2100 isn't leap year */
+ {
+ /*@ assert year > YEAR_2100 && leap_day > (YEAR_2100 + 3)/4; */
+ leap_day--;
+ }
+ if (IS_LEAP_YEAR(year) && month > 2)
+ leap_day++;
+ days = days_in_year[month];
+ /*@ assert days <= 334; */
+ days += year * 365 + leap_day + day + DAYS_DELTA;
+
+ secs = (f_time & 0x1f)<<1;
+ secs += ((f_time >> 5) & 0x3f) * SECS_PER_MIN;
+ secs += (f_time >> 11)* SECS_PER_HOUR;
+ secs += days * SECS_PER_DAY;
- /* first subtract and mask after that... Otherwise, if
- f_date == 0, bad things happen */
- month = ((f_date >> 5) - 1) & 15;
- year = f_date >> 9;
- secs = (f_time & 31)*2+60*((f_time >> 5) & 63)+(f_time >> 11)*3600+86400*
- ((f_date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
- month < 2 ? 1 : 0)+3653);
- /* days since 1.1.70 plus 80's leap day */
return secs+secwest;
}
@@ -287,23 +323,39 @@ time_t td_ntfs2utc (int64_t ntfstime)
return (ntfstime - (NTFS_TIME_OFFSET)) / 10000000;
}
-int check_command(char **current_cmd, const char *cmd, size_t n)
+int check_command(char **current_cmd, const char *cmd, const size_t n)
{
const int res=strncmp(*current_cmd, cmd, n);
if(res==0)
+ {
(*current_cmd)+=n;
+ /*@ assert valid_read_string(*current_cmd); */
+ return 0;
+ }
+ /*@ assert valid_read_string(*current_cmd); */
return res;
}
void skip_comma_in_command(char **current_cmd)
{
+ /*@
+ loop invariant valid_read_string(*current_cmd);
+ loop assigns *current_cmd;
+ */
while(*current_cmd[0]==',')
+ {
(*current_cmd)++;
+ }
+ /*@ assert valid_read_string(*current_cmd); */
}
uint64_t get_int_from_command(char **current_cmd)
{
uint64_t tmp=0;
+ /*@
+ loop invariant valid_read_string(*current_cmd);
+ loop assigns *current_cmd, tmp;
+ */
while(*current_cmd[0] >='0' && *current_cmd[0] <= '9')
{
tmp = tmp * 10 + *current_cmd[0] - '0';