From 7bbd4c56ebe1c91b90561e7f22e875f58e7facc9 Mon Sep 17 00:00:00 2001
From: Connor Lane Smith <cls@lubutu.com>
Date: Mon, 17 Oct 2011 01:44:07 +0100
Subject: [PATCH] lsx: detect read errors

---
 lsx.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/lsx.c b/lsx.c
index 57c03bf..cb016cf 100644
--- a/lsx.c
+++ b/lsx.c
@@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <dirent.h>
+#include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -28,14 +29,15 @@ lsx(const char *dir) {
 	struct stat st;
 	DIR *dp;
 
-	if(!(dp = opendir(dir))) {
+	for(dp = opendir(dir); dp && (d = readdir(dp)); errno = 0)
+		if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf
+		&& access(buf, X_OK) == 0 && stat(buf, &st) == 0 && S_ISREG(st.st_mode))
+			puts(d->d_name);
+
+	if(errno != 0) {
 		status = EXIT_FAILURE;
 		perror(dir);
-		return;
 	}
-	while((d = readdir(dp)))
-		if(snprintf(buf, sizeof buf, "%s/%s", dir, d->d_name) < (int)sizeof buf
-		&& stat(buf, &st) == 0 && S_ISREG(st.st_mode) && access(buf, X_OK) == 0)
-			puts(d->d_name);
-	closedir(dp);
+	if(dp)
+		closedir(dp);
 }