[hackers] [sbase] ls: sort argv || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Thu, 16 Jun 2011 02:13:53 +0200 (CEST)

changeset: 80:0a539291e996
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Thu Jun 16 01:13:46 2011 +0100
files: ls.c
description:
ls: sort argv

diff -r d3d529413bd8 -r 0a539291e996 ls.c
--- a/ls.c Sat Jun 11 00:44:17 2011 +0100
+++ b/ls.c Thu Jun 16 01:13:46 2011 +0100
@@ -39,6 +39,8 @@
 main(int argc, char *argv[])
 {
         char c;
+ int i, n;
+ Entry *ents;
 
         while((c = getopt(argc, argv, "adlt")) != -1)
                 switch(c) {
@@ -59,10 +61,17 @@
                 }
         many = (argc > optind+1);
 
- if(optind == argc)
+ if((n = argc - optind) > 0) {
+ if(!(ents = malloc((argc-1) * sizeof *ents)))
+ eprintf("malloc:");
+ for(i = 0; i < n; i++)
+ mkent(&ents[i], argv[optind+i]);
+ qsort(ents, n, sizeof *ents, (int (*)(const void *, const void *))entcmp);
+ for(i = 0; i < n; i++)
+ ls(ents[i].name);
+ }
+ else
                 ls(".");
- else for(; optind < argc; optind++)
- ls(argv[optind]);
         return EXIT_SUCCESS;
 }
 
@@ -210,7 +219,7 @@
                 fmt = "%b %d %H:%M";
 
         strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
- printf("%s %2ld %s %s %6lu %s %s", mode, (long)ent->nlink, pw->pw_name,
+ printf("%s %2ld %-4s %-5s %6lu %s %s", mode, (long)ent->nlink, pw->pw_name,
                gr->gr_name, (unsigned long)ent->size, buf, ent->name);
         if(S_ISLNK(ent->mode)) {
                 if((len = readlink(ent->name, buf, sizeof buf)) == -1)
Received on Thu Jun 16 2011 - 02:13:53 CEST

This archive was generated by hypermail 2.2.0 : Thu Jun 16 2011 - 02:24:05 CEST