This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] fix rewinddir filepos


This patch fixes rewinddir() to reset dirp->filepos.  Currently when
we call the following order, the first and the second telldir() don't
return the same value because dirp->filepos is not initialized:

    opendir() -> telldir() -> readdir() -> rewinddir() -> telldir()

I added the test case which is integrated into tst-seekdir.

Regards,
-- gotom

2005-01-16  GOTO Masanori  <gotom@debian.or.jp>

	* sysdeps/unix/rewinddir.c: Reset filepos.
	* dirent/tst-seekdir.c: Check telldir value after calling rewinddir.

Index: sysdeps/unix/rewinddir.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/unix/rewinddir.c,v
retrieving revision 1.8
diff -u -r1.8 rewinddir.c
--- sysdeps/unix/rewinddir.c	6 Jul 2001 04:56:07 -0000	1.8
+++ sysdeps/unix/rewinddir.c	16 Jan 2005 04:35:21 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1995-1998, 2005 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -30,6 +30,7 @@
 {
   __libc_lock_lock (dirp->lock);
   (void) __lseek (dirp->fd, (off_t) 0, SEEK_SET);
+  dirp->filepos = 0;
   dirp->offset = 0;
   dirp->size = 0;
   __libc_lock_unlock (dirp->lock);
Index: dirent/tst-seekdir.c
===================================================================
RCS file: /cvs/glibc/libc/dirent/tst-seekdir.c,v
retrieving revision 1.6
diff -u -r1.6 tst-seekdir.c
--- dirent/tst-seekdir.c	8 Aug 2001 02:48:58 -0000	1.6
+++ dirent/tst-seekdir.c	16 Jan 2005 05:13:06 -0000
@@ -11,8 +11,23 @@
   int i = 0;
   int result = 0;
   struct dirent *dp;
+  long int save0;
+  long int rewind;
 
   dirp = opendir (".");
+  if (dirp == NULL)
+    {
+      printf ("opendir failed: %m\n");
+      return 1;
+    }
+
+  save0 = telldir (dirp);
+  if (save0 == -1)
+    {
+      printf ("telldir failed: %m\n");
+      result = 1;
+    }
+
   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
     {
       /* save position 3 (after fourth entry) */
@@ -44,6 +59,19 @@
   for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp))
     printf ("%s\n", dp->d_name);
 
+  /* Check rewinddir */
+  rewinddir (dirp);
+  rewind = telldir (dirp);
+  if (rewind == -1)
+    {
+      printf ("telldir failed: %m\n");
+      result = 1;
+    }
+  else if (save0 != rewind)
+    {
+      printf ("rewinddir didn't reset directory stream\n");
+      result = 1;
+    }
 
   closedir (dirp);
   return result;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]