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