git: 7169c947ab41 - stable/14 - man(1): support spaces in filenames

From: Wolfram Schneider <wosch_at_FreeBSD.org>
Date: Sat, 20 Jan 2024 08:55:16 UTC
The branch stable/14 has been updated by wosch:

URL: https://cgit.FreeBSD.org/src/commit/?id=7169c947ab4150a539f2af6c9dcf3ab4344b0033

commit 7169c947ab4150a539f2af6c9dcf3ab4344b0033
Author:     Wolfram Schneider <wosch@FreeBSD.org>
AuthorDate: 2024-01-13 16:16:55 +0000
Commit:     Wolfram Schneider <wosch@FreeBSD.org>
CommitDate: 2024-01-20 08:51:12 +0000

    man(1): support spaces in filenames
    
    The globbing function in exists() needs to handle white spaces.
    
    PR:          275978
    Reviewed by: kevans, bapt, emaste
    Differential Revision: https://reviews.freebsd.org/D43222
    MFC after:   1 week
    
    (cherry picked from commit e3c7b76fa8105929ae9a785e5ffc44274b1b0a81)
---
 usr.bin/man/man.sh | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/usr.bin/man/man.sh b/usr.bin/man/man.sh
index 7e2151295ba7..777726f880db 100755
--- a/usr.bin/man/man.sh
+++ b/usr.bin/man/man.sh
@@ -191,7 +191,9 @@ decho() {
 }
 
 # Usage: exists glob
-# Returns true if glob resolves to a real file.
+#
+# Returns true if glob resolves to a real file and store the first
+# found filename in the variable $found
 exists() {
 	local IFS
 
@@ -201,14 +203,16 @@ exists() {
 	# Use some globbing tricks in the shell to determine if a file
 	# exists or not.
 	set +f
-	set -- "$1" $1
+	for file in "$1"*
+	do
+		if [ -r "$file" ]; then
+			found="$file"
+			set -f
+			return 0
+		fi
+	done
 	set -f
 
-	if [ "$1" != "$2" -a -r "$2" ]; then
-		found="$2"
-		return 0
-	fi
-
 	return 1
 }
 
@@ -230,10 +234,10 @@ find_file() {
 	fi
 	decho "  Searching directory $manroot" 2
 
-	mann="$manroot/$4.$2*"
-	man0="$manroot/$4.0*"
-	catn="$catroot/$4.$2*"
-	cat0="$catroot/$4.0*"
+	mann="$manroot/$4.$2"
+	man0="$manroot/$4.0"
+	catn="$catroot/$4.$2"
+	cat0="$catroot/$4.0"
 
 	# This is the behavior as seen by the original man utility.
 	# Let's not change that which doesn't seem broken.
@@ -313,7 +317,7 @@ man_check_for_so() {
 		.so*)	trim "${line#.so}"
 			decho "$manpage includes $tstr"
 			# Glob and check for the file.
-			if ! check_man "$path/$tstr*" ""; then
+			if ! check_man "$path/$tstr" ""; then
 				decho "  Unable to find $tstr"
 				return 1
 			fi