summaryrefslogtreecommitdiff
path: root/src/who.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2004-06-23 14:48:16 +0000
committerJim Meyering <jim@meyering.net>2004-06-23 14:48:16 +0000
commit22191da8fba049e9391f3f77a028772c8a20478f (patch)
treebbf62659acf87887942ab04bef91299032cf283c /src/who.c
parent3ba733e19d5a25e26f5c9ea7b33f8a3abb47c89f (diff)
downloadcoreutils-22191da8fba049e9391f3f77a028772c8a20478f.tar.xz
(idle_string, print_user): New arg boottime,
specifying the most recent boot time. All uses changed. (idle_string) Consider a line to be "old" if it hasn't been used since the last boot time. Watch out for overflow when computing times, and for times in the future. (idle_string): Record latest boot time.
Diffstat (limited to 'src/who.c')
-rw-r--r--src/who.c46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/who.c b/src/who.c
index 49209a562..3f4911770 100644
--- a/src/who.c
+++ b/src/who.c
@@ -202,29 +202,32 @@ static struct option const longopts[] = {
{NULL, 0, NULL, 0}
};
-/* Return a string representing the time between WHEN and the time
- that this function is first run.
+/* Return a string representing the time between WHEN and now.
+ BOOTTIME is the time of last reboot.
FIXME: locale? */
static const char *
-idle_string (time_t when)
+idle_string (time_t when, time_t boottime)
{
- static time_t now = 0;
- static char idle_hhmm[IDLESTR_LEN];
- time_t seconds_idle;
+ static time_t now = TYPE_MINIMUM (time_t);
- if (now == 0)
+ if (now == TYPE_MINIMUM (time_t))
time (&now);
- seconds_idle = now - when;
- if (seconds_idle < 60) /* One minute. */
- return " . ";
- if (seconds_idle < (24 * 60 * 60)) /* One day. */
+ if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
{
- sprintf (idle_hhmm, "%02d:%02d",
- (int) (seconds_idle / (60 * 60)),
- (int) ((seconds_idle % (60 * 60)) / 60));
- return (const char *) idle_hhmm;
+ int seconds_idle = now - when;
+ if (seconds_idle < 60)
+ return " . ";
+ else
+ {
+ static char idle_hhmm[IDLESTR_LEN];
+ sprintf (idle_hhmm, "%02d:%02d",
+ seconds_idle / (60 * 60),
+ (seconds_idle % (60 * 60)) / 60);
+ return idle_hhmm;
+ }
}
+
return _(" old ");
}
@@ -325,9 +328,10 @@ print_line (const char *user, const char state, const char *line,
free (x_exitstr);
}
-/* Send properly parsed USER_PROCESS info to print_line */
+/* Send properly parsed USER_PROCESS info to print_line. The most
+ recent boot time is BOOTTIME. */
static void
-print_user (const STRUCT_UTMP *utmp_ent)
+print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
{
struct stat stats;
time_t last_change;
@@ -370,7 +374,7 @@ print_user (const STRUCT_UTMP *utmp_ent)
}
if (last_change)
- sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change));
+ sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
else
sprintf (idlestr, " ?");
@@ -563,6 +567,7 @@ static void
scan_entries (int n, const STRUCT_UTMP *utmp_buf)
{
char *ttyname_b IF_LINT ( = NULL);
+ time_t boottime = TYPE_MINIMUM (time_t);
if (include_heading)
print_heading ();
@@ -583,7 +588,7 @@ scan_entries (int n, const STRUCT_UTMP *utmp_buf)
sizeof (utmp_buf->ut_line)) == 0)
{
if (need_users && IS_USER_PROCESS (utmp_buf))
- print_user (utmp_buf);
+ print_user (utmp_buf, boottime);
else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
print_runlevel (utmp_buf);
else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
@@ -601,6 +606,9 @@ scan_entries (int n, const STRUCT_UTMP *utmp_buf)
print_deadprocs (utmp_buf);
}
+ if (UT_TYPE (utmp_buf) == BOOT_TIME)
+ boottime = UT_TIME_MEMBER (utmp_buf);
+
utmp_buf++;
}
}