diff --git a/contrib/pg_archivecleanup/pg_archivecleanup.c b/contrib/pg_archivecleanup/pg_archivecleanup.c
index f12331a..3e47f3c 100644
--- a/contrib/pg_archivecleanup/pg_archivecleanup.c
+++ b/contrib/pg_archivecleanup/pg_archivecleanup.c
@@ -117,7 +117,7 @@ CleanupPriorWALFiles(void)
 
 	if ((xldir = opendir(archiveLocation)) != NULL)
 	{
-		while ((xlde = readdir(xldir)) != NULL)
+		while (errno = 0, (xlde = readdir(xldir)) != NULL)
 		{
 			strncpy(walfile, xlde->d_name, MAXPGPATH);
 			TrimExtension(walfile, additional_ext);
@@ -175,6 +175,11 @@ CleanupPriorWALFiles(void)
 				}
 			}
 		}
+
+		if (errno)
+			fprintf(stderr, "%s: ERROR: could not read next directory entry: %s\n",
+					progname, strerror(errno));
+
 		closedir(xldir);
 	}
 	else
