1 // Yet another (tm) fetch program
4 #include <sys/utsname.h>
5 #include <sys/sysinfo.h>
16 char hostname[HOST_NAME_MAX];
17 gethostname(hostname, HOST_NAME_MAX);
20 char username[LOGIN_NAME_MAX];
21 getlogin_r(username, LOGIN_NAME_MAX);
23 // Get current working directory
25 getcwd(cwd, PATH_MAX);
27 // Get assorted system info
32 struct tm *uptime = gmtime(&info.uptime);
37 struct tm *wtime = localtime(&rawtime);
40 struct utsname kernel;
43 /* Get memory info from /proc. Inspired by busybox free.c, which is
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;
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)
55 if (sscanf(buf, "MemAvailable: %lu %*s\n", &avail_kb) == 1)
62 printf("%s@%s\n", username, hostname);
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,
70 printf("%0.0f/%0.0f MB RAM\n", (total_kb - avail_kb)/1024.0, total_kb/1024.0);