File listing for current directory under Unix?

Tom Armistead toma at swsrv1.cirr.com
Sun May 5 05:14:33 AEST 1991


In article <689 at generic.UUCP> marekp at 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 at gnu.ai.mit.edu, marekp at cerf.net, marekp at pnet91.cts.com,       */
>/* marekp at generic.uucp, Voice: (416) 884-4501 4-8pm Toronto time        */
>/* "F U cn rd dis U mst Uz Unix" - ericmcg at 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 at swsrv1.cirr.com                {egsner,letni,ozdaltx,void}!swsrv1!toma

/*****************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <malloc.h>

#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 at swsrv1.cirr.com                {egsner,letni,ozdaltx,void}!swsrv1!toma



More information about the Comp.lang.c mailing list