summaryrefslogtreecommitdiff
path: root/bin/ls/print.c
diff options
context:
space:
mode:
Diffstat (limited to 'bin/ls/print.c')
-rw-r--r--bin/ls/print.c84
1 files changed, 42 insertions, 42 deletions
diff --git a/bin/ls/print.c b/bin/ls/print.c
index b3aecd8..8e62329 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -32,15 +32,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
-#else
-__RCSID("$NetBSD: print.c,v 1.55 2014/05/10 09:39:18 martin Exp $");
-#endif
-#endif /* not lint */
-
#include <sys/param.h>
#include <sys/stat.h>
@@ -49,14 +40,15 @@ __RCSID("$NetBSD: print.c,v 1.55 2014/05/10 09:39:18 martin Exp $");
#include <inttypes.h>
#include <fts.h>
#include <grp.h>
+#include <math.h>
#include <pwd.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <bsd/string.h>
#include <string.h>
#include <time.h>
-#include <tzfile.h>
#include <unistd.h>
-#include <util.h>
#include "ls.h"
#include "extern.h"
@@ -119,13 +111,45 @@ printscol(DISPLAY *dp)
}
}
+
+char *scale[] = {
+ "",
+ "k",
+ "M",
+ "G",
+ "T",
+ "P",
+ "E",
+ "Z",
+ "Y"
+};
+
+char *humanize(uint64_t size, bool si) {
+ uint16_t unit = si ? 1000 : 1024;
+ int8_t radix = (int8_t)(log(size)/log(unit));
+ char suffix[2], ret[6];
+ double val;
+ uint64_t temp = size;
+
+ if (radix > 9) {
+ radix = 9;
+ }
+
+ val = size / (double)pow(unit, radix);
+
+ snprintf(ret, 6, "%.0f%s", val, scale[radix]);
+
+ return strndup(ret, 6);
+}
+
+
void
printlong(DISPLAY *dp)
{
struct stat *sp;
FTSENT *p;
NAMES *np;
- char buf[20], szbuf[5];
+ char buf[20];
now = time(NULL);
@@ -140,12 +164,7 @@ printlong(DISPLAY *dp)
(void)printf("%*"PRIu64" ", dp->s_inode, sp->st_ino);
if (f_size) {
if (f_humanize) {
- if ((humanize_number(szbuf, sizeof(szbuf),
- sp->st_blocks * S_BLKSIZE,
- "", HN_AUTOSCALE,
- (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
- err(1, "humanize_number");
- (void)printf("%*s ", dp->s_block, szbuf);
+ printf("%*s ", 10, humanize(sp->st_size, false));
} else {
(void)printf(f_commas ? "%'*llu " : "%*llu ",
dp->s_block,
@@ -160,19 +179,13 @@ printlong(DISPLAY *dp)
if (!f_grouponly)
(void)printf("%-*s ", dp->s_user, np->user);
(void)printf("%-*s ", dp->s_group, np->group);
- if (f_flags)
- (void)printf("%-*s ", dp->s_flags, np->flags);
if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
(void)printf("%*lld, %*lld ",
dp->s_major, (long long)major(sp->st_rdev),
dp->s_minor, (long long)minor(sp->st_rdev));
else
if (f_humanize) {
- if ((humanize_number(szbuf, sizeof(szbuf),
- sp->st_size, "", HN_AUTOSCALE,
- (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
- err(1, "humanize_number");
- (void)printf("%*s ", dp->s_size, szbuf);
+ (void)printf("%*s ", 10, humanize(sp->st_size, false));
} else {
(void)printf(f_commas ? "%'*llu " : "%*llu ",
dp->s_size, (unsigned long long)
@@ -360,7 +373,6 @@ printaname(FTSENT *p, int inodefield, int sizefield)
{
struct stat *sp;
int chcnt;
- char szbuf[5];
sp = p->fts_statp;
chcnt = 0;
@@ -368,11 +380,7 @@ printaname(FTSENT *p, int inodefield, int sizefield)
chcnt += printf("%*"PRIu64" ", inodefield, sp->st_ino);
if (f_size) {
if (f_humanize) {
- if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
- "", HN_AUTOSCALE,
- (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
- err(1, "humanize_number");
- chcnt += printf("%*s ", sizefield, szbuf);
+ chcnt += printf("%*s ", 10, humanize(sp->st_size, false));
} else {
chcnt += printf(f_commas ? "%'*llu " : "%*llu ",
sizefield, (unsigned long long)
@@ -402,7 +410,8 @@ printtime(time_t ftime)
}
for (i = 4; i < 11; ++i)
(void)putchar(longstring[i]);
-
+#define DAYSPERNYEAR 365
+#define SECSPERDAY 24*60*60
#define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
if (f_sectime)
for (i = 11; i < 24; i++)
@@ -427,15 +436,9 @@ printtime(time_t ftime)
static void
printtotal(DISPLAY *dp)
{
- char szbuf[5];
-
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
if (f_humanize) {
- if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal,
- "", HN_AUTOSCALE,
- (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
- err(1, "humanize_number");
- (void)printf("total %s\n", szbuf);
+ printf("total %s\n", humanize(dp->stotal, false));
} else {
(void)printf(f_commas ? "total %'llu\n" :
"total %llu\n", (unsigned long long)
@@ -460,9 +463,6 @@ printtype(u_int mode)
case S_IFSOCK:
(void)putchar('=');
return (1);
- case S_IFWHT:
- (void)putchar('%');
- return (1);
}
if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
(void)putchar('*');