#include #include #include #include #include #include #define HAVE_GETDIRENTRIES 1 typedef struct OSI_DIRECTORY { char path[1024]; char filename[1024]; DIR *handle; struct dirent *file; /* used in case we need to traverse with getdirentries. */ /* instead of opendir and readdir. */ unsigned long dd_loc; unsigned long dd_seek; unsigned long dd_fd; unsigned long dd_size; unsigned long dd_len; unsigned char buffer[8192]; } OSI_DIRECTORY; struct dirent * osi_readdir( OSI_DIRECTORY *d ) { #if defined(HAVE_GETDIRENTRIES) || defined(HAVE_GETDENTS) register struct dirent *dp; if( d == NULL ) { return NULL; } for(;;) { if( d->dd_loc == 0 ) { #ifdef HAVE_GETDIRENTRIES d->dd_size = getdirentries( d->dd_fd, d->buffer, d->dd_len, &d->dd_seek ); #else d->dd_size = getdents( d->dd_fd, (struct dirent *)d->buffer, d->dd_len ); #endif } if( d->dd_size <= 0 ) { return NULL; } dp = (struct dirent *)(d->buffer + d->dd_loc ); if( dp == NULL ) { return NULL; } if( dp->d_reclen <= 0 || dp->d_reclen > d->dd_len + 1 - d->dd_loc ) { return NULL; } d->dd_loc += dp->d_reclen; /* this causes a crash on irix, alignment? not sure yet. */ #ifndef SYSTEM_IRIX if( dp->d_ino == 0 ) { continue; } #endif d->file = dp; if( d->file->d_name[0] == '\0' ) { return NULL; } return dp; } #endif /* HAVE_GETDIRENTRIES */ return NULL; } int osi_directory_set_fd( OSI_DIRECTORY *directory, int fd ) { if( ( directory == NULL ) || ( fd <= 0 ) ) { return -1; } /* we are using an fd, so we make sure our handle is set */ /* to NULL, close it if it is not. */ directory->handle = NULL; directory->dd_fd = fd; directory->dd_len = sizeof( directory->buffer ); return 0; } int main(int argc,char *argv[]) { int fd; OSI_DIRECTORY dir; int keep_going = 1; fd = open(argv[1],O_RDONLY); if (!fd) { fprintf(stderr,"unable to open dir!\n" ); return 0; } strcpy(dir.path,"."); osi_directory_set_fd(&dir, fd ); if ( dir.dd_fd > 0 ) { while(keep_going) { if ( osi_readdir( &dir ) ) { fprintf( stderr,"(%s)\n", dir.file->d_name ); } else { keep_going = 0; } } } close(fd); return 0; }