From owner-svn-src-all@FreeBSD.ORG Sun Jan 24 12:35:37 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1AAAC106566B; Sun, 24 Jan 2010 12:35:37 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 062968FC0A; Sun, 24 Jan 2010 12:35:37 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o0OCZau6029466; Sun, 24 Jan 2010 12:35:36 GMT (envelope-from kib@svn.freebsd.org) Received: (from kib@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o0OCZaZT029457; Sun, 24 Jan 2010 12:35:36 GMT (envelope-from kib@svn.freebsd.org) Message-Id: <201001241235.o0OCZaZT029457@svn.freebsd.org> From: Konstantin Belousov Date: Sun, 24 Jan 2010 12:35:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r202921 - in stable/8: include lib/libc/gen usr.bin/catman usr.bin/makewhatis usr.sbin/lpr/common_source X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Jan 2010 12:35:37 -0000 Author: kib Date: Sun Jan 24 12:35:36 2010 New Revision: 202921 URL: http://svn.freebsd.org/changeset/base/202921 Log: Merge scandir(3) interface update to stable/8. MFC r201512: Modernize scandir(3) and alphasort(3) interfaces according to the IEEE Std 1003.1-2008. MFC r201602: Move scandir(3) and alphasort(3) into XSI namespace. MFC r201604: Use thunks to adapt alphasort-like interface to the comparision function required by qsort() and qsort_r(). MFC r202556 (by ache): Use strcoll() in opendir() and alphasort(). Remove some comments. MFC r202572 (by ache): Revert to using strcmp() for opendir(). MFC r202677 (by ache): Style. MFC r202679 (by ache): Style: rename internal function to opendir_compar(). MFC r202691 (by ache): For alphasort(3) add reference to strcoll(3). MFC r202693 (by ache): Style: reword comment. Modified: stable/8/include/dirent.h stable/8/lib/libc/gen/opendir.c stable/8/lib/libc/gen/scandir.3 stable/8/lib/libc/gen/scandir.c stable/8/usr.bin/catman/catman.c stable/8/usr.bin/makewhatis/makewhatis.c stable/8/usr.sbin/lpr/common_source/lp.h stable/8/usr.sbin/lpr/common_source/rmjob.c Directory Properties: stable/8/include/ (props changed) stable/8/lib/libc/ (props changed) stable/8/lib/libc/stdtime/ (props changed) stable/8/usr.bin/catman/ (props changed) stable/8/usr.bin/makewhatis/ (props changed) stable/8/usr.sbin/lpr/ (props changed) Modified: stable/8/include/dirent.h ============================================================================== --- stable/8/include/dirent.h Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/include/dirent.h Sun Jan 24 12:35:36 2010 (r202921) @@ -93,9 +93,11 @@ typedef void * DIR; #ifndef _KERNEL __BEGIN_DECLS +#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE >= 700 +int alphasort(const struct dirent **, const struct dirent **); +#endif #if __BSD_VISIBLE DIR *__opendir2(const char *, int); -int alphasort(const void *, const void *); int getdents(int, char *, int); int getdirentries(int, char *, int, long *); #endif @@ -107,9 +109,10 @@ struct dirent * int readdir_r(DIR *, struct dirent *, struct dirent **); #endif void rewinddir(DIR *); -#if __BSD_VISIBLE +#if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE >= 700 int scandir(const char *, struct dirent ***, - int (*)(struct dirent *), int (*)(const void *, const void *)); + int (*)(const struct dirent *), int (*)(const struct dirent **, + const struct dirent **)); #endif #if __XSI_VISIBLE void seekdir(DIR *, long); Modified: stable/8/lib/libc/gen/opendir.c ============================================================================== --- stable/8/lib/libc/gen/opendir.c Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/lib/libc/gen/opendir.c Sun Jan 24 12:35:36 2010 (r202921) @@ -92,6 +92,14 @@ __opendir2(const char *name, int flags) return __opendir_common(fd, name, flags); } +static int +opendir_compar(const void *p1, const void *p2) +{ + + return (strcmp((*(const struct dirent **)p1)->d_name, + (*(const struct dirent **)p2)->d_name)); +} + /* * Common routine for opendir(3), __opendir2(3) and fdopendir(3). */ @@ -240,7 +248,8 @@ __opendir_common(int fd, const char *nam /* * This sort must be stable. */ - mergesort(dpv, n, sizeof(*dpv), alphasort); + mergesort(dpv, n, sizeof(*dpv), + opendir_compar); dpv[n] = NULL; xp = NULL; Modified: stable/8/lib/libc/gen/scandir.3 ============================================================================== --- stable/8/lib/libc/gen/scandir.3 Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/lib/libc/gen/scandir.3 Sun Jan 24 12:35:36 2010 (r202921) @@ -28,7 +28,7 @@ .\" @(#)scandir.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd June 4, 1993 +.Dd January 3, 2010 .Dt SCANDIR 3 .Os .Sh NAME @@ -38,12 +38,11 @@ .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In sys/types.h .In dirent.h .Ft int -.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \\*(lp*select\\*(rp\\*(lpstruct dirent *\\*(rp" "int \\*(lp*compar\\*(rp\\*(lpconst void *, const void *\\*(rp" +.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \\*(lp*select\\*(rp\\*(lpconst struct dirent *\\*(rp" "int \\*(lp*compar\\*(rp\\*(lpconst struct dirent **, const struct dirent **\\*(rp" .Ft int -.Fn alphasort "const void *d1" "const void *d2" +.Fn alphasort "const struct dirent **d1" "const struct dirent **d2" .Sh DESCRIPTION The .Fn scandir @@ -82,7 +81,8 @@ The function is a routine which can be used for the .Fa compar -argument to sort the array alphabetically. +argument to sort the array alphabetically using +.Xr strcoll 3 . .Pp The memory allocated for the array can be deallocated with .Xr free 3 , @@ -95,7 +95,8 @@ cannot allocate enough memory to hold al .Xr directory 3 , .Xr malloc 3 , .Xr qsort 3 , -.Xr dir 5 +.Xr dir 5 , +.Xr strcoll 3 .Sh HISTORY The .Fn scandir Modified: stable/8/lib/libc/gen/scandir.c ============================================================================== --- stable/8/lib/libc/gen/scandir.c Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/lib/libc/gen/scandir.c Sun Jan 24 12:35:36 2010 (r202921) @@ -46,6 +46,8 @@ __FBSDID("$FreeBSD$"); #include #include "un-namespace.h" +static int alphasort_thunk(void *thunk, const void *p1, const void *p2); + /* * The DIRSIZ macro is the minimum record length which will hold the directory * entry. This requires the amount of space in struct dirent without the @@ -58,11 +60,9 @@ __FBSDID("$FreeBSD$"); (((dp)->d_namlen + 1 + 3) &~ 3)) int -scandir(dirname, namelist, select, dcomp) - const char *dirname; - struct dirent ***namelist; - int (*select)(struct dirent *); - int (*dcomp)(const void *, const void *); +scandir(const char *dirname, struct dirent ***namelist, + int (*select)(const struct dirent *), int (*dcomp)(const struct dirent **, + const struct dirent **)) { struct dirent *d, *p, **names = NULL; size_t nitems = 0; @@ -111,26 +111,35 @@ scandir(dirname, namelist, select, dcomp } closedir(dirp); if (nitems && dcomp != NULL) - qsort(names, nitems, sizeof(struct dirent *), dcomp); + qsort_r(names, nitems, sizeof(struct dirent *), + &dcomp, alphasort_thunk); *namelist = names; - return(nitems); + return (nitems); fail: while (nitems > 0) free(names[--nitems]); free(names); closedir(dirp); - return -1; + return (-1); } /* * Alphabetic order comparison routine for those who want it. + * POSIX 2008 requires that alphasort() uses strcoll(). */ int -alphasort(d1, d2) - const void *d1; - const void *d2; +alphasort(const struct dirent **d1, const struct dirent **d2) +{ + + return (strcoll((*d1)->d_name, (*d2)->d_name)); +} + +static int +alphasort_thunk(void *thunk, const void *p1, const void *p2) { - return(strcmp((*(struct dirent **)d1)->d_name, - (*(struct dirent **)d2)->d_name)); + int (*dc)(const struct dirent **, const struct dirent **); + + dc = *(int (**)(const struct dirent **, const struct dirent **))thunk; + return (dc((const struct dirent **)p1, (const struct dirent **)p2)); } Modified: stable/8/usr.bin/catman/catman.c ============================================================================== --- stable/8/usr.bin/catman/catman.c Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/usr.bin/catman/catman.c Sun Jan 24 12:35:36 2010 (r202921) @@ -589,9 +589,15 @@ process_section(char *mandir, char *sect } static int -select_sections(struct dirent *entry) +select_sections(const struct dirent *entry) { - return directory_type(entry->d_name) == MAN_SECTION_DIR; + char *name; + int ret; + + name = strdup(entry->d_name); + ret = directory_type(name) == MAN_SECTION_DIR; + free(name); + return (ret); } /* Modified: stable/8/usr.bin/makewhatis/makewhatis.c ============================================================================== --- stable/8/usr.bin/makewhatis/makewhatis.c Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/usr.bin/makewhatis/makewhatis.c Sun Jan 24 12:35:36 2010 (r202921) @@ -879,9 +879,9 @@ process_section(char *section_dir) * Returns whether the directory entry is a man page section. */ static int -select_sections(struct dirent *entry) +select_sections(const struct dirent *entry) { - char *p = &entry->d_name[3]; + const char *p = &entry->d_name[3]; if (strncmp(entry->d_name, "man", 3) != 0) return 0; Modified: stable/8/usr.sbin/lpr/common_source/lp.h ============================================================================== --- stable/8/usr.sbin/lpr/common_source/lp.h Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/usr.sbin/lpr/common_source/lp.h Sun Jan 24 12:35:36 2010 (r202921) @@ -280,7 +280,7 @@ void inform(const struct printer *_pp, void init_printer(struct printer *_pp); void init_request(struct request *_rp); int inlist(char *_uname, char *_cfile); -int iscf(struct dirent *_d); +int iscf(const struct dirent *_d); void ldump(const char *_nfile, const char *_datafile, int _copies); void lastprinter(void); int lockchk(struct printer *_pp, char *_slockf); Modified: stable/8/usr.sbin/lpr/common_source/rmjob.c ============================================================================== --- stable/8/usr.sbin/lpr/common_source/rmjob.c Sun Jan 24 12:22:38 2010 (r202920) +++ stable/8/usr.sbin/lpr/common_source/rmjob.c Sun Jan 24 12:35:36 2010 (r202921) @@ -384,7 +384,7 @@ rmremote(const struct printer *pp) * Return 1 if the filename begins with 'cf' */ int -iscf(struct dirent *d) +iscf(const struct dirent *d) { return(d->d_name[0] == 'c' && d->d_name[1] == 'f'); }