/* * This is a driver program for the directory hashing homework */ #include #include #include #include #define printf ( void )printf #define fprintf ( void )fprintf #define strcpy ( void )strcpy typedef int HashTbl; /* dummy declaration - you may need to change this */ static HashTbl H; /* dummy declaration - you may need to change this */ void Print( const HashTbl H ); struct dirent * Find( DIR *DirStream, const char *FileName, const HashTbl H ); struct dirent * Insert( DIR *DirStream, const char *FileName, const HashTbl H ); /* * This array contains a list of files for you to Find(). You can * add your own files to the end, if you wish. The files in the array * must all be in whatever directory it is that you open (default is * /bin, but you can change that for testing purposes). */ static char * Files[] = { "ls", "rm", "cat", "mv", "vi", "ed", /* add some more names of /bin files here, if you want */ NULL }; int main( void ) { int i; /* a counter */ /* * pointers for use with dirent routines */ DIR *DirStream; struct dirent *DirEntPtr; /* * this variable is used to hold a copy of the d_name - it * keeps data from being corrupted under certain circumstances */ char FileName[ MAXNAMLEN ]; /* * open a directory stream - use of /bin is suggested here * because its files are static and there are lots of them */ if( ( DirStream = opendir( "/bin" ) ) != NULL ) { /* * read directory entries and Insert() them in the HashTbl */ while( ( DirEntPtr = readdir( DirStream ) ) != NULL ) { /* * this strcpy() should not be necessary, but ... */ strcpy( FileName, DirEntPtr->d_name ); if( Insert( DirStream, FileName, H ) == NULL ) fprintf( stderr, "%s: directory entry not found\n", FileName ); } /* * Print() out your representation of the hash table */ Print( H ); /* * now see if you can Find() the files again */ for( i = 0; Files[ i ] != NULL; i++ ) if( ( DirEntPtr = Find( DirStream, Files[ i ], H ) ) != NULL ) printf( "file /bin/%-5.5s is linked to inode %u\n", DirEntPtr->d_name, ( unsigned int )DirEntPtr->d_ino ); else fprintf( stderr, "%s: directory entry not found\n", Files[ i ] ); /* * close a directory stream */ ( void )closedir( DirStream ); } return EXIT_SUCCESS; }