ls: add -h flag
for util/human don't show "B" for bytes.
This commit is contained in:
parent
2cf82f4c16
commit
4d4e2608c1
3
ls.1
3
ls.1
|
@ -20,6 +20,9 @@ lists directories themselves, not their contents.
|
||||||
.B \-F
|
.B \-F
|
||||||
append a file type indicator to files.
|
append a file type indicator to files.
|
||||||
.TP
|
.TP
|
||||||
|
.B \-h
|
||||||
|
show filesizes in human\-readable format.
|
||||||
|
.TP
|
||||||
.B \-i
|
.B \-i
|
||||||
print the index number of each file.
|
print the index number of each file.
|
||||||
.TP
|
.TP
|
||||||
|
|
14
ls.c
14
ls.c
|
@ -32,6 +32,7 @@ static void output(Entry *);
|
||||||
static bool aflag = false;
|
static bool aflag = false;
|
||||||
static bool dflag = false;
|
static bool dflag = false;
|
||||||
static bool Fflag = false;
|
static bool Fflag = false;
|
||||||
|
static bool hflag = false;
|
||||||
static bool iflag = false;
|
static bool iflag = false;
|
||||||
static bool lflag = false;
|
static bool lflag = false;
|
||||||
static bool rflag = false;
|
static bool rflag = false;
|
||||||
|
@ -43,7 +44,7 @@ static bool many;
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-1adFilrtU] [FILE...]\n", argv0);
|
eprintf("usage: %s [-1adFhilrtU] [FILE...]\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -65,6 +66,9 @@ main(int argc, char *argv[])
|
||||||
case 'F':
|
case 'F':
|
||||||
Fflag = true;
|
Fflag = true;
|
||||||
break;
|
break;
|
||||||
|
case 'h':
|
||||||
|
hflag = true;
|
||||||
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
iflag = true;
|
iflag = true;
|
||||||
break;
|
break;
|
||||||
|
@ -282,8 +286,12 @@ output(Entry *ent)
|
||||||
fmt = "%b %d %H:%M";
|
fmt = "%b %d %H:%M";
|
||||||
|
|
||||||
strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
|
strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
|
||||||
printf("%s %4ld %-8.8s %-8.8s %10lu %s %s%s", mode, (long)ent->nlink, pwname,
|
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
|
||||||
grname, (unsigned long)ent->size, buf, ent->name, indicator(ent->mode));
|
if(hflag)
|
||||||
|
printf("%10s ", humansize((unsigned long)ent->size));
|
||||||
|
else
|
||||||
|
printf("%10lu ", (unsigned long)ent->size);
|
||||||
|
printf("%s %s%s", buf, ent->name, indicator(ent->mode));
|
||||||
if(S_ISLNK(ent->mode)) {
|
if(S_ISLNK(ent->mode)) {
|
||||||
if((len = readlink(ent->name, buf, sizeof buf)) == -1)
|
if((len = readlink(ent->name, buf, sizeof buf)) == -1)
|
||||||
eprintf("readlink %s:", ent->name);
|
eprintf("readlink %s:", ent->name);
|
||||||
|
|
|
@ -7,14 +7,14 @@ char *
|
||||||
humansize(double n)
|
humansize(double n)
|
||||||
{
|
{
|
||||||
static char buf[16];
|
static char buf[16];
|
||||||
const char postfixes[] = " KMGTPE";
|
const char postfixes[] = "BKMGTPE";
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for(i = 0; n >= 1024 && i < strlen(postfixes); i++)
|
for(i = 0; n >= 1024 && i < strlen(postfixes); i++)
|
||||||
n /= 1024;
|
n /= 1024;
|
||||||
|
|
||||||
if(!i)
|
if(!i)
|
||||||
snprintf(buf, sizeof(buf), "%lu%c", (unsigned long)n, postfixes[i]);
|
snprintf(buf, sizeof(buf), "%lu", (unsigned long)n);
|
||||||
else
|
else
|
||||||
snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]);
|
snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]);
|
||||||
return buf;
|
return buf;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user