Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_dir.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_dir.c,v 12.1 2005/06/16 20:23:23 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #if HAVE_DIRENT_H
00016 # include <dirent.h>
00017 # define NAMLEN(dirent) strlen((dirent)->d_name)
00018 #else
00019 # define dirent direct
00020 # define NAMLEN(dirent) (dirent)->d_namlen
00021 # if HAVE_SYS_NDIR_H
00022 #  include <sys/ndir.h>
00023 # endif
00024 # if HAVE_SYS_DIR_H
00025 #  include <sys/dir.h>
00026 # endif
00027 # if HAVE_NDIR_H
00028 #  include <ndir.h>
00029 # endif
00030 #endif
00031 
00032 #endif
00033 
00034 #include "db_int.h"
00035 
00036 /*
00037  * __os_dirlist --
00038  *      Return a list of the files in a directory.
00039  *
00040  * PUBLIC: int __os_dirlist __P((DB_ENV *, const char *, char ***, int *));
00041  */
00042 int
00043 __os_dirlist(dbenv, dir, namesp, cntp)
00044         DB_ENV *dbenv;
00045         const char *dir;
00046         char ***namesp;
00047         int *cntp;
00048 {
00049         struct dirent *dp;
00050         DIR *dirp;
00051         int arraysz, cnt, ret;
00052         char **names;
00053 
00054         if (DB_GLOBAL(j_dirlist) != NULL)
00055                 return (DB_GLOBAL(j_dirlist)(dir, namesp, cntp));
00056 
00057 #ifdef HAVE_VXWORKS
00058         if ((dirp = opendir((char *)dir)) == NULL)
00059 #else
00060         if ((dirp = opendir(dir)) == NULL)
00061 #endif
00062                 return (__os_get_errno());
00063         names = NULL;
00064         for (arraysz = cnt = 0; (dp = readdir(dirp)) != NULL; ++cnt) {
00065                 if (cnt >= arraysz) {
00066                         arraysz += 100;
00067                         if ((ret = __os_realloc(dbenv,
00068                             (u_int)arraysz * sizeof(names[0]), &names)) != 0)
00069                                 goto nomem;
00070                 }
00071                 if ((ret = __os_strdup(dbenv, dp->d_name, &names[cnt])) != 0)
00072                         goto nomem;
00073         }
00074         (void)closedir(dirp);
00075 
00076         *namesp = names;
00077         *cntp = cnt;
00078         return (0);
00079 
00080 nomem:  if (names != NULL)
00081                 __os_dirfree(dbenv, names, cnt);
00082         if (dirp != NULL)
00083                 (void)closedir(dirp);
00084         return (ret);
00085 }
00086 
00087 /*
00088  * __os_dirfree --
00089  *      Free the list of files.
00090  *
00091  * PUBLIC: void __os_dirfree __P((DB_ENV *, char **, int));
00092  */
00093 void
00094 __os_dirfree(dbenv, names, cnt)
00095         DB_ENV *dbenv;
00096         char **names;
00097         int cnt;
00098 {
00099         if (DB_GLOBAL(j_dirfree) != NULL)
00100                 DB_GLOBAL(j_dirfree)(names, cnt);
00101         else {
00102                 while (cnt > 0)
00103                         __os_free(dbenv, names[--cnt]);
00104                 __os_free(dbenv, names);
00105         }
00106 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2