Module Name: src
Committed By: christos
Date: Wed Feb 8 03:24:30 UTC 2012
Modified Files:
src/lib/libc/compat/gen: compat_readdir.c
Log Message:
Restore binary compatibility with netbsd-1.x for readdir(3). Tested with
a shark tcsh dynamic binary:
tcsh: ELF 32-bit LSB executable, ARM, version 1, dynamically linked \
(uses shared libs), for NetBSD, not stripped
-rwxr-xr-x 1 root wheel 994170 Jun 24 2004 /bin/tcsh
This would infinite loop on ls-F /dev
To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/lib/libc/compat/gen/compat_readdir.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/compat/gen/compat_readdir.c
diff -u src/lib/libc/compat/gen/compat_readdir.c:1.1 src/lib/libc/compat/gen/compat_readdir.c:1.2
--- src/lib/libc/compat/gen/compat_readdir.c:1.1 Mon Sep 12 21:44:09 2005
+++ src/lib/libc/compat/gen/compat_readdir.c Tue Feb 7 22:24:30 2012
@@ -1,8 +1,12 @@
-/* $NetBSD: compat_readdir.c,v 1.1 2005/09/13 01:44:09 christos Exp $ */
+/* $NetBSD: compat_readdir.c,v 1.2 2012/02/08 03:24:30 christos Exp $ */
#define __LIBC12_SOURCE__
#include "namespace.h"
+#include <sys/param.h>
#include <dirent.h>
+#include <errno.h>
+#include <string.h>
+#include <limits.h>
#include <compat/include/dirent.h>
#ifdef __weak_alias
@@ -17,6 +21,42 @@ __warn_references(readdir_r,
"warning: reference to compatibility readdir_r(); include <dirent.h> for correct reference")
#endif
-#define dirent dirent12
+static struct dirent12 *
+direnttodirent12(struct dirent12 *d12, const struct dirent *d)
+{
+ if (d == NULL)
+ return NULL;
-#include "gen/readdir.c"
+ if (d->d_fileno > UINT_MAX || d->d_namlen >= sizeof(d12->d_name)) {
+ errno = ERANGE;
+ return NULL;
+ }
+ d12->d_fileno = (uint32_t)d->d_fileno;
+ d12->d_reclen = (uint16_t)d->d_reclen;
+ d12->d_namlen = (uint8_t)MIN(d->d_namlen, sizeof(d->d_name) - 1);
+ d12->d_type = (uint8_t)d->d_type;
+ memcpy(d12->d_name, d->d_name, d12->d_namlen);
+ d12->d_name[d12->d_namlen] = '\0';
+ return d12;
+}
+
+struct dirent12 *
+readdir(DIR *dirp)
+{
+ static struct dirent12 d12;
+ return direnttodirent12(&d12, __readdir30(dirp));
+}
+
+int
+readdir_r(DIR *dirp, struct dirent12 *entry, struct dirent12 **result)
+{
+ int error;
+ struct dirent e, *ep;
+
+ if ((error = __readdir_r30(dirp, &e, &ep)) != 0)
+ return error;
+
+ *result = entry;
+ (void)direnttodirent12(entry, &e);
+ return 0;
+}