ls: add option to reverse the sort order
Now you can list directory contents ordered by mtime with the latest file displayed last in the output.
This commit is contained in:
parent
96c8c5f389
commit
3a3cd24092
3
ls.1
3
ls.1
@ -24,6 +24,9 @@ print the index number of each file.
|
|||||||
lists detailed information about each file, including their type, permissions,
|
lists detailed information about each file, including their type, permissions,
|
||||||
links, owner, group, size, and modification time.
|
links, owner, group, size, and modification time.
|
||||||
.TP
|
.TP
|
||||||
|
.B \-r
|
||||||
|
reverses the sort order.
|
||||||
|
.TP
|
||||||
.B \-t
|
.B \-t
|
||||||
sorts files by modification time instead of by name.
|
sorts files by modification time instead of by name.
|
||||||
.TP
|
.TP
|
||||||
|
8
ls.c
8
ls.c
@ -35,6 +35,7 @@ static bool iflag = false;
|
|||||||
static bool lflag = false;
|
static bool lflag = false;
|
||||||
static bool tflag = false;
|
static bool tflag = false;
|
||||||
static bool Uflag = false;
|
static bool Uflag = false;
|
||||||
|
static int sortorder = 1;
|
||||||
static bool first = true;
|
static bool first = true;
|
||||||
static bool many;
|
static bool many;
|
||||||
|
|
||||||
@ -63,6 +64,9 @@ main(int argc, char *argv[])
|
|||||||
case 'l':
|
case 'l':
|
||||||
lflag = true;
|
lflag = true;
|
||||||
break;
|
break;
|
||||||
|
case 'r':
|
||||||
|
sortorder = -1;
|
||||||
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
tflag = true;
|
tflag = true;
|
||||||
break;
|
break;
|
||||||
@ -94,9 +98,9 @@ entcmp(const void *va, const void *vb)
|
|||||||
const Entry *a = va, *b = vb;
|
const Entry *a = va, *b = vb;
|
||||||
|
|
||||||
if(tflag)
|
if(tflag)
|
||||||
return b->mtime - a->mtime;
|
return sortorder * (b->mtime - a->mtime);
|
||||||
else
|
else
|
||||||
return strcmp(a->name, b->name);
|
return sortorder * (strcmp(a->name, b->name));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
Reference in New Issue
Block a user