/*
 *  call-seq:
 *     Dir.new( string ) -> aDir
 *
 *  Returns a new directory object for the named directory.
 */
static VALUE
dir_initialize(dir, dirname)
    VALUE dir, dirname;
{
    struct dir_data *dp;

    SafeStringValue(dirname);
    Data_Get_Struct(dir, struct dir_data, dp);
    if (dp->dir) closedir(dp->dir);
    if (dp->path) free(dp->path);
    dp->dir = NULL;
    dp->path = NULL;
    dp->dir = opendir(RSTRING(dirname)->ptr);
    if (dp->dir == NULL) {
        if (errno == EMFILE || errno == ENFILE) {
            rb_gc();
            dp->dir = opendir(RSTRING(dirname)->ptr);
        }
        if (dp->dir == NULL) {
            rb_sys_fail(RSTRING(dirname)->ptr);
        }
    }
    dp->path = strdup(RSTRING(dirname)->ptr);

    return dir;
}