change atexit newline print policy
authorAndrey Rys <rys@lynxlynx.ru>
Sun, 28 Nov 2021 13:36:30 +0000 (14:36 +0100)
committerAndrey Rys <rys@lynxlynx.ru>
Sun, 28 Nov 2021 13:36:55 +0000 (14:36 +0100)
VERSION
tfc_base64.c
tfc_error.c
tfc_random.c
tfc_signal.c
tfc_skein.c
tfcrypt.c
tfcrypt.h

diff --git a/VERSION b/VERSION
index e373ee695f6e76d7d3f8f8c4e92d1d60995352e5..82cced27d7be32719d009707139bd949ad6263c9 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-50
+51
index c76fdd90dfe912e809dd8d5d2cc257fb72f198fd..68f0677998db07d6046e1edf8e8af87ae17711fb 100644 (file)
@@ -36,6 +36,8 @@ void do_edbase64(char **fargv)
        struct base64_encodestate estate;
        size_t lread = 0;
 
+       xexit_no_nl = YES;
+
        sfd = 0; dfd = 1;
 
        if (fargv[0]) {
@@ -148,7 +150,6 @@ _wagain:    lio = xwrite(dfd, pblk, lrem);
 
        memset(&estate, 0, sizeof(struct base64_encodestate));
        memset(&dstate, 0, sizeof(struct base64_decodestate));
-       if (do_preserve_time) fcopy_matime(dfd, &s_stat);
        xexit(0);
 }
 
index 9bface14b50b5b13535f703ce82c188df21ecf1f..ad14e7d0bc23eca1cf7b97230738f628fa8c75e2 100644 (file)
 
 #include "tfcrypt.h"
 
+tfc_yesno xexit_no_nl;
+
 void xerror(tfc_yesno noexit, tfc_yesno noerrno, tfc_yesno nostats, const char *fmt, ...)
 {
        va_list ap;
        char *s;
 
-       if (quiet) goto _ex;
+       if (quiet) goto _do_sil_exit;
 
        va_start(ap, fmt);
 
@@ -49,9 +51,14 @@ void xerror(tfc_yesno noexit, tfc_yesno noerrno, tfc_yesno nostats, const char *
 
        va_end(ap);
 
-       if (nostats == NO) print_crypt_status(-1);
+       if (nostats == NO) {
+               print_crypt_status(-1);
+               tfc_esay("\n");
+       }
+
+       xexit_no_nl = YES;
 
-_ex:
+_do_sil_exit:
        if (noexit == YES) {
                errno = 0;
                return;
@@ -64,15 +71,16 @@ void xexit(int status)
 {
        if (status > 1) goto _do_clean_and_exit;
 
-       xclose(sfd);
        if (do_ftrunc > TFC_NO_FTRUNC) {
                if (do_ftrunc == TFC_FTRUNC_TAIL) ftrunc_dfd = total_processed_dst;
                if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);
        }
        if (do_preserve_time) fcopy_matime(dfd, &s_stat);
-       xclose(dfd);
 
 _do_clean_and_exit:
+       xclose(sfd);
+       xclose(dfd);
+
        memset(srcblk, 0, sizeof(srcblk));
        memset(dstblk, 0, sizeof(dstblk));
 
@@ -94,6 +102,7 @@ _do_clean_and_exit:
        memset(pwdask, 0, sizeof(pwdask));
        memset(pwdagain, 0, sizeof(pwdagain));
 
+       if (xexit_no_nl == NO) tfc_esay("\n");
        exit(status);
 }
 
@@ -101,6 +110,8 @@ void usage(void)
 {
        tfc_yesno is_embedded_prog = NO;
 
+       xexit_no_nl = YES;
+
        if (optopt == 'V') {
                tfc_say("tfcrypt toolkit, version %s.", _TFCRYPT_VERSION);
                if (ctr_mode != TFC_MODE_PLAIN) {
index 9f9105af68fdaf9a53beca93f24ff1e43882309d..d3f2ee303ff09bcd8a9dfe6d93dc19518fb451be 100644 (file)
@@ -88,6 +88,8 @@ void gen_write_bytes(const char *foutname, tfc_fsize offset, tfc_fsize nrbytes)
        size_t lblock, lio, lrem;
        tfc_byte *pblk;
 
+       xexit_no_nl = YES;
+
        for (x = 1; x < NSIG; x++) signal(x, SIG_IGN);
        memset(&sigact, 0, sizeof(sigact));
        sigact.sa_flags = SA_RESTART;
@@ -180,6 +182,7 @@ _wagain:    lio = xwrite(fd, pblk, lrem);
 
        if (verbose) tfc_esay("done!");
        if (verbose || status_timer) print_crypt_status(0);
+       if (verbose) tfc_esay("\n");
 
        xclose(fd);
        xexit(0);
index 25d765e7ed0f9a6806a7f2717b45b22462f0161c..1e025349a52c70e73a6c945d1ac141353ad0bdcc 100644 (file)
@@ -52,9 +52,14 @@ void print_crypt_status(int signal)
        int src_scale_idx, dst_scale_idx, wri_scale_idx, wr_speed_scale;
        const char *oper_mode, *inplace;
        static tfc_yesno last;
+       tfc_yesno finished = NO;
 
        if (last == YES) return;
-       if (signal == 0 || signal == -1) last = YES;
+       if (signal <= 0) {
+               finished = YES;
+               do_stop = YES; /* error path or sksum finished */
+               if (signal == 0) last = YES;
+       }
 
        switch (do_edcrypt) {
                case TFC_DO_ENCRYPT: oper_mode = "encrypted"; break;
@@ -87,7 +92,6 @@ void print_crypt_status(int signal)
                        oper_mode,
                        total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
                        wr_speed, human_wr_speed, tfc_getscale(wr_speed_scale), seconds);
-               tfc_esay("\n");
                xexit(0);
        }
 
@@ -98,7 +102,7 @@ void print_crypt_status(int signal)
                tfc_nfsay(stderr, "%s%s%s:"
                        " %s %.2f%s,"
                        " %.2f%s B/s @%s",
-                       inplace, (last && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
+                       inplace, (finished && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
                        oper_mode,
                        human_totalproc_dst, tfc_getscale(dst_scale_idx),
                        human_wr_speed, tfc_getscale(wr_speed_scale), tfc_format_time(total_time));
@@ -107,7 +111,7 @@ void print_crypt_status(int signal)
                if (ctr_mode <= TFC_MODE_PLAIN) tfc_nfsay(stderr, "%s%s%s: read: %llu (%.2f%s),"
                        " %s %llu (%.2f%s) bytes,"
                        " (%llu (%.2f%s) B/s), time %s",
-                       inplace, (last && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
+                       inplace, (finished && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
                        total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
                        oper_mode,
                        total_processed_dst, human_totalproc_dst, tfc_getscale(dst_scale_idx),
@@ -116,7 +120,7 @@ void print_crypt_status(int signal)
                        " %s %s %llu (%.2f%s) bytes,"
                        " written %llu (%.2f%s) bytes,"
                        " (%llu (%.2f%s) B/s), time %s",
-                       inplace, (last && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
+                       inplace, (finished && show_when_done) ? "finished: " : "", tfc_format_pid(progname),
                        total_processed_src, human_totalproc_src, tfc_getscale(src_scale_idx),
                        tfc_modename(ctr_mode), oper_mode,
                        total_processed_dst, human_totalproc_dst, tfc_getscale(dst_scale_idx),
@@ -125,13 +129,9 @@ void print_crypt_status(int signal)
        }
 
        if (do_stop == NO && do_statline_dynamic == NO) tfc_esay("\n");
-       if (last) tfc_esay("\n");
        statline_was_shown = YES;
 
-       if ((signal == SIGINT || signal == SIGTERM) && do_stop == YES) {
-               tfc_esay("\n");
-               exit_sigterm(signal);
-       }
+       if ((signal == SIGINT || signal == SIGTERM) && do_stop == YES) exit_sigterm(signal);
 
        delta_processed = 0;
        tfc_getcurtime(&delta_time);
index 58cd36e716bf6e72453217a517b68d3ea4abbca4..e2c31f9a2e7d69c35b7f239b2a5057650fed5775 100644 (file)
@@ -47,7 +47,6 @@ tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize of
        tfc_byte *pblk;
        size_t ldone, lblock, lrem, lio;
        tfc_fsize total = 0;
-       tfc_yesno stop;
 
        if (ctr_mode == TFC_MODE_SKSUM) total_processed_src = total_processed_dst = delta_processed = 0;
 
@@ -68,15 +67,15 @@ tfc_yesno skeinfd(void *hash, size_t bits, const void *key, int fd, tfc_fsize of
        else skein_init(&sk, bits);
 
        errno = 0;
-       stop = NO;
+       do_stop = NO;
        while (1) {
-               if (stop) break;
+               if (do_stop) break;
                pblk = skblk;
                lblock = lrem = blk_len_adj(readto, total, TFC_BLKSIZE);
                ldone = 0;
                if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(fd);
 _again:                lio = xread(fd, pblk, lrem);
-               if (lio == 0) stop = YES;
+               if (lio == 0) do_stop = YES;
                if (lio != NOSIZE) ldone += lio;
                else {
                        if (errno != EIO && catch_all_errors != YES) goto _fail;
@@ -112,7 +111,10 @@ _again:            lio = xread(fd, pblk, lrem);
 
        skein_final(hash, &sk);
        if (ctr_mode == TFC_MODE_SKSUM) {
-               if (verbose || status_timer) print_crypt_status(-1);
+               if (verbose || status_timer) {
+                       print_crypt_status(-1);
+                       tfc_esay("\n");
+               }
                total_processed_src = total_processed_dst = delta_processed = 0;
        }
        memset(skblk, 0, TFC_BLKSIZE);
@@ -133,6 +135,8 @@ void do_sksum(char *spec, char **fargv)
        int x = 0, xx;
        size_t bits;
 
+       xexit_no_nl = YES;
+
        if (macbits < TF_MAX_BITS) {
                bits = macbits;
                goto _dothat;
index dd137ae6ba4d9ac26d05fd5596bee5c4e9d22d6e..facb5077c9bd864cb592447dff6b3ce429b50296 100644 (file)
--- a/tfcrypt.c
+++ b/tfcrypt.c
@@ -576,6 +576,7 @@ _baddfname:
                                break;
                        case 'q':
                                quiet = YES;
+                               xexit_no_nl = YES;
                                verbose = NO;
                                do_full_hexdump = NO;
                                status_timer = 0;
index 569e82ca76f6ba1d58a50827e7ae26b848f83287..a571397d5186b9423437ee96803e68b4b37c2ef4 100644 (file)
--- a/tfcrypt.h
+++ b/tfcrypt.h
@@ -170,6 +170,8 @@ extern struct getpasswd_state getps;
 size_t xread(int fd, void *data, size_t szdata);
 size_t xwrite(int fd, const void *data, size_t szdata);
 
+extern tfc_yesno xexit_no_nl;
+
 void xerror(tfc_yesno noexit, tfc_yesno noerrno, tfc_yesno nostats, const char *fmt, ...);
 void xexit(int status);
 void usage(void);