~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to db/os/os_dir.c

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * See the file LICENSE for redistribution information.
 
3
 *
 
4
 * Copyright (c) 1997-2001
 
5
 *      Sleepycat Software.  All rights reserved.
 
6
 */
 
7
 
 
8
#include "db_config.h"
 
9
 
 
10
#ifndef lint
 
11
static const char revid[] = "$Id: os_dir.c,v 11.10 2001/04/03 15:14:26 krinsky Exp $";
 
12
#endif /* not lint */
 
13
 
 
14
#ifndef NO_SYSTEM_INCLUDES
 
15
#include <sys/types.h>
 
16
 
 
17
#if HAVE_DIRENT_H
 
18
# include <dirent.h>
 
19
# define NAMLEN(dirent) strlen((dirent)->d_name)
 
20
#else
 
21
# define dirent direct
 
22
# define NAMLEN(dirent) (dirent)->d_namlen
 
23
# if HAVE_SYS_NDIR_H
 
24
#  include <sys/ndir.h>
 
25
# endif
 
26
# if HAVE_SYS_DIR_H
 
27
#  include <sys/dir.h>
 
28
# endif
 
29
# if HAVE_NDIR_H
 
30
#  include <ndir.h>
 
31
# endif
 
32
#endif
 
33
 
 
34
#endif
 
35
 
 
36
#include "db_int.h"
 
37
#include "os_jump.h"
 
38
 
 
39
/*
 
40
 * __os_dirlist --
 
41
 *      Return a list of the files in a directory.
 
42
 *
 
43
 * PUBLIC: int __os_dirlist __P((DB_ENV *, const char *, char ***, int *));
 
44
 */
 
45
int
 
46
__os_dirlist(dbenv, dir, namesp, cntp)
 
47
        DB_ENV *dbenv;
 
48
        const char *dir;
 
49
        char ***namesp;
 
50
        int *cntp;
 
51
{
 
52
        struct dirent *dp;
 
53
        DIR *dirp;
 
54
        int arraysz, cnt, ret;
 
55
        char **names;
 
56
 
 
57
        if (__db_jump.j_dirlist != NULL)
 
58
                return (__db_jump.j_dirlist(dir, namesp, cntp));
 
59
 
 
60
#ifdef HAVE_VXWORKS
 
61
        if ((dirp = opendir((char *)dir)) == NULL)
 
62
#else
 
63
        if ((dirp = opendir(dir)) == NULL)
 
64
#endif
 
65
                return (__os_get_errno());
 
66
        names = NULL;
 
67
        for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
 
68
                if (cnt >= arraysz) {
 
69
                        arraysz += 100;
 
70
                        if ((ret = __os_realloc(dbenv,
 
71
                            arraysz * sizeof(names[0]), &names)) != 0)
 
72
                                goto nomem;
 
73
                }
 
74
                if ((ret = __os_strdup(dbenv, dp->d_name, &names[cnt])) != 0)
 
75
                        goto nomem;
 
76
        }
 
77
        (void)closedir(dirp);
 
78
 
 
79
        *namesp = names;
 
80
        *cntp = cnt;
 
81
        return (0);
 
82
 
 
83
nomem:  if (names != NULL)
 
84
                __os_dirfree(dbenv, names, cnt);
 
85
        if (dirp != NULL)
 
86
                (void)closedir(dirp);
 
87
        return (ret);
 
88
}
 
89
 
 
90
/*
 
91
 * __os_dirfree --
 
92
 *      Free the list of files.
 
93
 *
 
94
 * PUBLIC: void __os_dirfree __P((DB_ENV *, char **, int));
 
95
 */
 
96
void
 
97
__os_dirfree(dbenv, names, cnt)
 
98
        DB_ENV *dbenv;
 
99
        char **names;
 
100
        int cnt;
 
101
{
 
102
        if (__db_jump.j_dirfree != NULL)
 
103
                __db_jump.j_dirfree(names, cnt);
 
104
        else {
 
105
                while (cnt > 0)
 
106
                        __os_free(dbenv, names[--cnt], 0);
 
107
                __os_free(dbenv, names, 0);
 
108
        }
 
109
}