--- Begin Message ---
Package: procps
Version: 1:2.0.7-8
Severity: wishlist
Tags: patch
"-u" flag added to uptime to no longer have the need to complicately
(mis-) parse its output or do the math for /proc/uptime over&over.
cheers,
&rw
Here is the diff:
diff -Nuar procps-2.0.7.orig/proc/whattime.c procps-2.0.7-rw/proc/whattime.c
--- procps-2.0.7.orig/proc/whattime.c Thu Jan 24 16:06:35 2002
+++ procps-2.0.7-rw/proc/whattime.c Thu Jan 24 15:29:16 2002
@@ -11,6 +11,8 @@
* Modified by J. Cowley to add printing the uptime message to a
* string (for top) and to optimize file handling. 19 Mar 1993.
*
+ * Modified by Robert Waldner to add "-u" to uptime. 2001/01/24
+ * <[email protected]>
*/
#include <stdio.h>
@@ -27,10 +29,10 @@
static char buf[128];
static double av[3];
-char *sprint_uptime(void) {
+char *sprint_uptime(int what) {
struct utmp *utmpstruct;
int upminutes, uphours, updays;
- int pos;
+ int pos=0;
struct tm *realtime;
time_t realseconds;
int numuser;
@@ -38,11 +40,13 @@
/* first get the current time */
- time(&realseconds);
- realtime = localtime(&realseconds);
- pos = sprintf(buf, " %02d:%02d:%02d ",
- realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
-
+ if (what == 0) // only if more than raw uptime is required
+ {
+ time(&realseconds);
+ realtime = localtime(&realseconds);
+ pos = sprintf(buf, " %02d:%02d:%02d ",
+ realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
+ }
/* read and calculate the amount of uptime */
@@ -59,32 +63,41 @@
uphours = uphours % 24;
upminutes = upminutes % 60;
if(uphours)
- pos += sprintf(buf + pos, "%2d:%02d, ", uphours, upminutes);
+ pos += sprintf(buf + pos, "%2dh:%02dmin", uphours, upminutes);
else
- pos += sprintf(buf + pos, "%d min, ", upminutes);
+ pos += sprintf(buf + pos, "%d min", upminutes);
-/* count the number of users */
+ if (what == 0) // only if more than raw uptime is required
+ {
+ pos += sprintf(buf + pos, ", ");
+ }
- numuser = 0;
- setutent();
- while ((utmpstruct = getutent())) {
- if ((utmpstruct->ut_type == USER_PROCESS) &&
- (utmpstruct->ut_name[0] != '\0'))
- numuser++;
- }
- endutent();
+/* count the number of users */
- pos += sprintf(buf + pos, "%2d user%s, ", numuser, numuser == 1 ? "" : "s");
+ if (what == 0) // only if more than raw uptime is required
+ {
+ numuser = 0;
+ setutent();
+ while ((utmpstruct = getutent())) {
+ if ((utmpstruct->ut_type == USER_PROCESS) &&
+ (utmpstruct->ut_name[0] != '\0'))
+ numuser++;
+ }
+ endutent();
- loadavg(&av[0], &av[1], &av[2]);
+ pos += sprintf(buf + pos, "%2d user%s, ", numuser, numuser == 1 ? "" :
"s");
- pos += sprintf(buf + pos, " load average: %.2f, %.2f, %.2f",
- av[0], av[1], av[2]);
+ loadavg(&av[0], &av[1], &av[2]);
+ pos += sprintf(buf + pos, " load average: %.2f, %.2f, %.2f",
+ av[0], av[1], av[2]);
+ }
return buf;
}
-void print_uptime(void)
+void print_uptime(int what)
{
- printf("%s\n", sprint_uptime());
+ // if (what == 0) { printf("%s\n", sprint_uptime(0)); }
+ // if (what == 1) { printf("%s\n", sprint_uptime(1)); }
+ fprintf(stdout,"%s\n", sprint_uptime(what));
}
diff -Nuar procps-2.0.7.orig/proc/whattime.h procps-2.0.7-rw/proc/whattime.h
--- procps-2.0.7.orig/proc/whattime.h Fri Jul 10 23:04:00 1998
+++ procps-2.0.7-rw/proc/whattime.h Thu Jan 24 13:22:05 2002
@@ -3,7 +3,7 @@
#ifndef __WHATTIME_H
#define __WHATTIME_H
-extern void print_uptime(void);
-extern char *sprint_uptime(void);
+extern void print_uptime(int what);
+extern char *sprint_uptime(int what);
#endif
diff -Nuar procps-2.0.7.orig/top.c procps-2.0.7-rw/top.c
--- procps-2.0.7.orig/top.c Thu Jan 24 16:06:35 2002
+++ procps-2.0.7-rw/top.c Thu Jan 24 13:00:52 2002
@@ -1200,7 +1200,7 @@
PUTP(ho);
PUTP(md);
if (show_loadav) {
- printf("%s", sprint_uptime());
+ printf("%s", sprint_uptime(0));
PUTP(top_clrtoeol);
putchar('\n');
}
@@ -1310,7 +1310,7 @@
static void do_stats(proc_t** p, float elapsed_time, int pass)
{
proc_t *this;
- int arrindex, total_time, cpumap, i, n = 0;
+ int arrindex, total_time, i, n = 0;
int sleeping = 0, stopped = 0, zombie = 0, running = 0;
double system_ticks, user_ticks, nice_ticks, idle_ticks;
static int prev_count = 0;
diff -Nuar procps-2.0.7.orig/uptime.c procps-2.0.7-rw/uptime.c
--- procps-2.0.7.orig/uptime.c Thu Jan 24 16:06:35 2002
+++ procps-2.0.7-rw/uptime.c Thu Jan 24 15:27:46 2002
@@ -1,17 +1,26 @@
+/* 2002/01/24: Robert Waldner, "-u" added
+*/
+
#include <stdio.h>
#include <string.h>
-#include "proc/whattime.h"
+#include "proc/whattime.c"
#include "proc/version.h"
int main(int argc, char *argv[]) {
if(argc == 1) {
- print_uptime();
+ print_uptime(0);
return 0;
}
if((argc == 2) && (!strcmp(argv[1], "-V"))) {
display_version();
return 0;
}
- fprintf(stderr, "usage: w -V\n -V display version\n");
+ if((argc == 2) && (!strcmp(argv[1], "-u"))) {
+ print_uptime(1);
+ return 0;
+ }
+ fprintf(stderr, "usage: uptime (-V|-u)\n");
+ fprintf(stderr, " -V display version\n");
+ fprintf(stderr, " -u display only uptime\n");
return 1;
}
diff -Nuar procps-2.0.7.orig/w.c procps-2.0.7-rw/w.c
--- procps-2.0.7.orig/w.c Thu Jan 24 16:06:35 2002
+++ procps-2.0.7-rw/w.c Thu Jan 24 12:58:48 2002
@@ -269,7 +269,7 @@
procs = readproctab(PROC_FILLCMD | PROC_FILLUSR);
if (header) { /* print uptime and headers */
- print_uptime();
+ print_uptime(0);
printf("USER TTY ");
if (from)
printf("FROM ");
-- System Information
Debian Release: 3.0
Architecture: i386
Kernel: Linux fsck 2.4.17 #5 Mon Jan 21 02:09:23 CET 2002 i686
Locale: LANG=C, LC_CTYPE=C
Versions of packages procps depends on:
ii libc6 2.2.4-7 GNU C Library: Shared libraries an
ii libncurses5 5.2.20010318-3 Shared libraries for terminal hand
--- End Message ---