Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!convex!egsner!swsrv1!toma From: toma@swsrv1.cirr.com (Tom Armistead) Newsgroups: comp.lang.c Subject: Re: File listing for current directory under Unix? Message-ID: <1991May4.191433.10026@swsrv1.cirr.com> Date: 4 May 91 19:14:33 GMT References: <689@generic.UUCP> Organization: Software Services: Garland, Tx Lines: 113 In article <689@generic.UUCP> marekp@pnet91.cts.com (Marek Pawlowski) writes: >Does anyone know of a way to get the filenames of the files in the current >directory, in to a two dimensional array of sorts? > >/* Marek Pawlowski, President, Intelligent Twist Software, 250 Harding */ >/* Blvd., PO BOX 32017, Richmond Hill, Ontario, L4C 9M7, CANADA. */ >/* marekp@gnu.ai.mit.edu, marekp@cerf.net, marekp@pnet91.cts.com, */ >/* marekp@generic.uucp, Voice: (416) 884-4501 4-8pm Toronto time */ >/* "F U cn rd dis U mst Uz Unix" - ericmcg@pnet91.cts.com */ Look at the man page for directory(3C). Specifically, the opendir() and readdir() routines. Here a short hack that will work under System V: P.S. If you use this, make sure to check the return values from malloc() and realloc(), I didn't... Tom -- Tom Armistead - Software Services - 2918 Dukeswood Dr. - Garland, Tx 75040 =========================================================================== toma@swsrv1.cirr.com {egsner,letni,ozdaltx,void}!swsrv1!toma /*****************************************************************************/ #include #include #include #include #include #define DIR_GRAN 10 /* initial size of files[] array */ void main() { DIR *dirp; /* pointer to opendir() return value */ char **files; /* holds directory names */ int file_indx; /* offset into files[] array */ struct dirent *dirent; /* return from readdir() */ int cntr; /* for for loop to display names */ /************************************************************************* ** Open the specified directory file (to allow readdir() to get entries ** from it. **************************************************************************/ if( (dirp = opendir( "." )) != NULL ) { /********************************************************************* ** Allocate initial space for storing the filenames from the directory **********************************************************************/ files = (char **)malloc( sizeof( char ** ) * (DIR_GRAN+1) ); files[0] = NULL; file_indx = 0; /******************************************************************** ** Read each entry from the directory file, until none are left. *********************************************************************/ while( (dirent = readdir( dirp )) != NULL ) { /**************************************************************** ** Skip the directory entries for "." and "..". *****************************************************************/ if( !strcmp( dirent->d_name, "." ) || !strcmp( dirent->d_name, ".." ) ) continue; /**************************************************************** ** Allocate space for, and store a copy of, the filename. *****************************************************************/ files[file_indx] = strdup( dirent->d_name ); ++file_indx; /* one more entry in array */ /**************************************************************** ** If the files array is full, allocate memory for DIR_GRAN more ** entries. *****************************************************************/ if( !(file_indx % DIR_GRAN) ) files = (char **)realloc( files, sizeof( char **) * (file_indx+DIR_GRAN+1) ); }/*end while readdir*/ files[file_indx] = NULL; /* show end of filenames */ closedir( dirp ); /* Close the directory file */ /********************************************************************* ** Print out all filenames stored in the loop above. **********************************************************************/ for( cntr=0; files[cntr] != NULL; cntr++ ) printf( "--> %s\n", files[cntr] ); /********************************************************************* ** Free the memory allocated to store the filenames. **********************************************************************/ for( cntr=0; files[cntr] != NULL; cntr++ ) free( files[cntr] ); free( files ); }/*end if opendir*/ }/*end main*/ -- Tom Armistead - Software Services - 2918 Dukeswood Dr. - Garland, Tx 75040 =========================================================================== toma@swsrv1.cirr.com {egsner,letni,ozdaltx,void}!swsrv1!toma