implement -h(umanize).

from David P. Reese Jr. in PR bin/23870.
This commit is contained in:
grant 2003-12-26 06:19:19 +00:00
parent 1f6b42b329
commit f4c4780250
4 changed files with 129 additions and 41 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: ls.1,v 1.46 2003/09/22 06:01:43 wiz Exp $
.\" $NetBSD: ls.1,v 1.47 2003/12/26 06:19:19 grant Exp $
.\"
.\" Copyright (c) 1980, 1990, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
@ -32,7 +32,7 @@
.\"
.\" @(#)ls.1 8.7 (Berkeley) 7/29/94
.\"
.Dd September 22, 2003
.Dd December 26, 2003
.Dt LS 1
.Os
.Sh NAME
@ -40,7 +40,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm
.Op Fl AaBbCcdFfgikLlmnopqRrSsTtuWwx1
.Op Fl AaBbCcdFfghikLlmnopqRrSsTtuWwx1
.Op Ar
.Sh DESCRIPTION
For each operand that names a
@ -117,6 +117,13 @@ Output is not sorted.
The same as
.Fl l ,
except that the owner is not printed.
.It Fl h
Modifies the
.Fl s
and
.Fl l
options, causing the sizes to be reported in bytes displayed in a human
readable format.
.It Fl i
For each file, print the file's file serial number (inode number).
.It Fl k

View File

@ -1,4 +1,4 @@
/* $NetBSD: ls.c,v 1.52 2003/09/22 02:43:20 jschauma Exp $ */
/* $NetBSD: ls.c,v 1.53 2003/12/26 06:19:19 grant Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
#else
__RCSID("$NetBSD: ls.c,v 1.52 2003/09/22 02:43:20 jschauma Exp $");
__RCSID("$NetBSD: ls.c,v 1.53 2003/12/26 06:19:19 grant Exp $");
#endif
#endif /* not lint */
@ -88,6 +88,7 @@ int f_column; /* columnated format */
int f_columnacross; /* columnated format, sorted across */
int f_flags; /* show flags associated with a file */
int f_grouponly; /* long listing without owner */
int f_humanize; /* humanize the size field */
int f_inode; /* print inode */
int f_listdir; /* list actual directory, not contents */
int f_listdot; /* list files beginning with . */
@ -135,7 +136,7 @@ ls_main(int argc, char *argv[])
f_listdot = 1;
fts_options = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "1ABCFLRSTWabcdfgiklmnopqrstuwx")) != -1) {
while ((ch = getopt(argc, argv, "1ABCFLRSTWabcdfghiklmnopqrstuwx")) != -1) {
switch (ch) {
/*
* The -1, -C, -l, -m and -x options all override each other so
@ -223,6 +224,13 @@ ls_main(int argc, char *argv[])
blocksize = 1024;
kflag = 1;
break;
/*
* The -h option forces all sizes to be measured in bytes.
* It also makes -l suppress -s.
*/
case 'h':
f_humanize = 1;
break;
case 'n':
f_numericonly = 1;
break;
@ -442,7 +450,7 @@ display(FTSENT *p, FTSENT *list)
DISPLAY d;
FTSENT *cur;
NAMES *np;
u_int64_t btotal, maxblock, maxsize;
u_int64_t btotal, stotal, maxblock, maxsize;
int maxinode, maxnlink, maxmajor, maxminor;
int bcfile, entries, flen, glen, ulen, maxflags, maxgroup, maxlen;
int maxuser, needstats;
@ -471,7 +479,7 @@ display(FTSENT *p, FTSENT *list)
maxinode = maxnlink = 0;
bcfile = 0;
maxuser = maxgroup = maxflags = maxlen = 0;
btotal = maxblock = maxsize = 0;
btotal = stotal = maxblock = maxsize = 0;
maxmajor = maxminor = 0;
for (cur = list, entries = 0; cur; cur = cur->fts_link) {
if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) {
@ -520,6 +528,7 @@ display(FTSENT *p, FTSENT *list)
}
btotal += sp->st_blocks;
stotal += sp->st_size;
if (f_longform) {
if (f_numericonly ||
(user = user_from_uid(sp->st_uid, 0)) ==
@ -574,17 +583,24 @@ display(FTSENT *p, FTSENT *list)
d.maxlen = maxlen;
if (needstats) {
d.btotal = btotal;
(void)snprintf(buf, sizeof(buf), "%llu",
(long long)howmany(maxblock, blocksize));
d.s_block = strlen(buf);
d.stotal = stotal;
if (!f_humanize) {
(void)snprintf(buf, sizeof(buf), "%llu",
(long long)howmany(maxblock, blocksize));
d.s_block = strlen(buf);
}
d.s_flags = maxflags;
d.s_group = maxgroup;
(void)snprintf(buf, sizeof(buf), "%u", maxinode);
d.s_inode = strlen(buf);
(void)snprintf(buf, sizeof(buf), "%u", maxnlink);
d.s_nlink = strlen(buf);
(void)snprintf(buf, sizeof(buf), "%llu", (long long)maxsize);
d.s_size = strlen(buf);
if (f_humanize) {
d.s_size = 4; /* min buffer length for humanize_number */
} else {
(void)snprintf(buf, sizeof(buf), "%llu", (long long)maxsize);
d.s_size = strlen(buf);
}
d.s_user = maxuser;
if (bcfile) {
(void)snprintf(buf, sizeof(buf), "%u", maxmajor);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ls.h,v 1.15 2003/09/22 02:43:20 jschauma Exp $ */
/* $NetBSD: ls.h,v 1.16 2003/12/26 06:19:19 grant Exp $ */
/*
* Copyright (c) 1989, 1993
@ -41,6 +41,7 @@ extern long blocksize; /* block size units */
extern int f_accesstime; /* use time of last access */
extern int f_flags; /* show flags associated with a file */
extern int f_grouponly; /* long listing without owner */
extern int f_humanize; /* humanize size field */
extern int f_inode; /* print inode */
extern int f_longform; /* long listing format */
extern int f_octal; /* print octal escapes for nongraphic characters */
@ -55,6 +56,7 @@ extern int f_nonprint; /* show unprintables as ? */
typedef struct {
FTSENT *list;
u_int64_t btotal;
u_int64_t stotal;
int entries;
int maxlen;
int s_block;

View File

@ -1,4 +1,4 @@
/* $NetBSD: print.c,v 1.37 2003/09/22 02:43:20 jschauma Exp $ */
/* $NetBSD: print.c,v 1.38 2003/12/26 06:19:19 grant Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
#else
__RCSID("$NetBSD: print.c,v 1.37 2003/09/22 02:43:20 jschauma Exp $");
__RCSID("$NetBSD: print.c,v 1.38 2003/12/26 06:19:19 grant Exp $");
#endif
#endif /* not lint */
@ -55,6 +55,7 @@ __RCSID("$NetBSD: print.c,v 1.37 2003/09/22 02:43:20 jschauma Exp $");
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
#include <util.h>
#include "ls.h"
#include "extern.h"
@ -89,13 +90,22 @@ printlong(DISPLAY *dp)
struct stat *sp;
FTSENT *p;
NAMES *np;
char buf[20];
char buf[20], szbuf[5];
now = time(NULL);
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
(void)printf("total %llu\n",
(long long)(howmany(dp->btotal, blocksize)));
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
if (f_humanize) {
if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
"", HN_AUTOSCALE,
(HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
err(1, "humanize_number");
(void)printf("total %s\n", szbuf);
} else {
(void)printf("total %llu\n",
(long long)(howmany(dp->btotal, blocksize)));
}
}
for (p = dp->list; p; p = p->fts_link) {
if (IS_NOPRINT(p))
@ -104,9 +114,10 @@ printlong(DISPLAY *dp)
if (f_inode)
(void)printf("%*lu ", dp->s_inode,
(unsigned long)sp->st_ino);
if (f_size)
if (f_size && !f_humanize) {
(void)printf("%*llu ", dp->s_block,
(long long)howmany(sp->st_blocks, blocksize));
}
(void)strmode(sp->st_mode, buf);
np = p->fts_pointer;
(void)printf("%s %*lu ", buf, dp->s_nlink,
@ -121,8 +132,16 @@ printlong(DISPLAY *dp)
dp->s_major, major(sp->st_rdev), dp->s_minor,
minor(sp->st_rdev));
else
(void)printf("%*llu ", dp->s_size,
(long long)sp->st_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");
(void)printf("%*s ", dp->s_size, szbuf);
} else {
(void)printf("%*llu ", dp->s_size,
(long long)sp->st_size);
}
if (f_accesstime)
printtime(sp->st_atime);
else if (f_statustime)
@ -152,12 +171,17 @@ printcol(DISPLAY *dp)
FTSENT *p;
int base, chcnt, col, colwidth, num;
int numcols, numrows, row;
char szbuf[5];
colwidth = dp->maxlen;
if (f_inode)
colwidth += dp->s_inode + 1;
if (f_size)
colwidth += dp->s_block + 1;
if (f_size) {
if (f_humanize)
colwidth += dp->s_size + 1;
else
colwidth += dp->s_block + 1;
}
if (f_type || f_typedir)
colwidth += 1;
@ -190,13 +214,22 @@ printcol(DISPLAY *dp)
if (num % numcols)
++numrows;
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
(void)printf("total %llu\n",
(long long)(howmany(dp->btotal, blocksize)));
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
if (f_humanize) {
if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
"", HN_AUTOSCALE,
(HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
err(1, "humanize_number");
(void)printf("total %s\n", szbuf);
} else {
(void)printf("total %llu\n",
(long long)(howmany(dp->btotal, blocksize)));
}
}
for (row = 0; row < numrows; ++row) {
for (base = row, chcnt = col = 0; col < numcols; ++col) {
chcnt = printaname(array[base], dp->s_inode,
dp->s_block);
f_humanize ? dp->s_size : dp->s_block);
if ((base += numrows) >= num)
break;
while (chcnt++ < colwidth)
@ -212,12 +245,17 @@ printacol(DISPLAY *dp)
FTSENT *p;
int chcnt, col, colwidth;
int numcols;
char szbuf[5];
colwidth = dp->maxlen;
if (f_inode)
colwidth += dp->s_inode + 1;
if (f_size)
colwidth += dp->s_block + 1;
if (f_size) {
if (f_humanize)
colwidth += dp->s_size + 1;
else
colwidth += dp->s_block + 1;
}
if (f_type || f_typedir)
colwidth += 1;
@ -231,9 +269,18 @@ printacol(DISPLAY *dp)
numcols = termwidth / colwidth;
colwidth = termwidth / numcols; /* spread out if possible */
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
(void)printf("total %llu\n",
(long long)(howmany(dp->btotal, blocksize)));
if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
if (f_humanize) {
if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
"", HN_AUTOSCALE,
(HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
err(1, "humanize_number");
(void)printf("total %s\n", szbuf);
} else {
(void)printf("total %llu\n",
(long long)(howmany(dp->btotal, blocksize)));
}
}
chcnt = col = 0;
for (p = dp->list; p; p = p->fts_link) {
if (IS_NOPRINT(p))
@ -242,7 +289,8 @@ printacol(DISPLAY *dp)
chcnt = col = 0;
(void)putchar('\n');
}
chcnt = printaname(p, dp->s_inode, dp->s_block);
chcnt = printaname(p, dp->s_inode,
f_humanize ? dp->s_size : dp->s_block);
while (chcnt++ < colwidth)
(void)putchar(' ');
col++;
@ -260,8 +308,12 @@ printstream(DISPLAY *dp)
extwidth = 0;
if (f_inode)
extwidth += dp->s_inode + 1;
if (f_size)
extwidth += dp->s_block + 1;
if (f_size) {
if (f_humanize)
extwidth += dp->s_size + 1;
else
extwidth += dp->s_block + 1;
}
if (f_type)
extwidth += 1;
@ -275,7 +327,8 @@ printstream(DISPLAY *dp)
else
(void)putchar(' '), col++;
}
col += printaname(p, dp->s_inode, dp->s_block);
col += printaname(p, dp->s_inode,
f_humanize ? dp->s_size : dp->s_block);
}
(void)putchar('\n');
}
@ -289,14 +342,24 @@ printaname(FTSENT *p, int inodefield, int sizefield)
{
struct stat *sp;
int chcnt;
char szbuf[5];
sp = p->fts_statp;
chcnt = 0;
if (f_inode)
chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
if (f_size)
chcnt += printf("%*llu ", sizefield,
(long long)howmany(sp->st_blocks, blocksize));
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);
} else {
chcnt += printf("%*llu ", sizefield,
(long long)howmany(sp->st_blocks, blocksize));
}
}
if (f_octal || f_octal_escape)
chcnt += safe_print(p->fts_name);
else if (f_nonprint)