themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / scandir.c
blobffe5803d8b3ce744299550b64e3e3ccd8b972a32
1 /* Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA. */
19 #if defined(WIN32) && !defined(__CYGWIN__)
20 # include "scandir_win32.c"
21 #else
23 # include "flstring.h"
25 # if !HAVE_SCANDIR
26 # include <stdlib.h>
27 # include <sys/types.h>
28 # include <errno.h>
30 # if HAVE_DIRENT_H
31 # include <dirent.h>
32 # define NAMLEN(dirent) strlen((dirent)->d_name)
33 # else
34 # define dirent direct
35 # define NAMLEN(dirent) (dirent)->d_namlen
36 # if HAVE_SYS_NDIR_H
37 # include <sys/ndir.h>
38 # endif
39 # if HAVE_SYS_DIR_H
40 # include <sys/dir.h>
41 # endif
42 # if HAVE_NDIR_H
43 # include <ndir.h>
44 # endif
45 # endif
47 int
48 fl_scandir(const char *dir, struct dirent ***namelist,
49 int (*select)(struct dirent *),
50 int (*compar)(struct dirent **, struct dirent **))
52 DIR *dp = opendir (dir);
53 struct dirent **v = NULL;
54 size_t vsize = 0, i;
55 struct dirent *d;
56 int save;
58 if (dp == NULL)
59 return -1;
61 save = errno;
62 errno = 0;
64 i = 0;
65 while ((d = readdir (dp)) != NULL)
66 if (select == NULL || (*select) (d))
68 size_t dsize;
70 if (i == vsize)
72 struct dirent **newv;
73 if (vsize == 0)
74 vsize = 10;
75 else
76 vsize *= 2;
77 newv = (struct dirent **) realloc (v, vsize * sizeof (*v));
78 if (newv == NULL)
80 lose:
81 errno = ENOMEM;
82 break;
84 v = newv;
87 # define _D_EXACT_NAMLEN(d) (strlen ((d)->d_name))
88 # define _D_ALLOC_NAMLEN(d) (sizeof (d)->d_name > 1 ? sizeof (d)->d_name : \
89 _D_EXACT_NAMLEN (d) + 1)
91 dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
92 v[i] = (struct dirent *) malloc (dsize);
93 if (v[i] == NULL)
94 goto lose;
96 memcpy (v[i++], d, dsize);
99 if (errno != 0)
101 save = errno;
102 (void) closedir (dp);
103 while (i > 0)
104 free (v[--i]);
105 free (v);
106 errno = save;
107 return -1;
110 (void) closedir (dp);
111 errno = save;
113 /* Sort the list if we have a comparison function to sort with. */
114 if (compar) qsort (v, i, sizeof (*v), (int (*)(const void *, const void *))compar);
115 *namelist = v;
116 return i;
119 # endif
120 #endif
123 * End of "$Id: scandir.c 8192 2011-01-05 16:50:10Z manolo $".