X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=tfc_misc.c;h=bfb7265ff2dbf00442f26702456e7caeaf2ea864;hb=ba5ce99d77078a8629779641e8506b74e50fe6af;hp=d1bb78368d2126bfa1a1b411178b1b79561a6d9f;hpb=efa545d64b910923248233618e774ca3b87efebb;p=tfcrypt.git diff --git a/tfc_misc.c b/tfc_misc.c index d1bb783..bfb7265 100644 --- a/tfc_misc.c +++ b/tfc_misc.c @@ -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: * @@ -63,6 +63,26 @@ tfc_yesno str_empty(const char *str) return NO; } +int xxopen(tfc_yesno noerr, const char *pathname, int flags) +{ + int r; + + if ((flags & O_WRONLY || flags & O_RDWR)) { + if (read_only == YES) flags = O_RDONLY; + else flags |= write_flags; + } + + flags |= O_LARGEFILE; + r = open(pathname, flags, 0666); + if (noerr == NO && r == -1) xerror(NO, NO, YES, "%s", pathname); + return r; +} + +int xopen(const char *pathname, int flags) +{ + return xxopen(NO, pathname, flags); +} + void xclose(int fd) { if (fd < 3) return; @@ -85,29 +105,71 @@ 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; +} + +char *tfc_format_pid(const char *str) +{ + static char r[128]; + size_t n; + + n = xstrlcpy(r, str, sizeof(r)); + if (show_pid == YES && sizeof(r)-n >= 22) sprintf(r+n, "[%lu]", (unsigned long)progpid); + + return r; +} + tfc_fsize tfc_fdsize(int fd) { off_t l, cur; cur = lseek(fd, 0L, SEEK_CUR); l = lseek(fd, 0L, SEEK_SET); - if (l == -1) return -1; + if (l == -1) return NOFSIZE; l = lseek(fd, 0L, SEEK_END); - if (l == -1) return -1; + if (l == -1) return NOFSIZE; lseek(fd, cur, SEEK_SET); return (tfc_fsize)l; } +tfc_fsize tfc_fdgetpos(int fd) +{ + off_t t; + + t = lseek(fd, 0L, SEEK_CUR); + if (t == (off_t)-1) return NOFSIZE; + return (tfc_fsize)t; +} + tfc_fsize tfc_fnamesize(char *fname, tfc_yesno noexit) { int fnmfd; @@ -122,7 +184,7 @@ tfc_fsize tfc_fnamesize(char *fname, tfc_yesno noexit) memset(s, 0, 2); } - fnmfd = open(fname, O_RDONLY); + fnmfd = xxopen(YES, fname, O_RDONLY); if (s) memcpy(s, T, 2); if (fnmfd == -1) { xerror(noexit, NO, YES, "%s", fname);