Print this page
Ensured various XPG7 stuff are declared properly in sys/stat.h (and cleanup)
New documentation for wcslen, wcsnlen, wcscasecmp (and friends), wcsdup.
Various other tweaks and markup improvements.
Various tweaks -- add our sections, etc.
Update libraries and sections.
More markup tweaks.
alphasort and scandir are new in XPG7
   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)

   1 .\" Copyright 2014 Garrett D'Amore <garrett@damore.org>
   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 .Dd "Jul 22, 2014"
   7 .Dt SCANDIR 3C
   8 .Os
   9 .Sh NAME
  10 .Nm scandir ,
  11 .Nm alphasort
  12 .Nd scan a directory
  13 .Sh SYNOPSIS
  14 .In dirent.h
  15 .
  16 .Ft int
  17 .Fo scandir
  18 .Fa "const char *dirname"
  19 .Fa "struct dirent ***namelist"
  20 .Fa "int \*(lp*select\*(rp\*(lpconst struct dirent *\*(rp"
  21 .Fa "int \*(lp*dcomp\*(rp\*(lpconst struct dirent  **, const struct dirent **\*(rp"
  22 .Fc
  23 .
  24 .Ft int
  25 .Fn alphasort "const struct dirent **d1" "const struct dirent **d2"
  26 .
  27 .Sh DESCRIPTION
  28 The
  29 .Fn scandir
  30 function reads the directory
  31 .Fa dirname
  32 using
  33 .Xr readdir 3C
  34 and builds an array of pointers to directory entries using
  35 .Xr malloc 3C .
  36 The
  37 .Fa namelist
  38 argument is a pointer to an array of
  39 structure pointers. The
  40 .Fa select
  41 argument is a pointer to a routine that is
  42 called with a pointer to a directory entry and returns a non-zero value if the
  43 directory entry is included in the array. If this pointer is
  44 .Dv NULL ,
  45 then
  46 all the directory entries are included. The
  47 .Fa dcomp
  48 argument is a pointer
  49 to a routine that is passed to
  50 .Xr qsort 3C ,
  51 which sorts the completed array. If this pointer is
  52 .Dv NULL ,
  53 the array is not sorted.
  54 .Lp
  55 The
  56 .Fn alphasort
  57 function can be used as the
  58 .Fn dcomp
  59 function parameter for the
  60 .Fn scandir
  61 function to sort the directory entries into
  62 alphabetical order, as if by the
  63 .Xr strcoll 3C
  64 function. Its arguments are
  65 the two directory entries to compare.
  66 .Sh RETURN VALUES
  67 The
  68 .Fn scandir
  69 function returns the number of entries in the array and a
  70 pointer to the array through the
  71 .Fa namelist
  72 argument. When an error is
  73 encountered,
  74 .Fn scandir
  75 returns -1 and
  76 .Va errno
  77 is set to indicate the error.
  78 .Lp
  79 The
  80 .Fn alphasort
  81 function returns an integer greater than, equal to, or
  82 less than 0 if the directory entry name pointed to by
  83 .Fa d1
  84 is greater than, equal to, or less than the directory entry name pointed to by
  85 .Fa d2
  86 when
  87 both are interpreted as appropriate to the current locale. There is no return
  88 value reserved to indicate an error.
  89 .Sh ERRORS
  90 The
  91 .Fn scandir
  92 function will fail if:
  93 .Bl -tag -width Er
  94 .It Er EOVERFLOW




  95 The number of directory entries exceeds the number that can be represented by
  96 an
  97 .Vt int .
  98 .El
  99 .Sh USAGE
 100 The
 101 .Fn scandir
 102 and
 103 .Fn alphasort
 104 functions have transitional interfaces for 64-bit file offsets. See
 105 .Xr lf64 5 .
 106 .Sh INTERFACE STABILITY
 107 .Sy Standard .
 108 .Sh MT-LEVEL
 109 The
 110 .Fn scandir
 111 function is
 112 .Sy Unsafe .
 113 The
 114 .Fn alphasort
 115 function is
 116 .Sy Safe .
 117 .Sh SEE ALSO
 118 .Xr malloc 3C ,
 119 .Xr qsort 3C ,
 120 .Xr readdir 3C ,
 121 .Xr strcoll 3C ,
 122 .Xr lf64 5 ,
 123 .Xr standards 5
 124 .Sh STANDARDS
 125 The
 126 .Fn alphasort
 127 and
 128 .Fn scandir
 129 functions were introduced in
 130 .St -p1003.1-2008 .