remove last vestige of AT&T code; it was really gone long ago
[unix-history] / usr / src / lib / libc / gen / scandir.c
index 74a3c6e..33415d0 100644 (file)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)scandir.c  5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)scandir.c  5.6 (Berkeley) %G%";
 #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.
  * Returns the number of entries and a pointer to a list of pointers to
 #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.
  * Returns the number of entries and a pointer to a list of pointers to
- * struct direct (through namelist). Returns -1 if there were any errors.
+ * struct dirent (through namelist). Returns -1 if there were any errors.
  */
 
 #include <sys/types.h>
 #include <sys/stat.h>
  */
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/dir.h>
+#include <dirent.h>
+
+/*
+ * The DIRSIZ macro gives the minimum record length which will hold
+ * the directory entry.  This requires the amount of space in struct dirent
+ * without the d_name field, plus enough space for the name with a terminating
+ * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
+ */
+#undef DIRSIZ
+#define DIRSIZ(dp) \
+    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
 
 scandir(dirname, namelist, select, dcomp)
        char *dirname;
 
 scandir(dirname, namelist, select, dcomp)
        char *dirname;
-       struct direct *(*namelist[]);
+       struct dirent *(*namelist[]);
        int (*select)(), (*dcomp)();
 {
        int (*select)(), (*dcomp)();
 {
-       register struct direct *d, *p, **names;
+       register struct dirent *d, *p, **names;
        register int nitems;
        register int nitems;
-       register char *cp1, *cp2;
        struct stat stb;
        long arraysz;
        DIR *dirp;
        struct stat stb;
        long arraysz;
        DIR *dirp;
@@ -52,7 +61,7 @@ scandir(dirname, namelist, select, dcomp)
         * and dividing it by a multiple of the minimum size entry. 
         */
        arraysz = (stb.st_size / 24);
         * and dividing it by a multiple of the minimum size entry. 
         */
        arraysz = (stb.st_size / 24);
-       names = (struct direct **)malloc(arraysz * sizeof(struct direct *));
+       names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
        if (names == NULL)
                return(-1);
 
        if (names == NULL)
                return(-1);
 
@@ -63,13 +72,13 @@ scandir(dirname, namelist, select, dcomp)
                /*
                 * Make a minimum size copy of the data
                 */
                /*
                 * Make a minimum size copy of the data
                 */
-               p = (struct direct *)malloc(DIRSIZ(d));
+               p = (struct dirent *)malloc(DIRSIZ(d));
                if (p == NULL)
                        return(-1);
                p->d_ino = d->d_ino;
                p->d_reclen = d->d_reclen;
                p->d_namlen = d->d_namlen;
                if (p == NULL)
                        return(-1);
                p->d_ino = d->d_ino;
                p->d_reclen = d->d_reclen;
                p->d_namlen = d->d_namlen;
-               for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
+               bcopy(d->d_name, p->d_name, p->d_namlen + 1);
                /*
                 * Check to make sure the array has space left and
                 * realloc the maximum size.
                /*
                 * Check to make sure the array has space left and
                 * realloc the maximum size.
@@ -78,8 +87,8 @@ scandir(dirname, namelist, select, dcomp)
                        if (fstat(dirp->dd_fd, &stb) < 0)
                                return(-1);     /* just might have grown */
                        arraysz = stb.st_size / 12;
                        if (fstat(dirp->dd_fd, &stb) < 0)
                                return(-1);     /* just might have grown */
                        arraysz = stb.st_size / 12;
-                       names = (struct direct **)realloc((char *)names,
-                               arraysz * sizeof(struct direct *));
+                       names = (struct dirent **)realloc((char *)names,
+                               arraysz * sizeof(struct dirent *));
                        if (names == NULL)
                                return(-1);
                }
                        if (names == NULL)
                                return(-1);
                }
@@ -87,7 +96,7 @@ scandir(dirname, namelist, select, dcomp)
        }
        closedir(dirp);
        if (nitems && dcomp != NULL)
        }
        closedir(dirp);
        if (nitems && dcomp != NULL)
-               qsort(names, nitems, sizeof(struct direct *), dcomp);
+               qsort(names, nitems, sizeof(struct dirent *), dcomp);
        *namelist = names;
        return(nitems);
 }
        *namelist = names;
        return(nitems);
 }
@@ -96,7 +105,7 @@ scandir(dirname, namelist, select, dcomp)
  * Alphabetic order comparison routine for those who want it.
  */
 alphasort(d1, d2)
  * Alphabetic order comparison routine for those who want it.
  */
 alphasort(d1, d2)
-       struct direct **d1, **d2;
+       struct dirent **d1, **d2;
 {
        return(strcmp((*d1)->d_name, (*d2)->d_name));
 }
 {
        return(strcmp((*d1)->d_name, (*d2)->d_name));
 }