Add a new flag, -h which when combined with the -l option causes
file sizes to be displayed with unit suffixes; Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less. Submitted by: nik
This commit is contained in:
parent
c1e7a5f1e1
commit
0e8d1551e0
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=88591
@ -4,6 +4,7 @@
|
||||
|
||||
PROG= ls
|
||||
SRCS= cmp.c ls.c print.c util.c lomac.c
|
||||
LDADD= -lm
|
||||
WARNS= 0
|
||||
|
||||
.if !defined(RELEASE_CRUNCH)
|
||||
|
28
bin/ls/ls.1
28
bin/ls/ls.1
@ -43,7 +43,7 @@
|
||||
.Nd list directory contents
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Op Fl ABCFGHLPRTWZabcdfgiklnoqrstu1
|
||||
.Op Fl ABCFGHLPRTWZabcdfghiklnoqrstu1
|
||||
.Op Ar
|
||||
.Sh DESCRIPTION
|
||||
For each operand that names a
|
||||
@ -146,6 +146,12 @@ with
|
||||
it was used to display the group name in the long
|
||||
.Pq Fl l
|
||||
format output.
|
||||
.It Fl h
|
||||
When used wih the
|
||||
.Fl l
|
||||
option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte
|
||||
and Petabyte in order to reduce the number of digits to three or less
|
||||
using base 2 for sizes.
|
||||
.It Fl i
|
||||
For each file, print the file's file serial number (inode number).
|
||||
.It Fl k
|
||||
@ -586,3 +592,23 @@ specification.
|
||||
.Sh BUGS
|
||||
To maintain backward compatibility, the relationships between the many
|
||||
options are quite complex.
|
||||
|
||||
|
||||
|
||||
|
||||
***************
|
||||
*** 43,49 ****
|
||||
.Nd list directory contents
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
- .Op Fl ABCFGHLPRTWabcdfgiklnoqrstu1
|
||||
.Op Ar
|
||||
.Sh DESCRIPTION
|
||||
For each operand that names a
|
||||
--- 43,49 ----
|
||||
.Nd list directory contents
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Op Ar
|
||||
.Sh DESCRIPTION
|
||||
For each operand that names a
|
||||
|
@ -94,6 +94,7 @@ int termwidth = 80; /* default terminal width */
|
||||
int f_accesstime; /* use time of last access */
|
||||
int f_column; /* columnated format */
|
||||
int f_flags; /* show flags associated with a file */
|
||||
int f_humanval; /* show human-readable file sizes */
|
||||
int f_inode; /* print inode */
|
||||
int f_kblocks; /* print size in kilobytes */
|
||||
int f_listdir; /* list actual directory, not contents */
|
||||
@ -167,7 +168,7 @@ main(argc, argv)
|
||||
f_listdot = 1;
|
||||
|
||||
fts_options = FTS_PHYSICAL;
|
||||
while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfgiklnoqrstu")) != -1) {
|
||||
while ((ch = getopt(argc, argv, "1ABCFGHLPRTWZabcdfghiklnoqrstu")) != -1) {
|
||||
switch (ch) {
|
||||
/*
|
||||
* The -1, -C and -l options all override each other so shell
|
||||
@ -236,6 +237,9 @@ main(argc, argv)
|
||||
break;
|
||||
case 'g': /* Compatibility with 4.3BSD. */
|
||||
break;
|
||||
case 'h':
|
||||
f_humanval = 1;
|
||||
break;
|
||||
case 'i':
|
||||
f_inode = 1;
|
||||
break;
|
||||
|
@ -44,6 +44,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_humanval; /* show human-readable file sizes */
|
||||
extern int f_lomac; /* show LOMAC attributes */
|
||||
extern int f_inode; /* print inode */
|
||||
extern int f_longform; /* long listing format */
|
||||
|
@ -50,6 +50,7 @@ static const char rcsid[] =
|
||||
#include <errno.h>
|
||||
#include <fts.h>
|
||||
#include <grp.h>
|
||||
#include <math.h>
|
||||
#include <langinfo.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
@ -70,12 +71,33 @@ static int printaname __P((FTSENT *, u_long, u_long));
|
||||
static void printlink __P((FTSENT *));
|
||||
static void printtime __P((time_t));
|
||||
static int printtype __P((u_int));
|
||||
static void printsize __P((size_t, off_t));
|
||||
#ifdef COLORLS
|
||||
static void endcolor __P((int));
|
||||
static int colortype __P((mode_t));
|
||||
#endif
|
||||
|
||||
#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
|
||||
#define UNITS_2 2
|
||||
|
||||
#define KILO_SZ(n) (n)
|
||||
#define MEGA_SZ(n) ((n) * (n))
|
||||
#define GIGA_SZ(n) ((n) * (n) * (n))
|
||||
#define TERA_SZ(n) ((n) * (n) * (n) * (n))
|
||||
#define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
|
||||
|
||||
#define KILO_2_SZ (KILO_SZ(1024ULL))
|
||||
#define MEGA_2_SZ (MEGA_SZ(1024ULL))
|
||||
#define GIGA_2_SZ (GIGA_SZ(1024ULL))
|
||||
#define TERA_2_SZ (TERA_SZ(1024ULL))
|
||||
#define PETA_2_SZ (PETA_SZ(1024ULL))
|
||||
|
||||
unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
|
||||
|
||||
typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
|
||||
static unit_t unit_adjust __P((off_t *));
|
||||
|
||||
int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
|
||||
|
||||
#ifdef COLORLS
|
||||
/* Most of these are taken from <sys/stat.h> */
|
||||
@ -178,7 +200,7 @@ printlong(dp)
|
||||
(void)printf("%*s%*qd ",
|
||||
8 - dp->s_size, "", dp->s_size, sp->st_size);
|
||||
else
|
||||
(void)printf("%*qd ", dp->s_size, sp->st_size);
|
||||
printsize(dp->s_size, sp->st_size);
|
||||
if (f_accesstime)
|
||||
printtime(sp->st_atime);
|
||||
else if (f_statustime)
|
||||
@ -550,3 +572,51 @@ printlink(p)
|
||||
(void)printf(" -> ");
|
||||
printname(path);
|
||||
}
|
||||
|
||||
static void
|
||||
printsize(width, bytes)
|
||||
size_t width;
|
||||
off_t bytes;
|
||||
{
|
||||
unit_t unit;
|
||||
|
||||
if (f_humanval) {
|
||||
unit = unit_adjust(&bytes);
|
||||
|
||||
if (bytes == 0)
|
||||
(void)printf("%*s ", width, "0B");
|
||||
else
|
||||
(void)printf("%*qd%c ", width - 1, bytes,
|
||||
"BKMGTPE"[unit]);
|
||||
}
|
||||
else
|
||||
(void)printf("%*qd ", width, bytes);
|
||||
}
|
||||
|
||||
/*
|
||||
* Output in "human-readable" format. Uses 3 digits max and puts
|
||||
* unit suffixes at the end. Makes output compact and easy to read,
|
||||
* especially on huge disks.
|
||||
*
|
||||
*/
|
||||
unit_t
|
||||
unit_adjust(val)
|
||||
off_t *val;
|
||||
{
|
||||
double abval;
|
||||
unit_t unit;
|
||||
unsigned int unit_sz;
|
||||
|
||||
abval = fabs(*val);
|
||||
|
||||
unit_sz = abval ? ilogb(abval) / 10 : 0;
|
||||
|
||||
if (unit_sz >= UNIT_MAX) {
|
||||
unit = NONE;
|
||||
} else {
|
||||
unit = unitp[unit_sz];
|
||||
*val /= (double)vals_base2[unit_sz];
|
||||
}
|
||||
|
||||
return (unit);
|
||||
}
|
||||
|
@ -162,9 +162,9 @@ usage()
|
||||
{
|
||||
(void)fprintf(stderr,
|
||||
#ifdef COLORLS
|
||||
"usage: ls [-ABCFGHLPRTWZabcdfgiklnoqrstu1]"
|
||||
"usage: ls [-ABCFGHLPRTWZabcdfghiklnoqrstu1]"
|
||||
#else
|
||||
"usage: ls [-ABCFHLPRTWZabcdfgiklnoqrstu1]"
|
||||
"usage: ls [-ABCFHLPRTWZabcdfghiklnoqrstu1]"
|
||||
#endif
|
||||
" [file ...]\n");
|
||||
exit(1);
|
||||
|
Loading…
Reference in New Issue
Block a user