BSD 4_3_Tahoe release
[unix-history] / usr / src / lib / libc / gen / scandir.c
index 33415d0..15d6e45 100644 (file)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)scandir.c  5.6 (Berkeley) %G%";
+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.
  * 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 dirent (through namelist). Returns -1 if there were any errors.
+ * struct direct (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 <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))
+#include <sys/dir.h>
 
 scandir(dirname, namelist, select, dcomp)
        char *dirname;
 
 scandir(dirname, namelist, select, dcomp)
        char *dirname;
-       struct dirent *(*namelist[]);
+       struct direct *(*namelist[]);
        int (*select)(), (*dcomp)();
 {
        int (*select)(), (*dcomp)();
 {
-       register struct dirent *d, *p, **names;
+       register struct direct *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;
@@ -61,7 +52,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 dirent **)malloc(arraysz * sizeof(struct dirent *));
+       names = (struct direct **)malloc(arraysz * sizeof(struct direct *));
        if (names == NULL)
                return(-1);
 
        if (names == NULL)
                return(-1);
 
@@ -72,13 +63,13 @@ scandir(dirname, namelist, select, dcomp)
                /*
                 * Make a minimum size copy of the data
                 */
                /*
                 * Make a minimum size copy of the data
                 */
-               p = (struct dirent *)malloc(DIRSIZ(d));
+               p = (struct direct *)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;
-               bcopy(d->d_name, p->d_name, p->d_namlen + 1);
+               for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
                /*
                 * 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.
@@ -87,8 +78,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 dirent **)realloc((char *)names,
-                               arraysz * sizeof(struct dirent *));
+                       names = (struct direct **)realloc((char *)names,
+                               arraysz * sizeof(struct direct *));
                        if (names == NULL)
                                return(-1);
                }
                        if (names == NULL)
                                return(-1);
                }
@@ -96,7 +87,7 @@ scandir(dirname, namelist, select, dcomp)
        }
        closedir(dirp);
        if (nitems && dcomp != NULL)
        }
        closedir(dirp);
        if (nitems && dcomp != NULL)
-               qsort(names, nitems, sizeof(struct dirent *), dcomp);
+               qsort(names, nitems, sizeof(struct direct *), dcomp);
        *namelist = names;
        return(nitems);
 }
        *namelist = names;
        return(nitems);
 }
@@ -105,7 +96,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 dirent **d1, **d2;
+       struct direct **d1, **d2;
 {
        return(strcmp((*d1)->d_name, (*d2)->d_name));
 }
 {
        return(strcmp((*d1)->d_name, (*d2)->d_name));
 }