ls: rework implementation for reversing the sort order

Update the usage line as well.
This commit is contained in:
Markus Teich 2013-10-06 19:52:18 +01:00 committed by sin
parent 5e9c4d781b
commit 952fa19362

16
ls.c
View File

@ -33,16 +33,16 @@ static bool aflag = false;
static bool dflag = false; static bool dflag = false;
static bool iflag = false; static bool iflag = false;
static bool lflag = false; static bool lflag = false;
static bool rflag = 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;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-adiltU] [FILE...]\n", argv0); eprintf("usage: %s [-adilrtU] [FILE...]\n", argv0);
} }
int int
@ -65,7 +65,7 @@ main(int argc, char *argv[])
lflag = true; lflag = true;
break; break;
case 'r': case 'r':
sortorder = -1; rflag = true;
break; break;
case 't': case 't':
tflag = true; tflag = true;
@ -87,7 +87,7 @@ main(int argc, char *argv[])
mkent(&ents[i], argv[i], true); mkent(&ents[i], argv[i], true);
qsort(ents, argc, sizeof *ents, entcmp); qsort(ents, argc, sizeof *ents, entcmp);
for(i = 0; i < argc; i++) for(i = 0; i < argc; i++)
ls(&ents[i]); ls(&ents[rflag ? argc-i-1 : i]);
return 0; return 0;
} }
@ -98,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 sortorder * (b->mtime - a->mtime); return b->mtime - a->mtime;
else else
return sortorder * (strcmp(a->name, b->name)); return strcmp(a->name, b->name);
} }
void void
@ -155,8 +155,8 @@ lsdir(const char *path)
if(!Uflag){ if(!Uflag){
qsort(ents, n, sizeof *ents, entcmp); qsort(ents, n, sizeof *ents, entcmp);
for(i = 0; i < n; i++) { for(i = 0; i < n; i++) {
output(&ents[i]); output(&ents[rflag ? n-i-1 : i]);
free(ents[i].name); free(ents[rflag ? n-i-1 : i].name);
} }
} }
if(chdir(cwd) == -1) if(chdir(cwd) == -1)