os_dir.c

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

Generated on Sun Jun 8 10:56:38 2008 for GNUmifluz by  doxygen 1.5.5