xexit: do post-crypt work only if exiting by system error or normally.
[tfcrypt.git] / tfc_misc.c
index e6af46ada39ca1b324649aa927c75c4c92411369..a39f4591831ddf988f8cbcb74ab323658c74b76a 100644 (file)
@@ -2,7 +2,7 @@
  * tfcrypt -- high security Threefish encryption tool.
  *
  * tfcrypt is copyrighted:
- * Copyright (C) 2012-2018 Andrey Rys. All rights reserved.
+ * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
  *
  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
  *
@@ -85,15 +85,37 @@ const char *tfc_modename(int mode)
 
 void tfc_getcurtime(tfc_useconds *tx)
 {
-       struct timeval t;
+       struct timespec t;
        memset(&t, 0, sizeof(t));
 
-       gettimeofday(&t, NULL);
-       *tx = t.tv_sec * 1000000 + t.tv_usec;
+       clock_gettime(CLOCK_MONOTONIC, &t);
+       *tx = (tfc_useconds)t.tv_sec * 1000000 + (t.tv_nsec / 1000);
 
        memset(&t, 0, sizeof(t));
 }
 
+char *tfc_format_time(tfc_useconds t)
+{
+       tfc_useconds secs, dsecs;
+       unsigned days, hours, minutes, seconds;
+       static char r[128];
+
+       secs = (tfc_useconds)TFC_UTODSECS(t);
+       dsecs = (tfc_useconds)(t - (secs * 1000000));
+
+       days = secs / 86400;
+       hours = (secs / 3600) % 24;
+       minutes = (secs / 60) % 60;
+       seconds = secs % 60;
+
+       if (days > 0) sprintf(r, "%ud,%02u:%02u:%02u.%04u", days, hours, minutes, seconds, (unsigned)(dsecs / 100));
+       else if (hours > 0) sprintf(r, "%02u:%02u:%02u.%04u", hours, minutes, seconds, (unsigned)(dsecs / 100));
+       else if (minutes > 0) sprintf(r, "%02u:%02u.%04u", minutes, seconds, (unsigned)(dsecs / 100));
+       else sprintf(r, "%02u.%04u", seconds, (unsigned)(dsecs / 100));
+
+       return r;
+}
+
 tfc_fsize tfc_fdsize(int fd)
 {
        off_t l, cur;
@@ -113,7 +135,7 @@ tfc_fsize tfc_fdgetpos(int fd)
        off_t t;
 
        t = lseek(fd, 0L, SEEK_CUR);
-       if (t == -1) return NOFSIZE;
+       if (t == (off_t)-1) return NOFSIZE;
        return (tfc_fsize)t;
 }