A companion function of scandir, also added in POSIX:2008, is alphasort. This adds the replacement, from glibc.
2009-01-18 Bruno Haible <br...@clisp.org> New module 'alphasort'. * lib/dirent.in.h (alphasort): New declaration. * lib/alphasort.c: New file, from glibc with modifications. * m4/alphasort.m4: New file. * modules/alphasort: New file. * m4/dirent_h.m4 (gl_DIRENT_H_DEFAULTS): Initialize GNULIB_ALPHASORT, HAVE_ALPHASORT. * modules/dirent (Makefile.am): Substitute GNULIB_ALPHASORT, HAVE_ALPHASORT. * doc/posix-functions/alphasort.texi: Mention the new module and the portability problems. =============================== lib/alphasort.c =============================== /* Copyright (C) 1992, 1997, 1998, 2009 Free Software Foundation, Inc. This file is part of the GNU C Library. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include <config.h> #include <dirent.h> #include <string.h> int alphasort (const struct dirent **a, const struct dirent **b) { return strcoll ((*a)->d_name, (*b)->d_name); } =============================== m4/alphasort.m4 =============================== # alphasort.m4 serial 1 dnl Copyright (C) 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_ALPHASORT], [ AC_REQUIRE([gl_DIRENT_H_DEFAULTS]) dnl Persuade glibc and Solaris <dirent.h> to declare alphasort(). AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) AC_CHECK_FUNCS([alphasort]) if test $ac_cv_func_alphasort = no; then HAVE_ALPHASORT=0 AC_LIBOBJ([alphasort]) gl_PREREQ_ALPHASORT fi ]) # Prerequisites of lib/alphasort.c. AC_DEFUN([gl_PREREQ_ALPHASORT], [:]) ============================== modules/alphasort ============================== Description: alphasort() function: compare entries of a directory alphabetically Files: lib/alphasort.c m4/alphasort.m4 Depends-on: dirent extensions configure.ac: gl_FUNC_ALPHASORT gl_DIRENT_MODULE_INDICATOR([alphasort]) Makefile.am: Include: <dirent.h> License: LGPL Maintainer: all, glibc =============================================================================== --- lib/dirent.in.h.orig 2009-01-18 12:36:48.000000000 +0100 +++ lib/dirent.in.h 2009-01-18 12:31:04.000000000 +0100 @@ -76,6 +76,19 @@ scandir (d, n, f, c)) #endif +#if @GNULIB_ALPHASORT@ +/* Compare two 'struct dirent' entries alphabetically. */ +# if !...@have_alphasort@ +extern int alphasort (const struct dirent **, const struct dirent **); +# endif +#elif defined GNULIB_POSIXCHECK +# undef alphasort +# define alphasort(a,b) \ + (GL_LINK_WARNING ("alphasort is unportable - " \ + "use gnulib module alphasort for portability"), \ + alphasort (a, b)) +#endif + #ifdef __cplusplus } #endif --- m4/dirent_h.m4.orig 2009-01-18 12:36:48.000000000 +0100 +++ m4/dirent_h.m4 2009-01-18 12:32:18.000000000 +0100 @@ -1,4 +1,4 @@ -# dirent_h.m4 serial 3 +# dirent_h.m4 serial 4 dnl Copyright (C) 2008-2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -32,10 +32,12 @@ AC_DEFUN([gl_DIRENT_H_DEFAULTS], [ AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) dnl for REPLACE_FCHDIR - GNULIB_DIRFD=0; AC_SUBST([GNULIB_DIRFD]) - GNULIB_SCANDIR=0; AC_SUBST([GNULIB_SCANDIR]) + GNULIB_DIRFD=0; AC_SUBST([GNULIB_DIRFD]) + GNULIB_SCANDIR=0; AC_SUBST([GNULIB_SCANDIR]) + GNULIB_ALPHASORT=0; AC_SUBST([GNULIB_ALPHASORT]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_DECL_DIRFD=1; AC_SUBST([HAVE_DECL_DIRFD]) HAVE_SCANDIR=1; AC_SUBST([HAVE_SCANDIR]) + HAVE_ALPHASORT=1; AC_SUBST([HAVE_ALPHASORT]) DIRENT_H=''; AC_SUBST([DIRENT_H]) ]) --- modules/dirent.orig 2009-01-18 12:36:48.000000000 +0100 +++ modules/dirent 2009-01-18 12:31:39.000000000 +0100 @@ -26,8 +26,10 @@ -e 's|@''NEXT_DIRENT_H''@|$(NEXT_DIRENT_H)|g' \ -e 's|@''GNULIB_DIRFD''@|$(GNULIB_DIRFD)|g' \ -e 's|@''GNULIB_SCANDIR''@|$(GNULIB_SCANDIR)|g' \ + -e 's|@''GNULIB_ALPHASORT''@|$(GNULIB_ALPHASORT)|g' \ -e 's|@''HAVE_DECL_DIRFD''@|$(HAVE_DECL_DIRFD)|g' \ -e 's|@''HAVE_SCANDIR''@|$(HAVE_SCANDIR)|g' \ + -e 's|@''HAVE_ALPHASORT''@|$(HAVE_ALPHASORT)|g' \ -e 's|@''REPLACE_FCHDIR''@|$(REPLACE_FCHDIR)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ < $(srcdir)/dirent.in.h; \ --- doc/posix-functions/alphasort.texi.orig 2009-01-18 12:36:48.000000000 +0100 +++ doc/posix-functions/alphasort.texi 2009-01-18 12:14:38.000000000 +0100 @@ -4,15 +4,21 @@ POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/alphasort.html} -Gnulib module: --- +Gnulib module: alphasort Portability problems fixed by Gnulib: @itemize +...@item +This function is missing on some platforms: +Solaris 9, mingw, BeOS. @end itemize Portability problems not fixed by Gnulib: @itemize @item -This function is missing on some platforms: -Solaris 9, mingw, BeOS. +The parameters of this function are declared as @code{const void *} on some platforms: +glibc 2.3.6, MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, Interix 3.5. +...@item +The parameters of this function are declared as @code{void *} on some platforms: +AIX 5.1. @end itemize