scandir.c
Go to the documentation of this file.
1 /* Copyright (C) 1992-1998, 2000 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 not,
16  write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  Boston, MA 02111-1307, USA. */
18 
19 /*
20  * $Id: scandir.c,v 1.1 2002-12-05 01:50:22 rtv Exp $
21  *
22  * taken from glibc, modified slightly for standalone compilation, and used as
23  * a fallback implementation when scandir() is not available. - BPG
24  */
25 
26 #include <dirent.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 
31 int
32 scandir(dir, namelist, select, cmp)
33  const char *dir;
34  struct dirent ***namelist;
35  int (*select) (const struct dirent *);
36  int (*cmp) (const void *, const void *);
37 {
38  DIR *dp = opendir (dir);
39  struct dirent **v = NULL;
40  size_t vsize = 0, i;
41  struct dirent *d;
42  int save;
43 
44  if (dp == NULL)
45  return -1;
46 
47  save = errno;
48  errno = 0;
49 
50  i = 0;
51  while ((d = readdir (dp)) != NULL)
52  if (select == NULL || (*select) (d))
53  {
54  struct dirent *vnew;
55  size_t dsize;
56 
57  /* Ignore errors from select or readdir */
58  errno = 0;
59 
60  if (i == vsize)
61  {
62  struct dirent **new;
63  if (vsize == 0)
64  vsize = 10;
65  else
66  vsize *= 2;
67  new = (struct dirent **) realloc (v, vsize * sizeof (*v));
68  if (new == NULL)
69  break;
70  v = new;
71  }
72 
73  dsize = &d->d_name[strlen(d->d_name)+1] - (char *) d;
74  vnew = (struct dirent *) malloc (dsize);
75  if (vnew == NULL)
76  break;
77 
78  v[i++] = (struct dirent *) memcpy (vnew, d, dsize);
79  }
80 
81  if (errno != 0)
82  {
83  save = errno;
84  (void) closedir (dp);
85  while (i > 0)
86  free (v[--i]);
87  free (v);
88  errno = save;
89  return -1;
90  }
91 
92  (void) closedir (dp);
93  errno = save;
94 
95  /* Sort the list if we have a comparison function to sort with. */
96  if (cmp != NULL)
97  qsort (v, i, sizeof (*v), cmp);
98  *namelist = v;
99  return i;
100 }
int scandir(char *dir, struct dirent ***namelist, int(*)(const struct dirent *) select, int *cmp) const
Definition: scandir.c:32


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 Mon Jun 10 2019 15:06:09