From 9725daa03a7806b1c9c5a3c511dfe2ff9f97dd26 Mon Sep 17 00:00:00 2001
From: Denis Vlasenko <vda.linux@googlemail.com>
Date: Thu, 11 Sep 2008 09:54:23 +0000
Subject: halt: reinstate -w even if !FEATURE_WTMP; beautify code in halt.c

---
 init/halt.c        | 64 +++++++++++++++++++++++++++++-------------------------
 loginutils/getty.c |  5 +----
 2 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/init/halt.c b/init/halt.c
index 42b9edc08..cbb325eb9 100644
--- a/init/halt.c
+++ b/init/halt.c
@@ -13,60 +13,66 @@
 #if ENABLE_FEATURE_WTMP
 #include <sys/utsname.h>
 #include <utmp.h>
+
+static void write_wtmp(void)
+{
+	struct utmp utmp;
+	struct utsname uts;
+	if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
+		close(creat(bb_path_wtmp_file, 0664));
+	}
+	memset(&utmp, 0, sizeof(utmp));
+	utmp.ut_tv.tv_sec = time(NULL);
+	safe_strncpy(utmp.ut_user, "shutdown", UT_NAMESIZE);
+	utmp.ut_type = RUN_LVL;
+	safe_strncpy(utmp.ut_id, "~~", sizeof(utmp.ut_id));
+	safe_strncpy(utmp.ut_line, "~~", UT_LINESIZE);
+	if (uname(&uts) == 0)
+		safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
+	updwtmp(bb_path_wtmp_file, &utmp);
+
+}
+#else
+#define write_wtmp() ((void)0)
+#endif
+
+#ifndef RB_HALT_SYSTEM
+#define RB_HALT_SYSTEM RB_HALT
+#endif
+
+#ifndef RB_POWER_OFF
+#define RB_POWER_OFF RB_POWERDOWN
 #endif
 
 int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int halt_main(int argc UNUSED_PARAM, char **argv)
 {
 	static const int magic[] = {
-#ifdef RB_HALT_SYSTEM
 		RB_HALT_SYSTEM,
-#elif defined RB_HALT
-		RB_HALT,
-#endif
-#ifdef RB_POWER_OFF
 		RB_POWER_OFF,
-#elif defined RB_POWERDOWN
-		RB_POWERDOWN,
-#endif
-		RB_AUTOBOOT
+		RB_AUTOBOOT	  
 	};
 	static const smallint signals[] = { SIGUSR1, SIGUSR2, SIGTERM };
 
 	int delay = 0;
 	int which, flags, rc;
-#if ENABLE_FEATURE_WTMP
-	struct utmp utmp;
-	struct utsname uts;
-#endif
 
 	/* Figure out which applet we're running */
-	for (which = 0; "hpr"[which] != *applet_name; which++)
+	for (which = 0; "hpr"[which] != applet_name[0]; which++)
 		continue;
 
 	/* Parse and handle arguments */
 	opt_complementary = "d+"; /* -d N */
-	flags = getopt32(argv, "d:nf" USE_FEATURE_WTMP("w"), &delay);
+	/* We support -w even if !ENABLE_FEATURE_WTMP, in order
+	 * to not break scripts */
+	flags = getopt32(argv, "d:nfw", &delay);
 
 	sleep(delay);
 
-#if ENABLE_FEATURE_WTMP
-	if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
-		close(creat(bb_path_wtmp_file, 0664));
-	}
-	memset(&utmp, 0, sizeof(utmp));
-	utmp.ut_tv.tv_sec = time(NULL);
-	safe_strncpy(utmp.ut_user, "shutdown", UT_NAMESIZE);
-	utmp.ut_type = RUN_LVL;
-	safe_strncpy(utmp.ut_id, "~~", sizeof(utmp.ut_id));
-	safe_strncpy(utmp.ut_line, "~~", UT_LINESIZE);
-	if (uname(&uts) == 0)
-		safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
-	updwtmp(bb_path_wtmp_file, &utmp);
+	write_wtmp();
 
 	if (flags & 8) /* -w */
 		return EXIT_SUCCESS;
-#endif /* !ENABLE_FEATURE_WTMP */
 
 	if (!(flags & 2)) /* no -n */
 		sync();
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 358a45c7c..8b0e729fb 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -19,7 +19,7 @@
 #include <syslog.h>
 
 #if ENABLE_FEATURE_UTMP
-#include <utmp.h>
+#include <utmp.h> /* updwtmp() */
 #endif
 
 /*
@@ -29,9 +29,6 @@
 #ifdef LOGIN_PROCESS                    /* defined in System V utmp.h */
 #include <sys/utsname.h>
 #include <time.h>
-#if ENABLE_FEATURE_WTMP
-extern void updwtmp(const char *filename, const struct utmp *ut);
-#endif
 #else /* if !sysV style, wtmp/utmp code is off */
 #undef ENABLE_FEATURE_UTMP
 #undef ENABLE_FEATURE_WTMP
-- 
cgit v1.2.3-55-g6feb