Add uptime(1) -pr options.

This commit is contained in:
Jonas 'Sortie' Termansen 2022-10-31 23:45:58 +01:00
parent 803d46e913
commit 77a373281e
1 changed files with 35 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2012, 2013 Jonas 'Sortie' Termansen. * Copyright (c) 2011, 2012, 2013, 2022 Jonas 'Sortie' Termansen.
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -17,8 +17,9 @@
* Tell how long the system has been running. * Tell how long the system has been running.
*/ */
#include <err.h>
#include <errno.h> #include <errno.h>
#include <error.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
@ -53,19 +54,40 @@ void PrintElement(size_t num, const char* single, const char* multiple)
prefix = ", "; prefix = ", ";
} }
int main(void) int main(int argc, char* argv[])
{ {
struct timespec uptime; bool raw = false;
if ( clock_gettime(CLOCK_BOOTTIME, &uptime) < 0 ) bool pretty = false;
error(1, errno, "clock_gettime(CLOCK_BOOTTIME)");
uintmax_t usecssinceboot = uptime.tv_sec * 1000000ULL + int opt;
uptime.tv_nsec / 1000ULL; while ( (opt = getopt(argc, argv, "pr")) != -1 )
PrintElement(Days(usecssinceboot), "day", "days"); {
PrintElement(Hours(usecssinceboot), "hour", "hours"); switch ( opt )
PrintElement(Minutes(usecssinceboot), "min", "mins"); {
PrintElement(Seconds(usecssinceboot), "sec", "secs"); case 'p': pretty = true; raw = false; break;
printf("\n"); case 'r': raw = true; pretty = false; break;
default: return 1;
}
}
struct timespec uptime;
clock_gettime(CLOCK_BOOTTIME, &uptime);
if ( raw )
printf("%ji.%09li\n", (intmax_t) uptime.tv_sec, uptime.tv_nsec);
else if ( pretty )
{
uintmax_t usecssinceboot = uptime.tv_sec * 1000000ULL +
uptime.tv_nsec / 1000ULL;
PrintElement(Days(usecssinceboot), "day", "days");
PrintElement(Hours(usecssinceboot), "hour", "hours");
PrintElement(Minutes(usecssinceboot), "min", "mins");
PrintElement(Seconds(usecssinceboot), "sec", "secs");
printf("\n");
}
else
printf("up %ji.%09li s\n", (intmax_t) uptime.tv_sec, uptime.tv_nsec);
return 0; return 0;
} }