mirror of git://git.suckless.org/ubase
Add uptime(1)
This commit is contained in:
parent
30e0acef7a
commit
74146c54a7
3
Makefile
3
Makefile
|
@ -36,7 +36,8 @@ SRC = \
|
|||
swapon.c \
|
||||
truncate.c \
|
||||
umount.c \
|
||||
unshare.c
|
||||
unshare.c \
|
||||
uptime.c
|
||||
|
||||
OBJ = $(SRC:.c=.o) $(LIB)
|
||||
BIN = $(SRC:.c=)
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <sys/sysinfo.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <utmp.h>
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: %s\n", argv0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct utmp usr;
|
||||
FILE *ufp;
|
||||
struct sysinfo info;
|
||||
time_t tmptime;
|
||||
struct tm *now;
|
||||
unsigned int days, hours, minutes;
|
||||
int nusers = 0;
|
||||
|
||||
ARGBEGIN {
|
||||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
sysinfo(&info);
|
||||
time(&tmptime);
|
||||
now = localtime(&tmptime);
|
||||
printf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec);
|
||||
info.uptime /= 60;
|
||||
minutes = info.uptime % 60;
|
||||
info.uptime /= 60;
|
||||
hours = info.uptime % 24;
|
||||
days = info.uptime / 24;
|
||||
if (days)
|
||||
printf("%d day%s, ", days, days > 1 ? "s" : "");
|
||||
if (hours)
|
||||
printf("%2d:%02d, ", hours, minutes);
|
||||
else
|
||||
printf("%d min, ", minutes);
|
||||
|
||||
if ((ufp = fopen(_PATH_UTMP, "r"))) {
|
||||
while(fread(&usr, sizeof(usr), 1, ufp) == 1) {
|
||||
if (!*usr.ut_name || !*usr.ut_line ||
|
||||
usr.ut_line[0] == '~')
|
||||
continue;
|
||||
nusers++;
|
||||
}
|
||||
fclose(ufp);
|
||||
printf(" %d user%s, ", nusers, nusers > 1 ? "s" : "");
|
||||
}
|
||||
|
||||
printf(" load average: %.02f, %.02f, %.02f\n",
|
||||
info.loads[0] / 65536.0f,
|
||||
info.loads[1] / 65536.0f,
|
||||
info.loads[2] / 65536.0f);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue