]> git.armaanb.net Git - bin.git/blob - tides.c
tides: new program, simple fetch program
[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 kernel info
24         struct utsname kernel;
25         uname(&kernel);
26
27         // Get assorted system info
28         struct sysinfo info;
29         sysinfo(&info);
30
31         /* Get memory info from /proc. Inspired by busybox free.c, which is
32                  GPLv2 licensed */
33         char buf[60]; // actual lines we expect are ~30 chars or less
34         int counter = 2; // Number of things being scanned for in the file
35         unsigned long total_kb, avail_kb;
36
37         FILE *fp = fopen("/proc/meminfo", "r");
38         total_kb = avail_kb = 0;
39         while (fgets(buf, sizeof(buf), fp)) {
40                 if (sscanf(buf, "MemTotal: %lu %*s\n", &total_kb) == 1)
41                         if (--counter == 0)
42                                 break;
43                 if (sscanf(buf, "MemAvailable: %lu %*s\n", &avail_kb) == 1)
44                         if (--counter == 0)
45                                 break;
46         }
47         fclose(fp);
48
49         // Display
50         printf("%s@%s\n", username, hostname);
51         printf("%s %s\n", kernel.sysname, kernel.release);
52         struct tm *uptime = gmtime(&info.uptime);
53         printf("Up %02dh %02dm %02ds\n", uptime->tm_hour, uptime->tm_min,
54                                  uptime->tm_sec);
55         printf("%0.0f/%0.0f MB RAM\n", (total_kb - avail_kb)/1024.0, total_kb/1024.0);
56 }