ls: group sorting options
tflag and Sflag are now grouped under 'sort' var, as suggested by cls. r-flag handling has been moved inside entcmp().
This commit is contained in:
parent
f1bbe6529a
commit
365e1a324a
35
ls.c
35
ls.c
|
@ -45,12 +45,11 @@ static int nflag = 0;
|
||||||
static int pflag = 0;
|
static int pflag = 0;
|
||||||
static int qflag = 0;
|
static int qflag = 0;
|
||||||
static int Rflag = 0;
|
static int Rflag = 0;
|
||||||
static int Sflag = 0;
|
|
||||||
static int rflag = 0;
|
static int rflag = 0;
|
||||||
static int tflag = 0;
|
|
||||||
static int Uflag = 0;
|
static int Uflag = 0;
|
||||||
static int uflag = 0;
|
static int uflag = 0;
|
||||||
static int first = 1;
|
static int first = 1;
|
||||||
|
static char sort = 0;
|
||||||
|
|
||||||
static void ls(const char *, const struct entry *, int);
|
static void ls(const char *, const struct entry *, int);
|
||||||
|
|
||||||
|
@ -217,12 +216,19 @@ entcmp(const void *va, const void *vb)
|
||||||
int cmp = 0;
|
int cmp = 0;
|
||||||
const struct entry *a = va, *b = vb;
|
const struct entry *a = va, *b = vb;
|
||||||
|
|
||||||
if (Sflag)
|
switch (sort) {
|
||||||
|
case 'S':
|
||||||
cmp = b->size - a->size;
|
cmp = b->size - a->size;
|
||||||
else if (tflag)
|
break;
|
||||||
|
case 't':
|
||||||
cmp = b->t - a->t;
|
cmp = b->t - a->t;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return cmp ? cmp : strcmp(a->name, b->name);
|
if (!cmp)
|
||||||
|
cmp = strcmp(a->name, b->name);
|
||||||
|
|
||||||
|
return rflag ? 0 - cmp : cmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -249,7 +255,7 @@ lsdir(const char *path, const struct entry *dir)
|
||||||
|
|
||||||
ents = ereallocarray(ents, ++n, sizeof(*ents));
|
ents = ereallocarray(ents, ++n, sizeof(*ents));
|
||||||
mkent(&ents[n - 1], estrdup(d->d_name), Fflag || iflag ||
|
mkent(&ents[n - 1], estrdup(d->d_name), Fflag || iflag ||
|
||||||
lflag || pflag || Rflag || Sflag, Lflag);
|
lflag || pflag || Rflag || sort, Lflag);
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
|
@ -260,7 +266,7 @@ lsdir(const char *path, const struct entry *dir)
|
||||||
if (path[0] || dir->name[0] != '.')
|
if (path[0] || dir->name[0] != '.')
|
||||||
printf("%s:\n", dir->name);
|
printf("%s:\n", dir->name);
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
output(&ents[rflag ? (n - 1 - i) : i]);
|
output(&ents[i]);
|
||||||
|
|
||||||
if (Rflag) {
|
if (Rflag) {
|
||||||
if (snprintf(prefix, PATH_MAX, "%s%s/", path, dir->name) >=
|
if (snprintf(prefix, PATH_MAX, "%s%s/", path, dir->name) >=
|
||||||
|
@ -268,7 +274,7 @@ lsdir(const char *path, const struct entry *dir)
|
||||||
eprintf("path too long: %s%s\n", path, dir->name);
|
eprintf("path too long: %s%s\n", path, dir->name);
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
ent = &ents[rflag ? (n - 1 - i) : i];
|
ent = &ents[i];
|
||||||
if (strcmp(ent->name, ".") == 0 ||
|
if (strcmp(ent->name, ".") == 0 ||
|
||||||
strcmp(ent->name, "..") == 0)
|
strcmp(ent->name, "..") == 0)
|
||||||
continue;
|
continue;
|
||||||
|
@ -403,16 +409,13 @@ main(int argc, char *argv[])
|
||||||
Rflag = 1;
|
Rflag = 1;
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
if (!fflag)
|
rflag = 1;
|
||||||
rflag = 1;
|
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
Sflag = 1;
|
sort = 'S';
|
||||||
tflag = 0;
|
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
Sflag = 0;
|
sort = 't';
|
||||||
tflag = 1;
|
|
||||||
break;
|
break;
|
||||||
case 'U':
|
case 'U':
|
||||||
Uflag = 1;
|
Uflag = 1;
|
||||||
|
@ -456,11 +459,11 @@ main(int argc, char *argv[])
|
||||||
qsort(dents, ds, sizeof(ent), entcmp);
|
qsort(dents, ds, sizeof(ent), entcmp);
|
||||||
|
|
||||||
for (i = 0; i < fs; ++i)
|
for (i = 0; i < fs; ++i)
|
||||||
ls("", fents[rflag ? (fs - i - 1) : i], 0);
|
ls("", fents[i], 0);
|
||||||
if (fs && ds)
|
if (fs && ds)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
for (i = 0; i < ds; ++i)
|
for (i = 0; i < ds; ++i)
|
||||||
ls("", dents[rflag ? (ds - i - 1) : i], 1);
|
ls("", dents[i], 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fshut(stdout, "<stdout>");
|
return fshut(stdout, "<stdout>");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user