ls: fix column widths and path printing with -l.
If you run
ls -l /bin
you expect to see all the filenames without /bin/ included.
If you run
ls -l /bin/ls
you expect to see the line about /bin/ls *with* the /bin/ included. This is
too allow for stuff like
ls -l /proc/*/fd/2
where seeing a bunch of rows named '2' is totally unhelpful.
'ls -s' (and ls with no options) was already doing this, it's just -l that
didn't.
While we're here, fix the column widths in -l to align. device and symlink
lines didn't use the same widths as regular files, so the output looked
strange.
Change-Id: If0c180c8b3fce26ed509543d2659ec67c114b610
diff --git a/ls.c b/ls.c
index 4990ed9..c9ff40f 100644
--- a/ls.c
+++ b/ls.c
@@ -257,22 +257,13 @@
return 0;
}
-static int listfile_long(const char *path, int flags)
+static int listfile_long(const char *path, const char *filename, int flags)
{
struct stat s;
char date[32];
char mode[16];
char user[16];
char group[16];
- const char *name;
-
- /* name is anything after the final '/', or the whole path if none*/
- name = strrchr(path, '/');
- if(name == 0) {
- name = path;
- } else {
- name++;
- }
if(lstat(path, &s) < 0) {
return -1;
@@ -291,14 +282,14 @@
switch(s.st_mode & S_IFMT) {
case S_IFBLK:
case S_IFCHR:
- printf("%s %-8s %-8s %3d, %3d %s %s\n",
+ printf("%s %-8s %-8s %5d, %3d %s %s\n",
mode, user, group,
(int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev),
- date, name);
+ date, filename);
break;
case S_IFREG:
- printf("%s %-8s %-8s %12lld %s %s\n",
- mode, user, group, s.st_size, date, name);
+ printf("%s %-8s %-8s %10lld %s %s\n",
+ mode, user, group, s.st_size, date, filename);
break;
case S_IFLNK: {
char linkto[256];
@@ -316,13 +307,13 @@
linkto[len] = 0;
}
- printf("%s %-8s %-8s %s %s -> %s\n",
- mode, user, group, date, name, linkto);
+ printf("%s %-8s %-8s %s %s -> %s\n",
+ mode, user, group, date, filename, linkto);
break;
}
default:
- printf("%s %-8s %-8s %s %s\n",
- mode, user, group, date, name);
+ printf("%s %-8s %-8s %s %s\n",
+ mode, user, group, date, filename);
}
return 0;
@@ -346,7 +337,7 @@
}
if ((flags & LIST_LONG) != 0) {
- return listfile_long(pathname, flags);
+ return listfile_long(pathname, filename, flags);
} else /*((flags & LIST_SIZE) != 0)*/ {
return listfile_size(pathname, filename, flags);
}