[PATCH 4/4] ls: Fix showing links to directories

Sascha Hauer s.hauer at pengutronix.de
Thu May 11 02:11:01 PDT 2017


With links to directories we have to do some adjustments in the
printout. In ls_one we have to use lstat() because we want to
show informations about the file or link. When determing if it's
a file or directory that we show we have to use stat() instead.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 commands/ls.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/commands/ls.c b/commands/ls.c
index 331a4d2015..771477b6e0 100644
--- a/commands/ls.c
+++ b/commands/ls.c
@@ -26,15 +26,21 @@
 #include <getopt.h>
 #include <stringlist.h>
 
-static void ls_one(const char *path, const char* fullname, struct stat *s)
+static void ls_one(const char *path, const char* fullname)
 {
 	char modestr[11];
 	unsigned int namelen = strlen(path);
+	struct stat s;
+	int ret;
+
+	ret = lstat(fullname, &s);
+	if (ret)
+		return;
 
-	mkmodestr(s->st_mode, modestr);
-	printf("%s %14llu %*.*s", modestr, s->st_size, namelen, namelen, path);
+	mkmodestr(s.st_mode, modestr);
+	printf("%s %14llu %*.*s", modestr, s.st_size, namelen, namelen, path);
 
-	if (S_ISLNK(s->st_mode)) {
+	if (S_ISLNK(s.st_mode)) {
 		char realname[PATH_MAX];
 
 		memset(realname, 0, PATH_MAX);
@@ -58,14 +64,14 @@ int ls(const char *path, ulong flags)
 
 	string_list_init(&sl);
 
-	if (lstat(path, &s))
+	if (stat(path, &s))
 		return -errno;
 
 	if (flags & LS_SHOWARG && s.st_mode & S_IFDIR)
 		printf("%s:\n", path);
 
 	if (!(s.st_mode & S_IFDIR)) {
-		ls_one(path, path, &s);
+		ls_one(path, path);
 		return 0;
 	}
 
@@ -89,7 +95,7 @@ int ls(const char *path, ulong flags)
 				continue;
 			}
 
-			ls_one(entry->str, tmp, &s);
+			ls_one(entry->str, tmp);
 		}
 	}
 
@@ -162,7 +168,7 @@ static int do_ls(int argc, char *argv[])
 
 	/* first pass: all files */
 	while (o < argc) {
-		ret = lstat(argv[o], &s);
+		ret = stat(argv[o], &s);
 		if (ret) {
 			printf("%s: %s: %s\n", argv[0],
 					argv[o], errno_str());
@@ -175,7 +181,7 @@ static int do_ls(int argc, char *argv[])
 			if (flags & LS_COLUMN)
 				string_list_add_sorted(&sl, argv[o]);
 			else
-				ls_one(argv[o], argv[o], &s);
+				ls_one(argv[o], argv[o]);
 		}
 
 		o++;
@@ -190,7 +196,7 @@ static int do_ls(int argc, char *argv[])
 
 	/* second pass: directories */
 	while (o < argc) {
-		ret = lstat(argv[o], &s);
+		ret = stat(argv[o], &s);
 		if (ret) {
 			o++;
 			exitcode = COMMAND_ERROR;
-- 
2.11.0




More information about the barebox mailing list