Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Apr 1995 09:48:27 -0400 (EDT)
From:      Craig Yap <craig@munich.gcomm.com>
To:        bugs@FreeBSD.org
Subject:   seekdir bug
Message-ID:  <199504121348.JAA27823@munich.gcomm.com>

next in thread | raw e-mail | index | archive | help
There is a bug when seeking through a directory.  telldir() seems to return
a position in the directory that doesnt match the name of the file.  I've
included a program that will exploit the bug.  I ran it as followed:

First made some test files:
touch 1 2 3 4
then ran the program:
./a "?"

I will continually show the name=a eventhough the position is incremented

Thanks for your time,
Craig

/*
 * test the viability of seekdir() after closedir()
 *
 * requires: fnmatch() and dirent routines
 */

#include <dirent.h>
#include <fnmatch.h>

static long position=-1;

char *
findfile(char *spec)
{
     struct dirent *dent;
     DIR *dirp;

     if ((dirp=opendir(".")) == NULL) {
	  perror("opendir");
	  exit(2);
     }
     if (position != -1) {
	  seekdir(dirp,position);
     }
     while((dent=readdir(dirp)) != NULL) {
	  if (fnmatch(spec,dent->d_name,0) == 0 &&
	      strcmp(dent->d_name,".") != 0 &&
	      strcmp(dent->d_name,"..") != 0) {
	       position=telldir(dirp);
	       closedir(dirp);
	       return(dent->d_name);
	  }
     }
     closedir(dirp);
     return(NULL);
}


main(
int argc,
char *argv[])
{
     char *fname;

     if (argc != 2) {
	  printf("usage: testdirent \"<filespec>\"\n");
	  return(1);
     }
     while ((fname=findfile(argv[1])) != NULL) {
	  printf("name=%s position=%ld\n",fname,position);
     }
     return(0);
}
     
     

-- 
------------------------------------------------------------------------------
Craig Yap                                               craig@munich.gcomm.com
Software Engineer
Unix Technical Division
Galacticomm, Inc



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199504121348.JAA27823>