fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

scandir.c

Go to the documentation of this file.
00001 /* Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
00002 This file is part of the GNU C Library.
00003 
00004 The GNU C Library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Library General Public License as
00006 published by the Free Software Foundation; either version 2 of the
00007 License, or (at your option) any later version.
00008 
00009 The GNU C Library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Library General Public License for more details.
00013 
00014 You should have received a copy of the GNU Library General Public
00015 License along with this library; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00017 USA.  */
00018 
00019 #if defined(WIN32) && !defined(__CYGWIN__)
00020 #  include "scandir_win32.c"
00021 #else
00022 
00023 #  include "flstring.h"
00024 
00025 #  if !HAVE_SCANDIR
00026 #    include <stdlib.h>
00027 #    include <sys/types.h>
00028 #    include <errno.h>
00029 
00030 #    if HAVE_DIRENT_H
00031 #      include <dirent.h>
00032 #      define NAMLEN(dirent) strlen((dirent)->d_name)
00033 #    else
00034 #      define dirent direct
00035 #      define NAMLEN(dirent) (dirent)->d_namlen
00036 #      if HAVE_SYS_NDIR_H
00037 #        include <sys/ndir.h>
00038 #      endif
00039 #      if HAVE_SYS_DIR_H
00040 #        include <sys/dir.h>
00041 #      endif
00042 #      if HAVE_NDIR_H
00043 #        include <ndir.h>
00044 #      endif
00045 #    endif
00046 
00047 int
00048 fl_scandir(const char *dir, struct dirent ***namelist,
00049            int (*select)(struct dirent *),
00050            int (*compar)(struct dirent **, struct dirent **))
00051 {
00052   DIR *dp = opendir (dir);
00053   struct dirent **v = NULL;
00054   size_t vsize = 0, i;
00055   struct dirent *d;
00056   int save;
00057 
00058   if (dp == NULL)
00059     return -1;
00060 
00061   save = errno;
00062   errno = 0;
00063 
00064   i = 0;
00065   while ((d = readdir (dp)) != NULL)
00066     if (select == NULL || (*select) (d))
00067       {
00068       size_t dsize;
00069 
00070       if (i == vsize)
00071         {
00072           struct dirent **newv;
00073           if (vsize == 0)
00074             vsize = 10;
00075           else
00076             vsize *= 2;
00077           newv = (struct dirent **) realloc (v, vsize * sizeof (*v));
00078           if (newv == NULL)
00079             {
00080             lose:
00081               errno = ENOMEM;
00082               break;
00083             }
00084           v = newv;
00085         }
00086 
00087 #    define _D_EXACT_NAMLEN(d) (strlen ((d)->d_name))
00088 #    define _D_ALLOC_NAMLEN(d) (sizeof (d)->d_name > 1 ? sizeof (d)->d_name : \
00089                               _D_EXACT_NAMLEN (d) + 1)
00090 
00091       dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
00092       v[i] = (struct dirent *) malloc (dsize);
00093       if (v[i] == NULL)
00094         goto lose;
00095 
00096       memcpy (v[i++], d, dsize);
00097       }
00098 
00099   if (errno != 0)
00100     {
00101       save = errno;
00102       (void) closedir (dp);
00103       while (i > 0)
00104       free (v[--i]);
00105       free (v);
00106       errno = save;
00107       return -1;
00108     }
00109 
00110   (void) closedir (dp);
00111   errno = save;
00112 
00113   /* Sort the list if we have a comparison function to sort with.  */
00114   if (compar) qsort (v, i, sizeof (*v), (int (*)(const void *, const void *))compar);
00115   *namelist = v;
00116   return i;
00117 }
00118 
00119 #  endif
00120 #endif
00121 
00122 /*
00123  * End of "$Id: scandir.c 8192 2011-01-05 16:50:10Z manolo $".
00124  */