17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
37c478bd9Sstevel@tonic-gate * All rights reserved.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
67c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are
77c478bd9Sstevel@tonic-gate * duplicated in all such forms and that any documentation,
87c478bd9Sstevel@tonic-gate * advertising materials, and other materials related to such
97c478bd9Sstevel@tonic-gate * distribution and use acknowledge that the software was developed
107c478bd9Sstevel@tonic-gate * by the University of California, Berkeley. The name of the
117c478bd9Sstevel@tonic-gate * University may not be used to endorse or promote products derived
127c478bd9Sstevel@tonic-gate * from this software without specific prior written permission.
137c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
157c478bd9Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
207c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
217c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
227c478bd9Sstevel@tonic-gate */
237c478bd9Sstevel@tonic-gate
24*5d54f3d8Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
25*5d54f3d8Smuffin
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Scan the directory dirname calling select to make a list of selected
287c478bd9Sstevel@tonic-gate * directory entries then sort using qsort and compare routine dcomp.
297c478bd9Sstevel@tonic-gate * Returns the number of entries and a pointer to a list of pointers to
307c478bd9Sstevel@tonic-gate * struct direct (through namelist). Returns -1 if there were any errors.
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/dir.h>
367c478bd9Sstevel@tonic-gate
37*5d54f3d8Smuffin int
scandir(char * dirname,struct direct * (* namelist[]),int (* select)(),int (* dcomp)())38*5d54f3d8Smuffin scandir(char *dirname, struct direct *(*namelist[]),
39*5d54f3d8Smuffin int (*select)(), int (*dcomp)())
407c478bd9Sstevel@tonic-gate {
41*5d54f3d8Smuffin struct direct *d, *p, **names;
42*5d54f3d8Smuffin int nitems;
43*5d54f3d8Smuffin char *cp1, *cp2;
447c478bd9Sstevel@tonic-gate struct stat stb;
457c478bd9Sstevel@tonic-gate long arraysz;
467c478bd9Sstevel@tonic-gate DIR *dirp;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate if ((dirp = opendir(dirname)) == NULL)
497c478bd9Sstevel@tonic-gate return (-1);
507c478bd9Sstevel@tonic-gate if (fstat(dirp->dd_fd, &stb) < 0)
517c478bd9Sstevel@tonic-gate return (-1);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate * estimate the array size by taking the size of the directory file
557c478bd9Sstevel@tonic-gate * and dividing it by a multiple of the minimum size entry.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate arraysz = (stb.st_size / 24);
587c478bd9Sstevel@tonic-gate names = (struct direct **)malloc(arraysz * sizeof(struct direct *));
597c478bd9Sstevel@tonic-gate if (names == NULL)
607c478bd9Sstevel@tonic-gate return (-1);
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate nitems = 0;
637c478bd9Sstevel@tonic-gate while ((d = readdir(dirp)) != NULL) {
647c478bd9Sstevel@tonic-gate if (select != NULL && !(*select)(d))
657c478bd9Sstevel@tonic-gate continue; /* just selected names */
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Make a minimum size copy of the data
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate p = (struct direct *)malloc(DIRSIZ(d));
707c478bd9Sstevel@tonic-gate if (p == NULL)
717c478bd9Sstevel@tonic-gate return (-1);
727c478bd9Sstevel@tonic-gate p->d_ino = d->d_ino;
737c478bd9Sstevel@tonic-gate p->d_reclen = d->d_reclen;
747c478bd9Sstevel@tonic-gate p->d_namlen = d->d_namlen;
757c478bd9Sstevel@tonic-gate for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * Check to make sure the array has space left and
787c478bd9Sstevel@tonic-gate * realloc the maximum size.
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate if (++nitems >= arraysz) {
817c478bd9Sstevel@tonic-gate if (fstat(dirp->dd_fd, &stb) < 0)
827c478bd9Sstevel@tonic-gate return (-1); /* just might have grown */
837c478bd9Sstevel@tonic-gate arraysz = stb.st_size / 12;
847c478bd9Sstevel@tonic-gate names = (struct direct **)realloc((char *)names,
857c478bd9Sstevel@tonic-gate arraysz * sizeof(struct direct *));
867c478bd9Sstevel@tonic-gate if (names == NULL)
877c478bd9Sstevel@tonic-gate return (-1);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate names[nitems-1] = p;
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate closedir(dirp);
927c478bd9Sstevel@tonic-gate if (nitems && dcomp != NULL)
937c478bd9Sstevel@tonic-gate qsort(names, nitems, sizeof(struct direct *), dcomp);
947c478bd9Sstevel@tonic-gate *namelist = names;
957c478bd9Sstevel@tonic-gate return (nitems);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Alphabetic order comparison routine for those who want it.
1007c478bd9Sstevel@tonic-gate */
101*5d54f3d8Smuffin int
alphasort(struct direct ** d1,struct direct ** d2)102*5d54f3d8Smuffin alphasort(struct direct **d1, struct direct **d2)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate return (strcmp((*d1)->d_name, (*d2)->d_name));
1057c478bd9Sstevel@tonic-gate }
106