- if (fp == NULL) {
- fprintf(stderr, "Could not open temperature file: %s\n",
- strerror(errno));
- 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)
-{
- uid_t uid = geteuid();
- struct passwd *pw = getpwuid(uid);
-
- if (pw == NULL)
- return smprintf("%s", pw->pw_name);
-
- fprintf(stderr, "Could not get username: %s\n",
- strerror(errno));
- return smprintf(UNKNOWN_STR);
-}
-
-static char *
-uid(void)
-{
- return smprintf("%d", geteuid());
-}
-
-
-static char *
-vol_perc(const char *snd_card)
-{ /* thanks to botika for this function */
- long int vol, max, min;
- snd_mixer_t *handle;
- snd_mixer_elem_t *elem;
- snd_mixer_selem_id_t *s_elem;
-
- snd_mixer_open(&handle, 0);
- snd_mixer_attach(handle, snd_card);
- snd_mixer_selem_register(handle, NULL, NULL);
- snd_mixer_load(handle);
- snd_mixer_selem_id_malloc(&s_elem);
- snd_mixer_selem_id_set_name(s_elem, "Master");
- elem = snd_mixer_find_selem(handle, s_elem);
-
- if (elem == NULL) {
- snd_mixer_selem_id_free(s_elem);
- snd_mixer_close(handle);
- perror("alsa error: ");
- return smprintf(UNKNOWN_STR);
- }
-
- snd_mixer_handle_events(handle);
- snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
- snd_mixer_selem_get_playback_volume(elem, 0, &vol);
-
- snd_mixer_selem_id_free(s_elem);
- snd_mixer_close(handle);
-
- return smprintf("%d", (vol * 100) / max);
-}
-
-static char *
-wifi_perc(const char *wificard)
-{
- int strength;
- char buf[255];
- char *datastart;
- char path[64];
- char status[5];
- char needle[strlen(wificard)+2];
- FILE *fp;
-
- strlcpy(path, "/sys/class/net/", sizeof(path));
- strlcat(path, wificard, sizeof(path));
- strlcat(path, "/operstate", sizeof(path));
-
- fp = fopen(path, "r");
-
- if(fp == NULL) {
- fprintf(stderr, "Error opening wifi operstate file: %s\n",
- strerror(errno));
- return smprintf(UNKNOWN_STR);
- }
-
- fgets(status, 5, fp);
- fclose(fp);
- if(strcmp(status, "up\n") != 0)
- return smprintf(UNKNOWN_STR);
-
- fp = fopen("/proc/net/wireless", "r");
- if (fp == NULL) {
- fprintf(stderr, "Error opening wireless file: %s\n",
- strerror(errno));
- return smprintf(UNKNOWN_STR);
- }
-
- strlcpy(needle, wificard, sizeof(needle));
- strlcat(needle, ":", sizeof(needle));
- fgets(buf, sizeof(buf), fp);
- fgets(buf, sizeof(buf), fp);
- fgets(buf, sizeof(buf), fp);
-
- datastart = strstr(buf, needle);
- if (datastart != NULL) {
- datastart = strstr(buf, ":");
- sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
- }
-
- fclose(fp);
- return smprintf("%d%%", strength);
-}