scandir.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992-1998, 2000 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 the GNU C Library; see the file COPYING.LIB.  If not,
00016    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.  */
00018 
00019 /*
00020  * $Id: scandir.c,v 1.1 2002-12-05 01:50:22 rtv Exp $
00021  *
00022  * taken from glibc, modified slightly for standalone compilation, and used as
00023  * a fallback implementation when scandir() is not available. - BPG
00024  */
00025 
00026 #include <dirent.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <errno.h>
00030 
00031 int
00032 scandir(dir, namelist, select, cmp)
00033      const char *dir;
00034      struct dirent ***namelist;
00035      int (*select) (const struct dirent *);
00036      int (*cmp) (const void *, const void *);
00037 {
00038   DIR *dp = opendir (dir);
00039   struct dirent **v = NULL;
00040   size_t vsize = 0, i;
00041   struct dirent *d;
00042   int save;
00043 
00044   if (dp == NULL)
00045     return -1;
00046 
00047   save = errno;
00048   errno = 0;
00049 
00050   i = 0;
00051   while ((d = readdir (dp)) != NULL)
00052     if (select == NULL || (*select) (d))
00053       {
00054         struct dirent *vnew;
00055         size_t dsize;
00056 
00057         /* Ignore errors from select or readdir */
00058         errno = 0;
00059 
00060         if (i == vsize)
00061           {
00062             struct dirent **new;
00063             if (vsize == 0)
00064               vsize = 10;
00065             else
00066               vsize *= 2;
00067             new = (struct dirent **) realloc (v, vsize * sizeof (*v));
00068             if (new == NULL)
00069               break;
00070             v = new;
00071           }
00072 
00073         dsize = &d->d_name[strlen(d->d_name)+1] - (char *) d;
00074         vnew = (struct dirent *) malloc (dsize);
00075         if (vnew == NULL)
00076           break;
00077 
00078         v[i++] = (struct dirent *) memcpy (vnew, d, dsize);
00079       }
00080 
00081   if (errno != 0)
00082     {
00083       save = errno;
00084       (void) closedir (dp);
00085       while (i > 0)
00086         free (v[--i]);
00087       free (v);
00088       errno = save;
00089       return -1;
00090     }
00091 
00092   (void) closedir (dp);
00093   errno = save;
00094 
00095   /* Sort the list if we have a comparison function to sort with.  */
00096   if (cmp != NULL)
00097     qsort (v, i, sizeof (*v), cmp);
00098   *namelist = v;
00099   return i;
00100 }


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Thu Aug 27 2015 15:20:57