home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / dirent / scandir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-28  |  2.4 KB  |  107 lines

  1. /* Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <dirent.h>
  21. #include <stdlib.h>
  22.  
  23. int
  24. #ifdef linux
  25. DEFUN(scandir, (dir, namelist, select, cmp),
  26.       CONST char *dir AND
  27.       struct dirent ***namelist AND
  28.       __dir_select_fn_t select AND
  29.       __dir_compar_fn_t cmp)
  30. #else
  31. DEFUN(scandir, (dir, namelist, select, cmp),
  32.       CONST char *dir AND
  33.       struct dirent ***namelist AND
  34.       int EXFUN((*select), (struct dirent *)) AND
  35.       int EXFUN((*cmp), (
  36.     CONST struct dirent * CONST *,
  37.     CONST struct dirent * CONST *)))
  38. #endif
  39. {
  40.   DIR *dp = opendir (dir);
  41.   struct dirent **v = NULL;
  42.   size_t vsize = 0, i;
  43.   struct dirent *d;
  44.   int save;
  45.  
  46.   if (dp == NULL)
  47.     return -1;
  48.  
  49.   save = errno;
  50.   errno = 0;
  51.  
  52.   i = 0;
  53.   while ((d = readdir (dp)) != NULL)
  54.     if (select == NULL || (*select) (d))
  55.       {
  56.     if (i == vsize)
  57.       {
  58.         struct dirent **new;
  59.         if (vsize == 0)
  60.           vsize = 10;
  61.         else
  62.           vsize *= 2;
  63.         new = (struct dirent **) realloc (v, vsize * sizeof (*v));
  64.         if (new == NULL)
  65.           {
  66.           lose:
  67.         (void) closedir (dp);
  68.         while (i > 0)
  69.           free (v[--i]);
  70.         free (v);
  71.         errno = ENOMEM;
  72.         return -1;
  73.           }
  74.         v = new;
  75.       }
  76.  
  77.     v[i] = (struct dirent *) malloc (sizeof (**v));
  78.     if (v[i] == NULL)
  79.       goto lose;
  80.  
  81.     *v[i++] = *d;
  82.       }
  83.  
  84.   if (errno != 0)
  85.     {
  86.       save = errno;
  87.  
  88.       (void) closedir (dp);
  89.  
  90.       /* Remember to free allocated memory upon error! */
  91.       while (i > 0) free(v[--i]);
  92.       free(v);
  93.  
  94.       errno = save;
  95.       return -1;
  96.     }
  97.  
  98.   (void) closedir (dp);
  99.   errno = save;
  100.  
  101.   /* Sort the list if we have a comparison function to sort with.  */
  102.   if (cmp != NULL)
  103.     qsort (v, i, sizeof (*v), cmp);
  104.   *namelist = v;
  105.   return i;
  106. }
  107.