'\" te
.\" Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH SCANDIR 3C "May 4, 2004"
.SH NAME
scandir, alphasort \- scan a directory
.SH SYNOPSIS
.LP
.nf
#include <sys/types.h>
#include <dirent.h>

\fBint\fR \fBscandir\fR(\fBconst char *\fR\fIdirname\fR, \fBstruct dirent *\fR(*\fInamelist\fR[]),
     \fBint\fR (*\fIselect\fR)(const struct dirent *),
     \fBint\fR (*\fIdcomp\fR)(\fBconst struct dirent  **\fR,
     \fBconst struct dirent **\fR));
.fi

.LP
.nf
\fBint\fR \fBalphasort\fR(\fBconst struct dirent **\fR\fId1\fR,
     \fBconst struct dirent **\fR\fId2\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBscandir()\fR function reads the directory \fIdirname\fR using
\fBreaddir\fR(3C) and builds an array of pointers to directory entries using
\fBmalloc\fR(3C). The \fInamelist\fR argument is a pointer to an array of
structure pointers. The \fIselect\fR argument is a pointer to a routine that is
called with a pointer to a directory entry and returns a non-zero value if the
directory entry is included in the array. If this pointer is \fINULL\fR, then
all the directory entries are included. The \fIdcomp\fR argument is a pointer
to a routine that is passed to \fBqsort\fR(3C), which sorts the completed
array. If this pointer is \fINULL\fR, the array is not sorted.
.sp
.LP
The \fBalphasort()\fR function can be used as the \fIdcomp\fR() function
parameter for the \fBscandir()\fR function to sort the directory entries into
alphabetical order, as if by the \fBstrcoll\fR(3C) function. Its arguments are
the two directory entries to compare.
.SH RETURN VALUES
.sp
.LP
The \fBscandir()\fR function returns the number of entries in the array and a
pointer to the array through the \fInamelist\fR argument. When an error is
encountered, \fBscandir()\fR returns -1 and \fBerrno\fR is set to indicate the
error.
.sp
.LP
The \fBalphasort()\fR function returns an integer greater than, equal to, or
less than 0 if the directory entry name pointed to by \fId1\fR is greater than,
equal to, or less than the directory entry name pointed to by \fId2\fR when
both are interpreted as appropriate to the current locale. There is no return
value reserved to indicate an error.
.SH ERRORS
.sp
.LP
The \fBscandir()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 13n
The number of directory entries exceeds the number that can be represented by
an \fBint\fR.
.RE

.SH USAGE
.sp
.LP
The \fBscandir()\fR and \fBalphasort()\fR functions have transitional
interfaces for 64-bit file offsets. See \fBlf64\fR(7).
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Stable
_
MT-Level	See below.
.TE

.sp
.LP
The \fBscandir()\fR function is Unsafe. The \fBalphasort()\fR function is Safe.
.SH SEE ALSO
.sp
.LP
.BR malloc (3C),
.BR qsort (3C),
.BR readdir (3C),
.BR strcoll (3C),
.BR attributes (7),
.BR lf64 (7)