42 |
|
|
43 |
#include <limits.h> |
#include <limits.h> |
44 |
#include <string.h> |
#include <string.h> |
45 |
|
#include <utmp.h> |
46 |
#include <unistd.h> |
#include <unistd.h> |
47 |
|
#include <err.h> |
48 |
#include <stdlib.h> |
#include <stdlib.h> |
49 |
#include <stdio.h> |
#include <stdio.h> |
50 |
|
|
56 |
#include <bits/posix2_lim.h> |
#include <bits/posix2_lim.h> |
57 |
#endif |
#endif |
58 |
|
|
59 |
|
#ifndef _PATH_UTMP |
60 |
|
#define _PATH_UTMP "/etc/utmp" |
61 |
|
#endif |
62 |
|
|
63 |
struct timeval boottime; |
struct timeval boottime; |
64 |
|
|
65 |
int |
int |
83 |
int |
int |
84 |
d_uptime(void) |
d_uptime(void) |
85 |
{ |
{ |
86 |
int mib[] = { CTL_KERN, KERN_BOOTTIME }; |
struct utmp utmp_str; |
87 |
int f, tmp = 0; |
time_t boottime = 0; |
88 |
size_t size = sizeof(boottime); |
FILE *utmp_file; |
89 |
time_t uptime, now; |
|
90 |
|
if ((utmp_file = (FILE *) fopen(_PATH_UTMP, "r")) == NULL) |
91 |
(void) time(&now); |
err(1, "d_uptime"); |
92 |
|
|
93 |
output("~WUp: ~w"); |
while (fread((char *) &utmp_str, sizeof(utmp_str), 1, utmp_file) == 1) { |
94 |
|
if (!strcmp(utmp_str.ut_line, (char *) BOOT_MSG)) |
95 |
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) { |
boottime = (time_t) utmp_str.ut_time; |
96 |
uptime = now - boottime.tv_sec; |
} |
97 |
|
|
98 |
if (uptime > 59) { |
if (!boottime) |
99 |
uptime += 30; /* round */ |
errx(1, "d_uptime"); |
100 |
tmp = (uptime / SECSPERDAY); /* get days */ |
|
101 |
|
return (1); |
|
if (tmp) { |
|
|
output("~C%d~w day%s", tmp, plural((int)tmp)); |
|
|
f++; |
|
|
} else { |
|
|
f = 0; |
|
|
} |
|
|
|
|
|
uptime %= SECSPERDAY; |
|
|
tmp = (uptime / SECSPERHOUR); /* get hours */ |
|
|
|
|
|
if (tmp) { |
|
|
output("%s~C%d~w hour%s", and(f), tmp, |
|
|
plural((int)tmp)); |
|
|
f++; |
|
|
} else { |
|
|
f = 0; |
|
|
} |
|
|
|
|
|
uptime %= SECSPERHOUR; |
|
|
tmp = (uptime / SECSPERMIN); |
|
|
|
|
|
if (tmp) { |
|
|
output("%s~C%d~w minute%s", and(f), tmp, |
|
|
plural((int)tmp)); |
|
|
f++; |
|
|
} |
|
|
|
|
|
output("\n"); |
|
|
return (1); |
|
|
} else { |
|
|
output("~RCouldn't find out"); |
|
|
|
|
|
return (0); |
|
|
} |
|
|
} |
|
|
|
|
|
return (1); /* default */ |
|
102 |
} |
} |