Index: uspace/app/cmpdirs/cmpdirs.c
===================================================================
--- uspace/app/cmpdirs/cmpdirs.c	(revision 128a311e02aa7ad74377185bd929a82874cfcade)
+++ uspace/app/cmpdirs/cmpdirs.c	(revision 8c35ebd2436462e92ef76474c72b0a2a5d964559)
@@ -35,4 +35,5 @@
  */
 
+#include <dirent.h>
 #include <getopt.h>
 #include <stdbool.h>
@@ -51,6 +52,4 @@
 /** When reading the content of a file for comparison, how large should one chunk be. */
 #define CHUNK_SIZE 0x4000
-/** File names longer than this will be truncated. */
-#define NAME_BUFFER_SIZE 256
 
 // Macros to make handling errors less cumbersome
@@ -212,17 +211,4 @@
 	(*entry_out) = s->buffer[s->size];
 	return EOK;
-}
-
-/** Writes the name of the entry at position pos in the directory represented by the handle into buffer out_name_buffer
- * and writes the number of byte written to the buffer into out_name_len. The name should be null-terminated, unless it was too long.
- *
- * It assumes:
- *	- handle is open for reading (vfs_open())
- *
- * There is no function for listing a directory in vfs.h and the functions from dirent.h kinda suck, so here I have my own. */
-static errno_t list_dir(int handle, aoff64_t pos, size_t buffer_size, char* out_name_buffer, ssize_t *out_name_len) {
-	errno_t rc = EOK;
-	rc = vfs_read_short(handle, pos, out_name_buffer, buffer_size, out_name_len);
-	return rc;
 }
 
@@ -290,6 +276,4 @@
 	check_ok(rc, close2, "No memory for file buffer #2 (%d bytes needed)\n", CHUNK_SIZE);
 
-	char name_buffer[NAME_BUFFER_SIZE];
-
 	// While there is something in the stack
 	for (struct entry item = {root_handle1, root_handle2}; // initial item
@@ -341,29 +325,31 @@
 			// 4. add both new handles to stack
 
-			for (ssize_t pos = 0;
-				((size_t) pos) < stat1.size;
-				) {
-				// 1.
-				ssize_t read;
-				rc = list_dir(item.handle1, pos, NAME_BUFFER_SIZE, name_buffer, &read);
-				log_msg(LOG_DEFAULT, LVL_DEBUG2, "Read directory entry name: result %s, read size: %"PRIdn".", str_error_name(rc), read);
-				check_ok_break(rc, "Failed (1) to list handle %d, pos %"PRIdn", total size of directory: %"PRIu64"\n", item.handle1, pos, stat1.size);
+			// Since the directories represented by handle1 and handle2 have equal size and all entries in handle1 are
+			// also present in handle2, then all entries in handle2 must also be present in handle1. Therefore they are equal.
+
+			struct dirent *dp;
+			DIR *dirp = opendir_handle(item.handle1);
+			if (!dirp) {
+				rc = errno;
+				assert(rc != EOK);
+			}
+			check_ok(rc, close_inner, "Failed to open directory (handle %d", item.handle1);
+
+			while ((dp = readdir(dirp))) { // 1.
 				// Calculate the real length of the entry name (excluding null-terminator)
-				size_t name_size = str_nsize(name_buffer, NAME_BUFFER_SIZE);
+				size_t name_buffer_size = sizeof(dp->d_name);
+				size_t name_size = str_nsize(dp->d_name, name_buffer_size);
 				// check that the string is null-terminated before the end of the buffer
-				if (name_size >= NAME_BUFFER_SIZE) {
+				if (name_size >= name_buffer_size) {
 					rc = ENAMETOOLONG;
 				}
-				check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE);
-				// advance pos to after the name of the entry (add 1 for null-terminator)
-				pos += read; // NOLINT(*-narrowing-conversions) because name_size < NAME_BUFFER_SIZE
-				log_msg(LOG_DEFAULT, LVL_DEBUG2, "Listed %d: pos %"PRIdn", name: %s\n", item.handle1, pos, name_buffer);
+				check_ok_break(rc, "Name too long (limit is %"PRIdn" excluding null-terminator)", name_buffer_size);
 				// 2.
 				int entry_handle1; // don't forget to put
-				rc = vfs_walk(item.handle1, name_buffer, 0, &entry_handle1);
-				check_ok_break(rc, "(0) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle1);
+				rc = vfs_walk(item.handle1, dp->d_name, 0, &entry_handle1);
+				check_ok_break(rc, "(0) Failed to find entry \"%s\" in directory of handle %d\n", dp->d_name, item.handle1);
 				// 3.
 				int entry_handle2; // must be put
-				rc= vfs_walk(item.handle2, name_buffer, 0, &entry_handle2);
+				rc= vfs_walk(item.handle2, dp->d_name, 0, &entry_handle2);
 				if (rc != EOK) {
 					errno_t rc2 = vfs_put(entry_handle1);
@@ -374,5 +360,5 @@
 						break;
 					}
-					check_ok_break(rc, "(1) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle2);
+					check_ok_break(rc, "(1) Failed to find entry \"%s\" in directory of handle %d\n", dp->d_name, item.handle2);
 				}
 
@@ -388,4 +374,5 @@
 				check_ok_break(rc, "Failed to append to stack\n");
 			}
+			closedir(dirp);
 			if (rc != EOK) goto close_inner;
 		}
@@ -401,6 +388,7 @@
 		}
 		if (rc != EOK) goto close_start;
-	}
-	assert(rc == EEMPTY);
+		if (!equal) break;
+	}
+	assert(rc == EEMPTY || !equal);
 	rc = EOK;
 close_start:
