Simplify code
authorMikael Berthe <mikael@lilotux.net>
Sun, 14 Nov 2010 11:02:15 +0100
changeset 9 c1f0277182a3
parent 8 af9ebb57baf6
child 10 36e7f4269583
Simplify code - Simplify code (revert to isbear's code in do_uptime() now that the initialization logic works better) - Calculate starttime precisely when using the /proc filesystem - Display the start date when /uptime is called
uptime.c
--- a/uptime.c	Sat Nov 13 17:51:20 2010 +0100
+++ b/uptime.c	Sun Nov 14 11:02:15 2010 +0100
@@ -99,27 +99,13 @@
 void do_uptime (char *arg)
 {
 	GString *line = g_string_new (NULL);
-	guint seconds;
-	guint minutes;
-	guint hours;
-	guint days;
-	time_t now = time (NULL);
+	gchar strstartdate[256];
 
-	if (proc_used && settings_opt_get_int ("uptime_use_proc")) {
-		int sysup = sys_uptime();
-		if (!sysup) {
-			g_string_free (line, TRUE);
-			scr_log_print (LPRINT_NORMAL, "Uptime not available :-(");
-			return;
-		}
-		seconds = (sysup - mstime) / hz;
-	} else {
-		seconds = now - starttime;
-	}
+	guint seconds = time (NULL) - starttime;
+	guint minutes = seconds / 60;
+	guint hours   = minutes / 60;
+	guint days    = hours   / 24;
 
-	minutes = seconds / 60;
-	hours   = minutes / 60;
-	days    = hours   / 24;
 	seconds %= 60;
 	minutes %= 60;
 	hours   %= 24;
@@ -133,7 +119,13 @@
 	if (seconds)
 		g_string_append_printf (line, " %u seconds", seconds);
 
-	scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds");
+	// Running since:
+	strftime(strstartdate, sizeof(strstartdate),
+			"%Y-%m-%d %H:%M", localtime(&starttime));
+
+	scr_log_print (LPRINT_NORMAL, "Uptime: %s.\n(Running since %s)",
+			line -> len ? line -> str : " 0 second",
+			strstartdate);
 
 	g_string_free (line, TRUE);
 }
@@ -222,8 +214,12 @@
 		g_string_free (line, TRUE);
 
 		hz = sysconf(_SC_CLK_TCK);
-		if (hz && mstime)
+		if (hz && mstime) {
+			int sysup = sys_uptime();
+			if (sysup) // Let's calculate starttime more precisely:
+				starttime = time (NULL) - (sysup - mstime) / hz;
 			proc_used = TRUE;
+		}
 	}
 
 	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);