[comp.lang.c] File listing for current directory under Unix?

marekp@pnet91.cts.com (Marek Pawlowski) (05/04/91)

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               */

toma@swsrv1.cirr.com (Tom Armistead) (05/05/91)

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

gordon@osiris.cso.uiuc.edu (John Gordon) (05/08/91)

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?

	Yes.  do a:    popen("ls *", "r");


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>

mike@bria.UUCP (mike.stefanik) (05/10/91)

In an article, gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>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?
>
>	Yes.  do a:    popen("ls *", "r");

Actually, this will only work on UNIX-based systems, or perhaps DOS that
has MKS tools or a facsimile thereof.

Generally, this is operating system dependant and therefore the question
is best posted for the particular os in question.

As an aside, the use of popen() for this purpose is rather ungainly.  Using
readdir() would have been a better suggestion.

-- 
Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
If MS-DOS didn't exist, who would UNIX programmers have to make fun of?

gwc@root.co.uk (Geoff Clare) (05/10/91)

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?

gordon@osiris.cso.uiuc.edu (John Gordon) writes:

>	Yes.  do a:    popen("ls *", "r");

That won't do what was asked for if there are any subdirectories under the
current directory.  To do it with popen() you should use either

	popen("/bin/ls", "r");
or
	popen("/bin/ls -a", "r");

depending on whether you want to include files beginning with "." or not.

As other people have already pointed out, it is much better to use
opendir(), readdir() and closedir() if you have them.
-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273