diff miscellany/libndir/readdir.c @ 0:bce86c4163a3

Initial revision
author kono
date Mon, 18 Apr 2005 23:46:02 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/miscellany/libndir/readdir.c	Mon Apr 18 23:46:02 2005 +0900
@@ -0,0 +1,48 @@
+static char sccsid[] = "@(#)readdir.c	4.1	(Berkeley)	83/03/21";
+
+#include <sys/types.h>
+#include <dir.h>
+
+/*
+ * read an old stlye directory entry and present it as a new one
+ */
+#define	ODIRSIZ	14
+
+struct	olddirect {
+	ino_t	od_ino;
+	char	od_name[ODIRSIZ];
+};
+
+/*
+ * get next entry in a directory.
+ */
+struct direct *
+readdir(dirp)
+	register DIR *dirp;
+{
+	register struct olddirect *dp;
+	static struct direct dir;
+
+	for (;;) {
+		if (dirp->dd_loc == 0) {
+			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
+			    DIRBLKSIZ);
+			if (dirp->dd_size <= 0)
+				return NULL;
+		}
+		if (dirp->dd_loc >= dirp->dd_size) {
+			dirp->dd_loc = 0;
+			continue;
+		}
+		dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
+		dirp->dd_loc += sizeof(struct olddirect);
+		if (dp->od_ino == 0)
+			continue;
+		dir.d_ino = dp->od_ino;
+		strncpy(dir.d_name, dp->od_name, ODIRSIZ);
+		dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
+		dir.d_namlen = strlen(dir.d_name);
+		dir.d_reclen = DIRBLKSIZ;
+		return (&dir);
+	}
+}