mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] string_list: Add string_list_for_each_entry macro
@ 2014-07-03  7:40 Sascha Hauer
  2014-07-03  7:40 ` [PATCH 2/2] ls: sort files with -l Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2014-07-03  7:40 UTC (permalink / raw)
  To: barebox

To ease iterating over a string_list.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/stringlist.h | 3 +++
 lib/stringlist.c     | 6 +++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/stringlist.h b/include/stringlist.h
index 8738137..127998c 100644
--- a/include/stringlist.h
+++ b/include/stringlist.h
@@ -29,4 +29,7 @@ static inline void string_list_free(struct string_list *sl)
 	}
 }
 
+#define string_list_for_each_entry(entry, sl) \
+	list_for_each_entry(entry, &(sl)->list, list)
+
 #endif /* __STRINGLIST_H */
diff --git a/lib/stringlist.c b/lib/stringlist.c
index a8af15d..cc84944 100644
--- a/lib/stringlist.c
+++ b/lib/stringlist.c
@@ -64,7 +64,7 @@ int string_list_contains(struct string_list *sl, char *str)
 {
 	struct string_list *entry;
 
-	list_for_each_entry(entry, &sl->list, list) {
+	string_list_for_each_entry(entry, sl) {
 		if (!strcmp(str, entry->str))
 			return 1;
 	}
@@ -77,7 +77,7 @@ void string_list_print_by_column(struct string_list *sl)
 	int len = 0, num, i;
 	struct string_list *entry;
 
-	list_for_each_entry(entry, &sl->list, list) {
+	string_list_for_each_entry(entry, sl) {
 		int l = strlen(entry->str) + 4;
 		if (l > len)
 			len = l;
@@ -91,7 +91,7 @@ void string_list_print_by_column(struct string_list *sl)
 		num = 1;
 
 	i = 0;
-	list_for_each_entry(entry, &sl->list, list) {
+	string_list_for_each_entry(entry, sl) {
 		if (!(++i % num))
 			printf("%s\n", entry->str);
 		else
-- 
2.0.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] ls: sort files with -l
  2014-07-03  7:40 [PATCH 1/2] string_list: Add string_list_for_each_entry macro Sascha Hauer
@ 2014-07-03  7:40 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2014-07-03  7:40 UTC (permalink / raw)
  To: barebox

Always collect directory entries in a string_list and evaluate it
later. This makes sure that the files are printed alphabetically
even when -l is given.

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

diff --git a/commands/ls.c b/commands/ls.c
index f8144ae..1a5925d 100644
--- a/commands/ls.c
+++ b/commands/ls.c
@@ -53,6 +53,8 @@ int ls(const char *path, ulong flags)
 	char tmp[PATH_MAX];
 	struct stat s;
 	struct string_list sl;
+	struct string_list *entry;
+	int ret;
 
 	string_list_init(&sl);
 
@@ -71,51 +73,51 @@ int ls(const char *path, ulong flags)
 	if (!dir)
 		return -errno;
 
-	while ((d = readdir(dir))) {
-		sprintf(tmp, "%s/%s", path, d->d_name);
-		if (flags & LS_COLUMN) {
-			string_list_add_sorted(&sl, d->d_name);
-		} else {
-			if (lstat(tmp, &s))
-				goto out;
-			ls_one(d->d_name, tmp, &s);
-		}
-	}
+	while ((d = readdir(dir)))
+		string_list_add_sorted(&sl, d->d_name);
 
 	closedir(dir);
 
 	if (flags & LS_COLUMN) {
 		string_list_print_by_column(&sl);
-		string_list_free(&sl);
-	}
-
-	if (!(flags & LS_RECURSIVE))
-		return 0;
+	} else {
+		string_list_for_each_entry(entry, &sl) {
+			sprintf(tmp, "%s/%s", path, entry->str);
+			ret = lstat(tmp, &s);
+			if (ret) {
+				printf("%s: %s\n", tmp, strerror(-ret));
+				continue;
+			}
 
-	dir = opendir(path);
-	if (!dir) {
-		errno = ENOENT;
-		return -ENOENT;
+			ls_one(entry->str, tmp, &s);
+		}
 	}
 
-	while ((d = readdir(dir))) {
+	if (!(flags & LS_RECURSIVE))
+		goto out;
 
-		if (!strcmp(d->d_name, "."))
+	string_list_for_each_entry(entry, &sl) {
+		if (!strcmp(entry->str, "."))
 			continue;
-		if (!strcmp(d->d_name, ".."))
+		if (!strcmp(entry->str, ".."))
 			continue;
-		sprintf(tmp, "%s/%s", path, d->d_name);
+		sprintf(tmp, "%s/%s", path, entry->str);
+
+		ret = lstat(tmp, &s);
+		if (ret) {
+			printf("%s: %s\n", tmp, strerror(-ret));
+			continue;
+		}
 
-		if (lstat(tmp, &s))
-			goto out;
 		if (s.st_mode & S_IFDIR) {
 			char *norm = normalise_path(tmp);
 			ls(norm, flags);
 			free(norm);
 		}
 	}
+
 out:
-	closedir(dir);
+	string_list_free(&sl);
 
 	return 0;
 }
-- 
2.0.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-07-03  7:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-03  7:40 [PATCH 1/2] string_list: Add string_list_for_each_entry macro Sascha Hauer
2014-07-03  7:40 ` [PATCH 2/2] ls: sort files with -l Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox