1 '\" te
   2 .\" Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
   3 .\" 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.
   4 .\" 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.
   5 .\" 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]
   6 .TH SCANDIR 3C "May 4, 2004"
   7 .SH NAME
   8 scandir, alphasort \- scan a directory
   9 .SH SYNOPSIS
  10 .LP
  11 .nf
  12 #include <sys/types.h>
  13 #include <dirent.h>
  14 
  15 \fBint\fR \fBscandir\fR(\fBconst char *\fR\fIdirname\fR, \fBstruct dirent *\fR(*\fInamelist\fR[]),
  16      \fBint\fR (*\fIselect\fR)(const struct dirent *),
  17      \fBint\fR (*\fIdcomp\fR)(\fBconst struct dirent  **\fR,
  18      \fBconst struct dirent **\fR));
  19 .fi
  20 
  21 .LP
  22 .nf
  23 \fBint\fR \fBalphasort\fR(\fBconst struct dirent **\fR\fId1\fR,
  24      \fBconst struct dirent **\fR\fId2\fR);
  25 .fi
  26 
  27 .SH DESCRIPTION
  28 .sp
  29 .LP
  30 The \fBscandir()\fR function reads the directory \fIdirname\fR using
  31 \fBreaddir\fR(3C) and builds an array of pointers to directory entries using
  32 \fBmalloc\fR(3C). The \fInamelist\fR argument is a pointer to an array of
  33 structure pointers. The \fIselect\fR argument is a pointer to a routine that is
  34 called with a pointer to a directory entry and returns a non-zero value if the
  35 directory entry is included in the array. If this pointer is \fINULL\fR, then
  36 all the directory entries are included. The \fIdcomp\fR argument is a pointer
  37 to a routine that is passed to \fBqsort\fR(3C), which sorts the completed
  38 array. If this pointer is \fINULL\fR, the array is not sorted.
  39 .sp
  40 .LP
  41 The \fBalphasort()\fR function can be used as the \fIdcomp\fR() function
  42 parameter for the \fBscandir()\fR function to sort the directory entries into
  43 alphabetical order, as if by the \fBstrcoll\fR(3C) function. Its arguments are
  44 the two directory entries to compare.
  45 .SH RETURN VALUES
  46 .sp
  47 .LP
  48 The \fBscandir()\fR function returns the number of entries in the array and a
  49 pointer to the array through the \fInamelist\fR argument. When an error is
  50 encountered, \fBscandir()\fR returns -1 and \fBerrno\fR is set to indicate the
  51 error.
  52 .sp
  53 .LP
  54 The \fBalphasort()\fR function returns an integer greater than, equal to, or
  55 less than 0 if the directory entry name pointed to by \fId1\fR is greater than,
  56 equal to, or less than the directory entry name pointed to by \fId2\fR when
  57 both are interpreted as appropriate to the current locale. There is no return
  58 value reserved to indicate an error.
  59 .SH ERRORS
  60 .sp
  61 .LP
  62 The \fBscandir()\fR function will fail if:
  63 .sp
  64 .ne 2
  65 .na
  66 \fB\fBEOVERFLOW\fR\fR
  67 .ad
  68 .RS 13n
  69 The number of directory entries exceeds the number that can be represented by
  70 an \fBint\fR.
  71 .RE
  72 
  73 .SH USAGE
  74 .sp
  75 .LP
  76 The \fBscandir()\fR and \fBalphasort()\fR functions have transitional
  77 interfaces for 64-bit file offsets. See \fBlf64\fR(5).
  78 .SH ATTRIBUTES
  79 .sp
  80 .LP
  81 See \fBattributes\fR(5) for descriptions of the following attributes:
  82 .sp
  83 
  84 .sp
  85 .TS
  86 box;
  87 c | c
  88 l | l .
  89 ATTRIBUTE TYPE  ATTRIBUTE VALUE
  90 _
  91 Interface Stability     Stable
  92 _
  93 MT-Level        See below.
  94 .TE
  95 
  96 .sp
  97 .LP
  98 The \fBscandir()\fR function is Unsafe. The \fBalphasort()\fR function is Safe.
  99 .SH SEE ALSO
 100 .sp
 101 .LP
 102 \fBmalloc\fR(3C), \fBqsort\fR(3C), \fBreaddir\fR(3C), \fBstrcoll\fR(3C),
 103 \fBattributes\fR(5), \fBlf64\fR(5)