commit: a391c455ff8b8eaa212c1a6258445a8625b7005c
parent a23c423257e11ce3a0ef0f14cf57bade1b20ad0b
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 31 Mar 2024 15:52:52 +0200
cmd/logname: switch from getlogin to $LOGNAME
POSIX mandates $LOGNAME anyway, matches musl's behavior.
Diffstat:
2 files changed, 11 insertions(+), 19 deletions(-)
diff --git a/cmd/logname.c b/cmd/logname.c
@@ -3,23 +3,23 @@
 // SPDX-License-Identifier: MPL-2.0
 
 #define _POSIX_C_SOURCE 200809L
-#include <stdio.h>  // puts, perror
-#include <unistd.h> // getlogin
+#include <stdio.h>  // printf, perror
+#include <stdlib.h> // getenv
 
 int
 main(void)
 {
-	/* flawfinder: ignore. POSIX requires getlogin(3), which ought to be correct */
-	char *name = getlogin();
-	if(name == NULL)
+	// Allows to avoid utmp (glibc…), $LOGNAME also being required to match user's login name
+	char *logname = getenv("LOGNAME");
+	if(logname == NULL)
 	{
-		perror("logname: getlogin");
+		fprintf(stderr, "logname: Error $LOGNAME isn't set (didn't login?)\n");
 		return 1;
 	}
 
-	if(puts(name) < 0)
+	if(printf("%s\n", logname) < 0)
 	{
-		perror("puts");
+		perror("logname: printf");
 		return 1;
 	}
 
diff --git a/test-cmd/logname b/test-cmd/logname
@@ -2,16 +2,10 @@
 # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
 # SPDX-License-Identifier: MPL-2.0
 
-atf_test_case id
-id_body() {
-	# glibc uses utmp, of all the things…
-	has_glibc && atf_skip
-
-	atf_check -o "inline:$(id -un)\n" ../cmd/logname
-}
-
 atf_test_case environ
 environ_body() {
+	test -n "${LOGNAME}" || atf_skip "$LOGNAME not set (wtf)"
+
 	atf_check -o "inline:${LOGNAME}\n" ../cmd/logname
 }
 
@@ -20,7 +14,5 @@ atf_init_test_cases() {
 
 	. ../test_functions.sh
 
-	atf_add_test_case id
-
-	test -n "${LOGNAME}" && atf_add_test_case environ
+	atf_add_test_case environ
 }