diff options
| author | Baptiste Daroussin <bapt@FreeBSD.org> | 2017-07-31 19:17:54 +0000 | 
|---|---|---|
| committer | Baptiste Daroussin <bapt@FreeBSD.org> | 2017-07-31 19:17:54 +0000 | 
| commit | 75b6c55cb02aaa5d40a725b90ffb672a4c25e2ee (patch) | |
| tree | 64b1aaf4aa206822fce9515a2b064662d04ef5b4 /mansearch.c | |
| parent | 7ad21139cd51f34b1749abea843f7f513b4a6410 (diff) | |
Notes
Diffstat (limited to 'mansearch.c')
| -rw-r--r-- | mansearch.c | 34 | 
1 files changed, 28 insertions, 6 deletions
| diff --git a/mansearch.c b/mansearch.c index 26a7c23f589c..c889aecf52f2 100644 --- a/mansearch.c +++ b/mansearch.c @@ -104,7 +104,8 @@ mansearch(const struct mansearch *search,  	}  	cur = maxres = 0; -	*res = NULL; +	if (res != NULL) +		*res = NULL;  	outkey = KEY_Nd;  	if (search->outkey != NULL) @@ -173,6 +174,10 @@ mansearch(const struct mansearch *search,  			    lstmatch(search->arch, page->arch) == 0)  				continue; +			if (res == NULL) { +				cur = 1; +				break; +			}  			if (cur + 1 > maxres) {  				maxres += 1024;  				*res = mandoc_reallocarray(*res, @@ -204,12 +209,13 @@ mansearch(const struct mansearch *search,  		if (cur && search->firstmatch)  			break;  	} -	qsort(*res, cur, sizeof(struct manpage), manpage_compare); +	if (res != NULL) +		qsort(*res, cur, sizeof(struct manpage), manpage_compare);  	if (chdir_status && getcwd_status && chdir(buf) == -1)  		warn("%s", buf);  	exprfree(e);  	*sz = cur; -	return 1; +	return res != NULL || cur;  }  /* @@ -388,13 +394,29 @@ static int  manpage_compare(const void *vp1, const void *vp2)  {  	const struct manpage	*mp1, *mp2; +	const char		*cp1, *cp2; +	size_t			 sz1, sz2;  	int			 diff;  	mp1 = vp1;  	mp2 = vp2; -	return (diff = mp2->bits - mp1->bits) ? diff : -	    (diff = mp1->sec - mp2->sec) ? diff : -	    strcasecmp(mp1->names, mp2->names); +	if ((diff = mp2->bits - mp1->bits) || +	    (diff = mp1->sec - mp2->sec)) +		return diff; + +	/* Fall back to alphabetic ordering of names. */ +	sz1 = strcspn(mp1->names, "("); +	sz2 = strcspn(mp2->names, "("); +	if (sz1 < sz2) +		sz1 = sz2; +	if ((diff = strncasecmp(mp1->names, mp2->names, sz1))) +		return diff; + +	/* For identical names and sections, prefer arch-dependent. */ +	cp1 = strchr(mp1->names + sz1, '/'); +	cp2 = strchr(mp2->names + sz2, '/'); +	return cp1 != NULL && cp2 != NULL ? strcasecmp(cp1, cp2) : +	    cp1 != NULL ? -1 : cp2 != NULL ? 1 : 0;  }  static char * | 
