]> git.armaanb.net Git - bin.git/blob - tides.c
tides: add current time info
[bin.git] / tides.c
1 // Yet another (tm) fetch program
2
3 #include <unistd.h>
4 #include <sys/utsname.h>
5 #include <sys/sysinfo.h>
6 #include <time.h>
7
8 #include <limits.h>
9 #include <string.h>
10 #include <stdio.h>
11
12 int
13 main(void)
14 {
15         // Get hostname
16         char hostname[HOST_NAME_MAX];
17         gethostname(hostname, HOST_NAME_MAX);
18
19         // Get username
20         char username[LOGIN_NAME_MAX];
21         getlogin_r(username, LOGIN_NAME_MAX);
22
23         // Get assorted system info
24         struct sysinfo info;
25         sysinfo(&info);
26
27         // Get uptime
28         struct tm *uptime = gmtime(&info.uptime);
29
30         // Get current time
31         time_t rawtime;
32         time(&rawtime);
33         struct tm *wtime = localtime(&rawtime);
34
35         // Get kernel info
36         struct utsname kernel;
37         uname(&kernel);
38
39         /* Get memory info from /proc. Inspired by busybox free.c, which is
40                  GPLv2 licensed */
41         char buf[60]; // actual lines we expect are ~30 chars or less
42         int counter = 2; // Number of things being scanned for in the file
43         unsigned long total_kb, avail_kb;
44
45         FILE *fp = fopen("/proc/meminfo", "r");
46         total_kb = avail_kb = 0;
47         while (fgets(buf, sizeof(buf), fp)) {
48                 if (sscanf(buf, "MemTotal: %lu %*s\n", &total_kb) == 1)
49                         if (--counter == 0)
50                                 break;
51                 if (sscanf(buf, "MemAvailable: %lu %*s\n", &avail_kb) == 1)
52                         if (--counter == 0)
53                                 break;
54         }
55         fclose(fp);
56
57         // Display
58         printf("%s@%s\n", username, hostname);
59         printf("%d-%02d-%02d %02d:%02d:%02d\n", wtime->tm_year + 1900, wtime->tm_mon,
60                                  wtime->tm_mday, wtime->tm_hour, wtime->tm_min, wtime->tm_sec);
61         printf("%0.0f/%0.0f MB RAM\n", (total_kb - avail_kb)/1024.0, total_kb/1024.0);
62         printf("%s %s\n", kernel.sysname, kernel.release);
63         printf("Up %02dh %02dm %02ds\n", uptime->tm_hour, uptime->tm_min,
64                                  uptime->tm_sec);
65         printf("%0.0f/%0.0f MB RAM\n", (total_kb - avail_kb)/1024.0, total_kb/1024.0);
66 }