git: f862c31a4159 - stable/13 - ln: Use stdbool, style nits.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 04 Apr 2024 11:39:04 UTC
The branch stable/13 has been updated by des:

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

commit f862c31a41599206b4bda19a10fc5d5b6256e715
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-03-27 10:03:49 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-04-04 09:51:05 +0000

    ln: Use stdbool, style nits.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    imp, allanjude
    Differential Revision:  https://reviews.freebsd.org/D44511
    
    (cherry picked from commit 437d53daf71a357aae960881ef037564736f7441)
    
    ln: Clean up and simplify tests.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D44512
    
    (cherry picked from commit e0afcbc85690b6464706ead57103baa0493fdfb2)
    
    ln: Add a test case for ln -sfF.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    allanjude, asomers
    Differential Revision:  https://reviews.freebsd.org/D44513
    
    (cherry picked from commit 2ae8d34666a6dea4c1c77a1905c7b8cebd6a21f9)
---
 bin/ln/ln.c             | 76 ++++++++++++++++++++++---------------------
 bin/ln/tests/ln_test.sh | 85 +++++++++++++++++++++----------------------------
 2 files changed, 76 insertions(+), 85 deletions(-)

diff --git a/bin/ln/ln.c b/bin/ln/ln.c
index 546bfba1c7c3..a0e19d702dea 100644
--- a/bin/ln/ln.c
+++ b/bin/ln/ln.c
@@ -49,24 +49,25 @@ static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
 #include <fcntl.h>
 #include <libgen.h>
 #include <limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-static int	fflag;			/* Unlink existing files. */
-static int	Fflag;			/* Remove empty directories also. */
-static int	hflag;			/* Check new name for symlink first. */
-static int	iflag;			/* Interactive mode. */
-static int	Pflag;			/* Create hard links to symlinks. */
-static int	sflag;			/* Symbolic, not hard, link. */
-static int	vflag;			/* Verbose output. */
-static int	wflag;			/* Warn if symlink target does not
+static bool	fflag;			/* Unlink existing files. */
+static bool	Fflag;			/* Remove empty directories also. */
+static bool	hflag;			/* Check new name for symlink first. */
+static bool	iflag;			/* Interactive mode. */
+static bool	Pflag;			/* Create hard links to symlinks. */
+static bool	sflag;			/* Symbolic, not hard, link. */
+static bool	vflag;			/* Verbose output. */
+static bool	wflag;			/* Warn if symlink target does not
 					 * exist, and -f is not enabled. */
 static char	linkch;
 
-static int	linkit(const char *, const char *, int);
-static void	usage(void);
+static int	linkit(const char *, const char *, bool);
+static void	usage(void) __dead2;
 
 int
 main(int argc, char *argv[])
@@ -91,41 +92,41 @@ main(int argc, char *argv[])
 		argv += optind;
 		if (argc != 2)
 			usage();
-		exit(linkit(argv[0], argv[1], 0));
+		exit(linkit(argv[0], argv[1], false));
 	}
 
 	while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
 		switch (ch) {
 		case 'F':
-			Fflag = 1;
+			Fflag = true;
 			break;
 		case 'L':
-			Pflag = 0;
+			Pflag = false;
 			break;
 		case 'P':
-			Pflag = 1;
+			Pflag = true;
 			break;
 		case 'f':
-			fflag = 1;
-			iflag = 0;
-			wflag = 0;
+			fflag = true;
+			iflag = false;
+			wflag = false;
 			break;
 		case 'h':
 		case 'n':
-			hflag = 1;
+			hflag = true;
 			break;
 		case 'i':
-			iflag = 1;
-			fflag = 0;
+			iflag = true;
+			fflag = false;
 			break;
 		case 's':
-			sflag = 1;
+			sflag = true;
 			break;
 		case 'v':
-			vflag = 1;
+			vflag = true;
 			break;
 		case 'w':
-			wflag = 1;
+			wflag = true;
 			break;
 		case '?':
 		default:
@@ -136,21 +137,21 @@ main(int argc, char *argv[])
 	argc -= optind;
 
 	linkch = sflag ? '-' : '=';
-	if (sflag == 0)
-		Fflag = 0;
-	if (Fflag == 1 && iflag == 0) {
-		fflag = 1;
-		wflag = 0;		/* Implied when fflag != 0 */
+	if (!sflag)
+		Fflag = false;
+	if (Fflag && !iflag) {
+		fflag = true;
+		wflag = false;		/* Implied when fflag is true */
 	}
 
-	switch(argc) {
+	switch (argc) {
 	case 0:
 		usage();
 		/* NOTREACHED */
 	case 1:				/* ln source */
-		exit(linkit(argv[0], ".", 1));
+		exit(linkit(argv[0], ".", true));
 	case 2:				/* ln source target */
-		exit(linkit(argv[0], argv[1], 0));
+		exit(linkit(argv[0], argv[1], false));
 	default:
 		;
 	}
@@ -169,7 +170,7 @@ main(int argc, char *argv[])
 	if (!S_ISDIR(sb.st_mode))
 		usage();
 	for (exitval = 0; *argv != targetdir; ++argv)
-		exitval |= linkit(*argv, targetdir, 1);
+		exitval |= linkit(*argv, targetdir, true);
 	exit(exitval);
 }
 
@@ -220,14 +221,15 @@ samedirent(const char *path1, const char *path2)
 }
 
 static int
-linkit(const char *source, const char *target, int isdir)
+linkit(const char *source, const char *target, bool isdir)
 {
-	struct stat sb;
-	const char *p;
-	int ch, exists, first;
 	char path[PATH_MAX];
 	char wbuf[PATH_MAX];
 	char bbuf[PATH_MAX];
+	struct stat sb;
+	const char *p;
+	int ch, first;
+	bool exists;
 
 	if (!sflag) {
 		/* If source doesn't exist, quit now. */
@@ -290,7 +292,7 @@ linkit(const char *source, const char *target, int isdir)
 	/*
 	 * If the file exists, first check it is not the same directory entry.
 	 */
-	exists = !lstat(target, &sb);
+	exists = lstat(target, &sb) == 0;
 	if (exists) {
 		if (!sflag && samedirent(source, target)) {
 			warnx("%s and %s are the same directory entry",
diff --git a/bin/ln/tests/ln_test.sh b/bin/ln/tests/ln_test.sh
index 75fda4ce2dd7..8e5dcf81e61f 100644
--- a/bin/ln/tests/ln_test.sh
+++ b/bin/ln/tests/ln_test.sh
@@ -1,4 +1,6 @@
 #
+# SPDX-License-Identifier: BSD-2-Clause
+#
 # Copyright 2017 Shivansh Rai
 # All rights reserved.
 #
@@ -23,13 +25,15 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 #
-#
 
-set_umask()
+atf_check_same_file()
 {
-	if ! umask 022; then
-		atf_fail "setting umask failed"
-	fi
+	atf_check_equal "$(stat -f %d,%i "$1")" "$(stat -f %d,%i "$2")"
+}
+
+atf_check_symlink_to()
+{
+	atf_check -o inline:"$1\n" readlink "$2"
 }
 
 atf_test_case L_flag
@@ -39,18 +43,13 @@ L_flag_head()
 			"symbolic link, '-L' option creates a hard" \
 			"link to the target of the symbolic link"
 }
-
 L_flag_body()
 {
-	set_umask
 	atf_check touch A
 	atf_check ln -s A B
 	atf_check ln -L B C
-	stat_A=$(stat -f %i A)
-	stat_C=$(stat -f %i C)
-	atf_check_equal "$stat_A" "$stat_C"
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
-	atf_check -o inline:'A\n' readlink B
+	atf_check_same_file A C
+	atf_check_symlink_to A B
 }
 
 atf_test_case P_flag
@@ -60,16 +59,12 @@ P_flag_head()
 			"symbolic link, '-P' option creates a hard " \
 			"link to the symbolic link itself"
 }
-
 P_flag_body()
 {
-	set_umask
 	atf_check touch A
 	atf_check ln -s A B
 	atf_check ln -P B C
-	stat_B=$(stat -f %i B)
-	stat_C=$(stat -f %i C)
-	atf_check_equal "$stat_B" "$stat_C"
+	atf_check_same_file B C
 }
 
 atf_test_case f_flag
@@ -78,15 +73,11 @@ f_flag_head()
 	atf_set "descr" "Verify that if the target file already exists, " \
 			"'-f' option unlinks it so that link may occur"
 }
-
 f_flag_body()
 {
-	set_umask
 	atf_check touch A B
 	atf_check ln -f A B
-	stat_A=$(stat -f %i A)
-	stat_B=$(stat -f %i B)
-	atf_check_equal "$stat_A" "$stat_B"
+	atf_check_same_file A B
 }
 
 atf_test_case target_exists_hard
@@ -95,10 +86,8 @@ target_exists_hard_head()
 	atf_set "descr" "Verify whether creating a hard link fails if the " \
 			"target file already exists"
 }
-
 target_exists_hard_body()
 {
-	set_umask
 	atf_check touch A B
 	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
 		ln A B
@@ -110,10 +99,8 @@ target_exists_symbolic_head()
 	atf_set "descr" "Verify whether creating a symbolic link fails if " \
 			"the target file already exists"
 }
-
 target_exists_symbolic_body()
 {
-	set_umask
 	atf_check touch A B
 	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
 		ln -s A B
@@ -124,13 +111,12 @@ shf_flag_dir_head() {
 	atf_set "descr" "Verify that if the target directory is a symbolic " \
 			"link, '-shf' option prevents following the link"
 }
-
 shf_flag_dir_body()
 {
 	atf_check mkdir -m 0777 A B
 	atf_check ln -s A C
 	atf_check ln -shf B C
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT C
+	atf_check test -L C
 	atf_check -o inline:'B\n' readlink C
 }
 
@@ -139,14 +125,12 @@ snf_flag_dir_head() {
 	atf_set "descr" "Verify that if the target directory is a symbolic " \
 			"link, '-snf' option prevents following the link"
 }
-
 snf_flag_dir_body()
 {
 	atf_check mkdir -m 0777 A B
 	atf_check ln -s A C
 	atf_check ln -snf B C
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT C
-	atf_check -o inline:'B\n' readlink C
+	atf_check_symlink_to B C
 }
 
 atf_test_case sF_flag
@@ -156,13 +140,11 @@ sF_flag_head()
 			"and is a directory, then '-sF' option removes " \
 			"it so that the link may occur"
 }
-
 sF_flag_body()
 {
 	atf_check mkdir A B
 	atf_check ln -sF A B
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
-	atf_check -o inline:'A\n' readlink B
+	atf_check_symlink_to A B
 }
 
 atf_test_case sf_flag
@@ -172,14 +154,27 @@ sf_flag_head()
 			"'-sf' option unlinks it and creates a symbolic link " \
 			"to the source file"
 }
-
 sf_flag_body()
 {
-	set_umask
 	atf_check touch A B
 	atf_check ln -sf A B
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
-	atf_check -o inline:'A\n' readlink B
+	atf_check_symlink_to A B
+}
+
+atf_test_case sfF_flag
+sfF_flag_head()
+{
+	atf_set "descr" "Verify that if the target file already exists " \
+			"and is a symlink, then '-sfF' option removes " \
+			"it so that the link may occur"
+}
+sfF_flag_body()
+{
+	atf_check mkdir A B C
+	atf_check ln -sF A C
+	atf_check_symlink_to A C
+	atf_check ln -sfF B C
+	atf_check_symlink_to B C
 }
 
 atf_test_case s_flag
@@ -187,14 +182,11 @@ s_flag_head()
 {
 	atf_set "descr" "Verify that '-s' option creates a symbolic link"
 }
-
 s_flag_body()
 {
-	set_umask
 	atf_check touch A
 	atf_check ln -s A B
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
-	atf_check -o inline:'A\n' readlink B
+	atf_check_symlink_to A B
 }
 
 atf_test_case s_flag_broken
@@ -203,12 +195,10 @@ s_flag_broken_head()
 	atf_set "descr" "Verify that if the source file does not exists, '-s' " \
 			"option creates a broken symbolic link to the source file"
 }
-
 s_flag_broken_body()
 {
 	atf_check ln -s A B
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
-	atf_check -o inline:'A\n' readlink B
+	atf_check_symlink_to A B
 }
 
 atf_test_case sw_flag
@@ -217,13 +207,11 @@ sw_flag_head()
 	atf_set "descr" "Verify that '-sw' option produces a warning if the " \
 			"source of a symbolic link does not currently exist"
 }
-
 sw_flag_body()
 {
 	atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \
 		ln -sw A B
-	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
-	atf_check -o inline:'A\n' readlink B
+	atf_check_symlink_to A B
 }
 
 atf_init_test_cases()
@@ -237,6 +225,7 @@ atf_init_test_cases()
 	atf_add_test_case snf_flag_dir
 	atf_add_test_case sF_flag
 	atf_add_test_case sf_flag
+	atf_add_test_case sfF_flag
 	atf_add_test_case s_flag
 	atf_add_test_case s_flag_broken
 	atf_add_test_case sw_flag