summaryrefslogtreecommitdiff
path: root/usr.bin/stat/stat.c
diff options
context:
space:
mode:
authoratatat <atatat@NetBSD.org>2005-06-23 03:13:24 +0000
committeratatat <atatat@NetBSD.org>2005-06-23 03:13:24 +0000
commit277396bd87bdd2798c8114fec80392e2e18c9c26 (patch)
tree7b1681f83988a51bde3455415dfc5641c57c962a /usr.bin/stat/stat.c
parenta30f20fce847338469a4ed8c022617903c079606 (diff)
Bend the 'L', 'M', and 'H' modifiers to work on the size, whereby it
is rounded to the nearest kilobyte, megabyte, or gigabyte. Implemented at lukem's request since some things can't deal with overly large numbers when files are really large. Have to do something like humanize_number(3), but that interface isn't really what I'm looking for. I think. More examination required.
Diffstat (limited to 'usr.bin/stat/stat.c')
-rw-r--r--usr.bin/stat/stat.c33
1 files changed, 29 insertions, 4 deletions
diff --git a/usr.bin/stat/stat.c b/usr.bin/stat/stat.c
index 3fa5ac28378..e2bb3ccfeed 100644
--- a/usr.bin/stat/stat.c
+++ b/usr.bin/stat/stat.c
@@ -1,4 +1,4 @@
-/* $NetBSD: stat.c,v 1.22 2005/04/22 03:36:48 atatat Exp $ */
+/* $NetBSD: stat.c,v 1.23 2005/06/23 03:13:24 atatat Exp $ */
/*
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -42,7 +42,7 @@
#include <sys/cdefs.h>
#if !defined(lint)
-__RCSID("$NetBSD: stat.c,v 1.22 2005/04/22 03:36:48 atatat Exp $");
+__RCSID("$NetBSD: stat.c,v 1.23 2005/06/23 03:13:24 atatat Exp $");
#endif
#if ! HAVE_NBTOOL_CONFIG_H
@@ -560,13 +560,14 @@ format1(const struct stat *st,
struct tm *tm;
time_t secs;
long nsecs;
- int l, small, formats, gottime;
+ int l, small, formats, gottime, shift;
formats = 0;
small = 0;
gottime = 0;
secs = 0;
nsecs = 0;
+ shift = 0;
/*
* First, pick out the data and tweak it based on hilo or
@@ -732,6 +733,20 @@ format1(const struct stat *st,
formats = FMTF_DECIMAL | FMTF_OCTAL | FMTF_UNSIGNED | FMTF_HEX;
if (ofmt == 0)
ofmt = FMTF_UNSIGNED;
+ switch (hilo) {
+ case HIGH_PIECE:
+ shift = 30; /* gigabytes */
+ hilo = 0;
+ break;
+ case MIDDLE_PIECE:
+ shift = 20; /* megabytes */
+ hilo = 0;
+ break;
+ case LOW_PIECE:
+ shift = 10; /* kilobytes */
+ hilo = 0;
+ break;
+ }
break;
case SHOW_st_blocks:
small = (sizeof(st->st_blocks) == 4);
@@ -1045,10 +1060,20 @@ format1(const struct stat *st,
(void)strcat(lfmt, "ll");
switch (ofmt) {
case FMTF_DECIMAL: (void)strcat(lfmt, "d"); break;
- case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
+ case FMTF_OCTAL: (void)strcat(lfmt, "o"); break;
case FMTF_UNSIGNED: (void)strcat(lfmt, "u"); break;
case FMTF_HEX: (void)strcat(lfmt, "x"); break;
}
+ /*
+ * shift and round to nearest for kilobytes, megabytes,
+ * gigabytes.
+ */
+ if (shift > 0) {
+ data >>= (shift - 1);
+ data++;
+ data >>= 1;
+ }
+
return (snprintf(buf, blen, lfmt, data));
}