BSD 4_3_Tahoe release
[unix-history] / usr / src / lib / libc / gen / scandir.c
index f00b8b4..15d6e45 100644 (file)
@@ -1,4 +1,24 @@
-/*     scandir.c       4.1     82/12/13        */
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)scandir.c  5.3 (Berkeley) 6/18/88";
+#endif /* LIBC_SCCS and not lint */
+
 /*
  * Scan the directory dirname calling select to make a list of selected
  * directory entries then sort using qsort and compare routine dcomp.
 /*
  * Scan the directory dirname calling select to make a list of selected
  * directory entries then sort using qsort and compare routine dcomp.
@@ -8,7 +28,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <dir.h>
+#include <sys/dir.h>
 
 scandir(dirname, namelist, select, dcomp)
        char *dirname;
 
 scandir(dirname, namelist, select, dcomp)
        char *dirname;
@@ -55,8 +75,11 @@ scandir(dirname, namelist, select, dcomp)
                 * realloc the maximum size.
                 */
                if (++nitems >= arraysz) {
                 * realloc the maximum size.
                 */
                if (++nitems >= arraysz) {
+                       if (fstat(dirp->dd_fd, &stb) < 0)
+                               return(-1);     /* just might have grown */
+                       arraysz = stb.st_size / 12;
                        names = (struct direct **)realloc((char *)names,
                        names = (struct direct **)realloc((char *)names,
-                               (stb.st_size/12) * sizeof(struct direct *));
+                               arraysz * sizeof(struct direct *));
                        if (names == NULL)
                                return(-1);
                }
                        if (names == NULL)
                                return(-1);
                }