/[hydra]/hydra/src/scandir.c
ViewVC logotype

Annotation of /hydra/src/scandir.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Fri Nov 29 14:56:36 2002 UTC (21 years, 4 months ago) by andreou
Branch: MAIN
CVS Tags: hydra_0_1_6_without_hic, hydra_0_1_7, hydra_0_1_6, hydra_0_1_4, hydra_0_1_8, HEAD
Changes since 1.1: +62 -60 lines
File MIME type: text/plain


Cleanup, minor code audit, few fixes.

1 nmav 1.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 Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the 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     Lesser General Public License for more details.
13    
14     You should have received a copy of the GNU Lesser General Public
15     License along with the GNU C Library; if not, write to the Free
16     Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17     02111-1307 USA. */
18    
19     /* This was modified for use in Hydra web server. --nmav
20     */
21    
22 andreou 1.2 /* $Id$ */
23    
24 nmav 1.1 #include <dirent.h>
25     #include <stdlib.h>
26     #include <string.h>
27     #include <errno.h>
28    
29     #include "compat.h"
30    
31     #ifndef HAVE_SCANDIR
32    
33     int
34 andreou 1.2 scandir(const char *dir, struct dirent ***namelist,
35     int (*select) (const struct dirent *),
36     int (*cmp) (const void *, const void *))
37 nmav 1.1 {
38 andreou 1.2 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 nmav 1.1
47 andreou 1.2 save = errno;
48 nmav 1.1 errno = (0);
49    
50 andreou 1.2 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 nmav 1.1
73     /* dsize = &d->d_name[_D_ALLOC_NAMLEN (d)] - (char *) d;
74     */
75 andreou 1.2 dsize = d->d_reclen;
76     vnew = (struct dirent *) malloc (dsize);
77     if (vnew == NULL)
78     break;
79    
80     v[i++] = (struct dirent *) memcpy (vnew, d, dsize);
81     }
82    
83     if ( errno != 0)
84     {
85     save = errno;
86     (void) closedir (dp);
87     while (i > 0)
88     free (v[--i]);
89     free (v);
90     errno = (save);
91     return -1;
92     }
93 nmav 1.1
94 andreou 1.2 (void) closedir (dp);
95     errno = (save);
96 nmav 1.1
97 andreou 1.2 /* Sort the list if we have a comparison function to sort with. */
98 nmav 1.1 #ifdef HAVE_QSORT
99 andreou 1.2 if (cmp != NULL)
100     qsort (v, i, sizeof (*v), cmp);
101 nmav 1.1 #endif
102    
103 andreou 1.2 *namelist = v;
104     return i;
105     } /* scandir() */
106 nmav 1.1
107     #endif

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26