-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.c
40 lines (33 loc) · 1.02 KB
/
uptime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/kernel.h>
#include <sys/sysinfo.h>
void help(int exitStatus) {
printf("Usage: ./uptime [OPTIONS]\n");
printf("Display how long the machine has been on.\n");
printf("Options:\n");
printf(" -a \tshow information about memory\n");
_exit(exitStatus);
}
int main(int argc, char *args[]) {
if (argc == 2 && (strcmp(args[1], "--help") == 0))
help(0);
if (argc > 2 || (argc == 2 && (strcmp(args[1], "-a") != 0)))
help(1);
const long m = 60;
const long h = m * 60;
const long d = h * 24;
const double mb = 1024 * 1024;
struct sysinfo info;
sysinfo(&info);
printf("up %d days, %d hour(s) and %d minute(s)\n",
info.uptime / d,
(info.uptime % d) / h,
(info.uptime % h) / m);
if (argc == 2 && strcmp(args[1], "-a") == 0)
printf("Total RAM: %5.1f MB\nAvailable RAM: %5.1f MB\n",
info.totalram / mb,
info.freeram / mb);
_exit(0);
}