summaryrefslogtreecommitdiffstats
path: root/bin/ls/print.c
diff options
context:
space:
mode:
authorjh <jh@FreeBSD.org>2010-01-24 19:23:07 +0000
committerjh <jh@FreeBSD.org>2010-01-24 19:23:07 +0000
commit5b71974232932d6b97061977abe1f7766cf0e61a (patch)
tree4064131b3657eef1b406048c69142445673077d2 /bin/ls/print.c
parent664f8ff8b9cea7c7266e8d170ef88f32d4911b1a (diff)
downloadFreeBSD-src-5b71974232932d6b97061977abe1f7766cf0e61a.zip
FreeBSD-src-5b71974232932d6b97061977abe1f7766cf0e61a.tar.gz
Fixes for ls(1) long format (-l) output:
- Allow -h option to work if the listing contains at least one device file. - Align major and minor device numbers correctly to the size field. PR: bin/125678 Approved by: trasz (mentor) MFC after: 1 month
Diffstat (limited to 'bin/ls/print.c')
-rw-r--r--bin/ls/print.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/bin/ls/print.c b/bin/ls/print.c
index 3b80d93..3f2033c 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$");
#include "extern.h"
static int printaname(const FTSENT *, u_long, u_long);
+static void printdev(size_t, dev_t);
static void printlink(const FTSENT *);
static void printtime(time_t);
static int printtype(u_int);
@@ -165,16 +166,7 @@ printlong(const DISPLAY *dp)
if (f_label)
(void)printf("%-*s ", dp->s_label, np->label);
if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
- if (minor(sp->st_rdev) > 255 || minor(sp->st_rdev) < 0)
- (void)printf("%3d, 0x%08x ",
- major(sp->st_rdev),
- (u_int)minor(sp->st_rdev));
- else
- (void)printf("%3d, %3d ",
- major(sp->st_rdev), minor(sp->st_rdev));
- else if (dp->bcfile)
- (void)printf("%*s%*jd ",
- 8 - dp->s_size, "", dp->s_size, sp->st_size);
+ printdev(dp->s_size, sp->st_rdev);
else
printsize(dp->s_size, sp->st_size);
if (f_accesstime)
@@ -353,6 +345,24 @@ printaname(const FTSENT *p, u_long inodefield, u_long sizefield)
return (chcnt);
}
+/*
+ * Print device special file major and minor numbers.
+ */
+static void
+printdev(size_t width, dev_t dev)
+{
+ char buf[DEVSTR_HEX_LEN + 1];
+
+ if (minor(dev) > 255 || minor(dev) < 0)
+ (void)snprintf(buf, sizeof(buf), "%3d, 0x%08x",
+ major(dev), (u_int)minor(dev));
+ else
+ (void)snprintf(buf, sizeof(buf), "%3d, %3d",
+ major(dev), minor(dev));
+
+ (void)printf("%*s ", (u_int)width, buf);
+}
+
static void
printtime(time_t ftime)
{
@@ -592,11 +602,15 @@ printsize(size_t width, off_t bytes)
{
if (f_humanval) {
- char buf[5];
+ /*
+ * Reserve one space before the size and allocate room for
+ * the trailing '\0'.
+ */
+ char buf[HUMANVALSTR_LEN - 1 + 1];
humanize_number(buf, sizeof(buf), (int64_t)bytes, "",
HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
- (void)printf("%5s ", buf);
+ (void)printf("%*s ", (u_int)width, buf);
} else
(void)printf("%*jd ", (u_int)width, bytes);
}
OpenPOWER on IntegriCloud