+ fscanf(fp, "MemTotal: %ld kB\n", &total);
+ fclose(fp);
+ return smprintf("%f", (float)total / 1024 / 1024);
+}
+
+static char *
+ram_used(void)
+{
+ long free, total, buffers, cached, used;
+ FILE *fp;
+
+ if (!(fp = fopen("/proc/meminfo", "r"))) {
+ fprintf(stderr, "Error opening meminfo file.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "MemTotal: %ld kB\n", &total);
+ fscanf(fp, "MemFree: %ld kB\n", &free);
+ fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
+ fscanf(fp, "Cached: %ld kB\n", &cached);
+
+ fclose(fp);
+ used = total - free - buffers - cached;
+ return smprintf("%f", (float)used / 1024 / 1024);
+}
+
+static char *
+run_command(const char* command)
+{
+ int good;
+ FILE *fp;
+ char buffer[64];
+
+ if (!(fp = popen(command, "r"))) {
+ fprintf(stderr, "Could not get command output for: %s.\n", command);
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fgets(buffer, sizeof(buffer) - 1, fp);
+ pclose(fp);
+ for (int i = 0 ; i != sizeof(buffer); i++) {
+ if (buffer[i] == '\0') {
+ good = 1;
+ break;
+ }
+ }
+ if (good)
+ buffer[strlen(buffer) - 1] = '\0';
+ return smprintf("%s", buffer);
+}
+
+static char *
+temp(const char *file)
+{
+ int temperature;
+ FILE *fp;
+
+ if (!(fp = fopen(file, "r"))) {
+ fprintf(stderr, "Could not open temperature file.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%d", &temperature);
+ fclose(fp);
+ return smprintf("%d°C", temperature / 1000);
+}
+
+static char *
+uptime(void)
+{
+ struct sysinfo info;
+ int hours = 0;
+ int minutes = 0;
+
+ sysinfo(&info);
+ hours = info.uptime / 3600;
+ minutes = (info.uptime - hours * 3600 ) / 60;
+
+ return smprintf("%dh %dm", hours, minutes);
+}
+
+static char *
+username(void)
+{
+ register struct passwd *pw;
+ register uid_t uid;
+
+ uid = geteuid();
+ pw = getpwuid(uid);
+
+ if (pw)
+ return smprintf("%s", pw->pw_name);
+ else {
+ fprintf(stderr, "Could not get username.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ return smprintf(UNKNOWN_STR);
+}
+
+static char *
+uid(void)
+{
+ register uid_t uid;
+
+ uid = geteuid();
+
+ if (uid)
+ return smprintf("%d", uid);
+ else {
+ fprintf(stderr, "Could not get uid.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ return smprintf(UNKNOWN_STR);
+}
+
+
+static char *
+vol_perc(const char *soundcard)
+{
+ int mute = 0;
+ long vol = 0, max = 0, min = 0;
+ snd_mixer_t *handle;
+ snd_mixer_elem_t *pcm_mixer, *mas_mixer;
+ snd_mixer_selem_id_t *vol_info, *mute_info;
+
+ snd_mixer_open(&handle, 0);
+ snd_mixer_attach(handle, soundcard);
+ snd_mixer_selem_register(handle, NULL, NULL);
+ snd_mixer_load(handle);
+
+ snd_mixer_selem_id_malloc(&vol_info);
+ snd_mixer_selem_id_malloc(&mute_info);
+ if (vol_info == NULL || mute_info == NULL) {
+ fprintf(stderr, "Could not get alsa volume.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+ snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL);
+ snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL);
+ pcm_mixer = snd_mixer_find_selem(handle, vol_info);
+ mas_mixer = snd_mixer_find_selem(handle, mute_info);
+
+ snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
+ snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
+ snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
+
+ if (vol_info)
+ snd_mixer_selem_id_free(vol_info);
+ if (mute_info)
+ snd_mixer_selem_id_free(mute_info);
+ if (handle)
+ snd_mixer_close(handle);
+
+ if (!mute)
+ return smprintf("mute");
+ else
+ return smprintf("%d%%", (vol * 100) / max);