hi list,
i tried to understand the halt code and found it hard because #ifdef obvouscated
a lot of the code. I moved the wtmp stuff into its own function what made it 
easy
for #ifdef.

can someone take a look at the #ifdef magic for RB_HALT_SYSTEM ?
1. it seems RB_HALT is only used by netbsd.
2. init.c should have the same problem but simply defines RB_HALT_SYSTEM
    when not found.

IMHO 2. is not clever.

1. both sould use the same code (= move to platform.h) or drop it
2. init should fail. if the value for RB_HALT_SYSTEM is wrong for a system
    user will become crasy why searching the bug (and halt is not used often)


size info:
   text    data     bss     dec     hex filename
    479       0       0     479     1df halt.o          /* org. BB 1.12 */
    470       0       0     470     1d6 halt.o.new      /* rearraged code */

note:
* i have not removed a single line therefore
* all changes are down to the compiler - your millage may vary !

re,
  wh

--- halt.c.org	2008-08-06 00:56:10.000000000 +0200
+++ halt.c	2008-09-09 17:31:03.000000000 +0200
@@ -13,32 +13,65 @@
 #if ENABLE_FEATURE_WTMP
 #include <sys/utsname.h>
 #include <utmp.h>
-#endif
 
-int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int halt_main(int argc UNUSED_PARAM, char **argv)
+static void write_wtmp(void)
 {
-	static const int magic[] = {
+	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
+static void write_wtmp(void) { /* empty */ };
+#endif
+
+/*
+magic code for "halt"
+*/
+
 #ifdef RB_HALT_SYSTEM
-RB_HALT_SYSTEM,
+#define HALT_MAGIC RB_HALT_SYSTEM
 #elif defined RB_HALT
-RB_HALT,
+#define HALT_MAGIC RB_HALT
 #endif
+
+
+/*
+magic code for "power of"
+*/
+
 #ifdef RB_POWER_OFF
-RB_POWER_OFF,
+#define PO_MAGIC     RB_POWER_OFF
 #elif defined RB_POWERDOWN
-RB_POWERDOWN,
+#define PO_MAGIC     RB_POWERDOWN
 #endif
-RB_AUTOBOOT
+
+
+
+
+int halt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int halt_main(int argc UNUSED_PARAM, char **argv)
+{
+	static const int magic[] = {
+	  HALT_MAGIC,
+	  PO_MAGIC,
+	  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++)
@@ -50,20 +83,7 @@
 
 	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);
-#endif /* !ENABLE_FEATURE_WTMP */
+	write_wtmp();
 
 	if (flags & 8) /* -w */
 		return EXIT_SUCCESS;

_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to