+static char *
+run_command(const char *cmd)
+{
+ char *nlptr;
+ FILE *fp;
+ char buf[1024] = UNKNOWN_STR;
+
+ fp = popen(cmd, "r");
+ if (fp == NULL) {
+ warn("Failed to get command output for %s", cmd);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fgets(buf, sizeof(buf), fp);
+ pclose(fp);
+ buf[sizeof(buf) - 1] = '\0';
+
+ if ((nlptr = strrchr(buf, '\n')) != NULL) {
+ nlptr[0] = '\0';
+ }
+
+ return smprintf("%s", buf);
+}
+
+static char *
+swap_free(void)
+{
+ long total, free;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
+ warn("swap_free: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%f", (float)free / 1024 / 1024);
+}
+
+static char *
+swap_perc(void)
+{
+ long total, free, cached;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
+ warn("swap_perc: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%d", 100 * (total - free - cached) / total);
+}
+
+static char *
+swap_total(void)
+{
+ long total;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
+ warn("swap_total: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ return smprintf("%f", (float)total / 1024 / 1024);
+}
+
+static char *
+swap_used(void)
+{
+ long total, free, cached;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
+ warn("swap_used: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
+}
+
+static char *
+temp(const char *file)
+{
+ int temp;
+ FILE *fp;
+
+ fp = fopen(file, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", file);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%d", &temp);
+ fclose(fp);
+
+ return smprintf("%d", temp / 1000);
+}
+
+static char *
+uptime(void)
+{
+ struct sysinfo info;
+ int h = 0;
+ int m = 0;
+
+ sysinfo(&info);
+ h = info.uptime / 3600;
+ m = (info.uptime - h * 3600 ) / 60;
+
+ return smprintf("%dh %dm", h, m);
+}
+
+static char *
+username(void)
+{
+ struct passwd *pw = getpwuid(geteuid());
+
+ if (pw == NULL) {
+ warn("Failed to get username");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ return smprintf("%s", pw->pw_name);
+}
+
+static char *
+uid(void)
+{
+ return smprintf("%d", geteuid());
+}
+
+
+static char *
+vol_perc(const char *card)
+{
+ unsigned int i;
+ int v, afd, devmask;
+ char *vnames[] = SOUND_DEVICE_NAMES;
+
+ afd = open(card, O_RDONLY | O_NONBLOCK);
+ if (afd == -1) {
+ warn("Cannot open %s", card);
+ return smprintf(UNKNOWN_STR);
+ }
+
+ if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
+ warn("Cannot get volume for %s", card);
+ close(afd);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
+ if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
+ if (ioctl(afd, MIXER_READ(i), &v) == -1) {
+ warn("vol_perc: ioctl");
+ close(afd);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ }
+ }
+
+ close(afd);
+
+ return smprintf("%d", v & 0xff);