diff options
author | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2024-01-20 17:34:51 +0000 |
---|---|---|
committer | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2024-01-20 17:34:51 +0000 |
commit | 90ddbc6f481fc2cc1a095c4a4dda5c8a9e2e81a1 (patch) | |
tree | 4ac3311e312295cb1c10ba175c28ed6468820753 /bin/pax/tar.c | |
parent | b77f201f291b6b1a1148a848a6406e73d38303b5 (diff) |
Better formatting for pax extended header times
As specified, don't include the subsecond part if zero and drop trailing
zeros in the subsecond part. ok millert@
Diffstat (limited to 'bin/pax/tar.c')
-rw-r--r-- | bin/pax/tar.c | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/bin/pax/tar.c b/bin/pax/tar.c index 949e9ce4ec1..cd64ec7bad1 100644 --- a/bin/pax/tar.c +++ b/bin/pax/tar.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tar.c,v 1.78 2023/12/27 08:29:41 jca Exp $ */ +/* $OpenBSD: tar.c,v 1.79 2024/01/20 17:34:50 jca Exp $ */ /* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */ /*- @@ -984,13 +984,28 @@ xheader_add_ts(struct xheader *xhdr, const char *keyword, { struct xheader_record *rec; int reclen, tmplen; + char frac[sizeof(".111222333")] = ""; char *s; + /* Only write subsecond part if non-zero */ + if (value->tv_nsec != 0) { + int n; + + n = snprintf(frac, sizeof(frac), ".%09ld", + (long)value->tv_nsec); + if (n <= 0) + return -1; + + /* Zap trailing zeros */ + for (n--; n > 1 && frac[n] == '0'; n--) + frac[n] = '\0'; + } + tmplen = MINXHDRSZ; do { reclen = tmplen; - tmplen = snprintf(NULL, 0, "%d %s=%lld.%09ld\n", reclen, - keyword, (long long)value->tv_sec, (long)value->tv_nsec); + tmplen = snprintf(NULL, 0, "%d %s=%lld%s\n", reclen, + keyword, (long long)value->tv_sec, frac); } while (tmplen >= 0 && tmplen != reclen); if (tmplen < 0) return -1; @@ -999,8 +1014,8 @@ xheader_add_ts(struct xheader *xhdr, const char *keyword, if (rec == NULL) return -1; rec->reclen = reclen; - if (asprintf(&s, "%d %s=%lld.%09ld\n", reclen, keyword, - (long long)value->tv_sec, (long)value->tv_nsec) < 0) { + if (asprintf(&s, "%d %s=%lld%s\n", reclen, keyword, + (long long)value->tv_sec, frac) < 0) { free(rec); return -1; } |