| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

open_dir

#include "sfldir.h"
Bool
open_dir (
    DIRST *dir,
    const char *dir_name)

Synopsis

Creates a directory stream and returns the first entry in the directory. The order of entries is arbitrary, and it is undefined whether you will get entries like '.' and '..' or not. Returns TRUE if something was found, else FALSE. If TRUE, fills-in the values in the directory stream block. Use the same directory stream block for the read_dir and close dir() functions. You must supply a DIRST block when calling open dir(). If you do not supply a dir_name (i.e. it is NULL or ""), open dir() assumes you want to read from the current working directory. You must call close dir() after an open dir(), even if it fails. The strings in DIRST all point to static areas that may change after a further call to read_dir. If you need persistent data (i.e. because you want to collect a set of DIRSTs and then sort them, call fix dir() after each call to open_dir and read_dir. You should then call free dir() to release each DIRST when you are finished.

Source Code - (sfldir.c)

{
    char
        *dir_spec,                      /*  Directory to search through      */
        *dir_spec_end;                  /*  Points to NULL in dir_spec       */

    ASSERT (dir != NULL);
    memset (dir, 0, sizeof (DIRST));

    /*  Copy and prepare the directory specification                         */
    dir_spec = mem_alloc (NAME_MAX);
    if (dir_name == NULL || *dir_name == 0)
        strcpy (dir_spec, DEFAULT_DIR);
    else
        strcpy (dir_spec, dir_name);

#if (defined (MSDOS_FILESYSTEM))
    strconvch (dir_spec, '/', '\\');
#endif
    /*  Remove a trailing slash from the directory name                      */
    dir_spec_end = dir_spec + strlen (dir_spec);
    if (dir_spec_end [-1] == PATHEND)
      {
        dir_spec_end [-1] = '\0';
        dir_spec_end--;
      }

    /*  Open directory stream or find first directory entry                  */
#if (defined (__UNIX__) || defined (__VMS_XOPEN) || defined (__OS2__))
    if (strnull (dir_spec))
        strcpy (dir_spec, "/");
#   if (defined (__OS2__))
    if (dir_spec_end [-1] == ':')                /*  Special case d: to d:\  */
        strcat (dir_spec, "\\");
#  endif
    if ((dir-> _dir_handle = opendir (dir_spec)) == NULL)

#elif (defined (WIN32))
    strcat (dir_spec, "\\*");
    if ((dir-> _dir_handle = FindFirstFile (dir_spec, &dir-> _dir_entry))
                           == INVALID_HANDLE_VALUE)

#elif (defined (_MSC_VER))
    strcat (dir_spec, "\\*.*");
    if ((dir-> _dir_handle = _dos_findfirst (dir_spec, _A_NORMAL | _A_SUBDIR,
                                             &dir-> _dir_entry)) != 0)

#elif (defined (__TURBOC__) || defined (__DJGPP__))
    strcat (dir_spec, "\\*.*");
    if (findfirst (dir_spec, &dir-> _dir_entry, 255 - FA_LABEL) == -1)
#endif
      {
        mem_free (dir_spec);
        return (FALSE);                 /*  Could not open directory         */
      }

    /*  Save the directory name in directory stream structure                */
#if (defined (__MSDOS__) || defined (__OS2__))
    *dir_spec_end = '\0';               /*  Kill the \*.* again              */
#endif
    dir-> dir_name = dir_spec;          /*  Now owned by DIRST structure     */

#if (defined (__UNIX__) || defined (__VMS_XOPEN) || defined (__OS2__))
    /*  Under UNIX & VMS we still need to fetch the first file entry         */
    return (read dir (dir));

#elif (defined (WIN32))
    /*  Under Win32 we have read an entry, so return those values            */
    return (populate_entry (dir));

#elif (defined (_MSC_VER))
    /*  Under MSC we have read an entry, so return those values              */
    return (populate_entry (dir));

#elif (defined (__TURBOC__) || defined (__DJGPP__))
    /*  Under Borland C we have read an entry, so return those values        */
    return (populate_entry (dir));
#else

    return (FALSE);                     /*  Directory access not supported   */
#endif
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation