- time_t tm;
- size_t bufsize = 64;
- char *buf = malloc(bufsize);
-
- /* get time in format */
- time(&tm);
- setlocale(LC_TIME, "");
- if(!strftime(buf, bufsize, timeformat, localtime(&tm))) {
- setlocale(LC_TIME, "C");
- fprintf(stderr, "Strftime failed.\n");
- return smprintf("n/a");
- }
-
- setlocale(LC_TIME, "C");
- /* return time */
- return smprintf("%s", buf);
-}
-
-/* disk usage percentage */
-char *
-get_diskusage()
-{
- int perc = 0;
- struct statvfs fs;
-
- /* try to open mountpoint */
- if (statvfs(mountpath, &fs) < 0) {
- fprintf(stderr, "Could not get filesystem info.\n");
- return smprintf("n/a");
- }
-
- /* calculate percent */
- perc = 100 * (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks));
-
- /* return perc */
- return smprintf("%d%%", perc);
-}
-
-/* ram percentage */
-char *
-get_ram_usage()
-{
- int perc;
- long total, free, buffers, cached;
- FILE *fp;
-
- /* open meminfo file */
- if (!(fp = fopen("/proc/meminfo", "r"))) {
- fprintf(stderr, "Error opening meminfo file.");
- return smprintf("n/a");
- }
-
- /* read the values */
- 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);
-
- /* close meminfo file */
- fclose(fp);
-
- /* calculate percentage */
- perc = 100 * ((total - free) - (buffers + cached)) / total;
-
- /* return perc as string */
- return smprintf("%d%%", perc);
-}
-
-/* alsa volume percentage */
-char *
-get_volume()
-{
- int mute = 0;
- long vol = 0, max = 0, min = 0;
-
- /* get volume from alsa */
- 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);
- snd_mixer_selem_id_set_name(vol_info, channel);
- snd_mixer_selem_id_set_name(mute_info, 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);
-
- /* return the string (mute) */
- if (!mute)
- return smprintf("mute");
- else
- return smprintf("%d%%", (vol * 100) / max);
-}
-
-/* wifi percentage */
-char *
-get_wifi_signal()
-{
- int bufsize = 255;
- int strength;
- char buf[bufsize];
- char *datastart;
- char path_start[16] = "/sys/class/net/";
- char path_end[11] = "/operstate";
- char path[32];
- char status[5];
- char needle[sizeof wificard + 1];
- FILE *fp;
-
- /* generate the path name */
- memset(path, 0, sizeof path);
- strcat(path, path_start);
- strcat(path, wificard);
- strcat(path, path_end);
-
- /* open wifi file */
- if(!(fp = fopen(path, "r"))) {
- fprintf(stderr, "Error opening wifi operstate file.");
- return smprintf("n/a");
- }
-
- /* read the status */
- fgets(status, 5, fp);
-
- /* close wifi file */
- fclose(fp);
-
- /* check if interface down */
- if(strcmp(status, "up\n") != 0){
- return smprintf("n/a");
- }
-
- /* open wifi file */
- if (!(fp = fopen("/proc/net/wireless", "r"))) {
- fprintf(stderr, "Error opening wireless file.");
- return smprintf("n/a");
- }
-
- /* extract the signal strength */
- strcpy(needle, wificard);
- strcat(needle, ":");
- fgets(buf, bufsize, fp);
- fgets(buf, bufsize, fp);
- fgets(buf, bufsize, fp);
- if ((datastart = strstr(buf, needle)) != NULL) {
- datastart = strstr(buf, ":");
- sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
- }
-
- /* close wifi file */
- fclose(fp);
-
- /* return strength in percent */
- return smprintf("%d%%", strength);
-}
-
-/* main function */
+ uid_t uid = geteuid();
+ struct passwd *pw = getpwuid(uid);
+
+ 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);
+ if (afd < 0) {
+ warn("Cannot open %s", card);
+ return smprintf(UNKNOWN_STR);
+ }
+
+ ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask);
+ for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
+ if (devmask & (1 << i)) {
+ if (!strcmp("vol", vnames[i])) {
+ ioctl(afd, MIXER_READ(i), &v);
+ }
+ }
+ }
+
+ close(afd);
+ if (v == 0) {
+ return smprintf("mute");
+ }
+ return smprintf("%d%%", v & 0xff);
+}
+
+static char *
+wifi_perc(const char *iface)
+{
+ int perc;
+ char buf[255];
+ char *datastart;
+ char status[5];
+ FILE *fp;
+
+ ccat(3, "/sys/class/net/", iface, "/operstate");
+ fp = fopen(concat, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", concat);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fgets(status, 5, fp);
+ fclose(fp);
+ if(strcmp(status, "up\n") != 0) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ fp = fopen("/proc/net/wireless", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/net/wireless");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ ccat(2, iface, ":");
+ fgets(buf, sizeof(buf), fp);
+ fgets(buf, sizeof(buf), fp);
+ fgets(buf, sizeof(buf), fp);
+ fclose(fp);
+
+ if ((datastart = strstr(buf, concat)) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ datastart = (datastart+(strlen(iface)+1));
+ sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
+
+ return smprintf("%d%%", perc);
+}
+
+static char *
+wifi_essid(const char *iface)
+{
+ char id[IW_ESSID_MAX_SIZE+1];
+ int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ struct iwreq wreq;
+
+ memset(&wreq, 0, sizeof(struct iwreq));
+ wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
+ snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
+
+ if (sockfd == -1) {
+ warn("Failed to get ESSID for interface %s", iface);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ wreq.u.essid.pointer = id;
+ if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
+ warn("Failed to get ESSID for interface %s", iface);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ close(sockfd);
+
+ if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
+ return smprintf("%s", UNKNOWN_STR);
+ else
+ return smprintf("%s", (char *)wreq.u.essid.pointer);
+}
+
+static void
+sighandler(const int signo)
+{
+ if (signo == SIGTERM || signo == SIGINT) {
+ done = 1;
+ }
+}
+
+static void
+usage(const int eval)
+{
+ fprintf(stderr, "usage: %s [-d] [-o] [-v] [-h]\n", argv0);
+ exit(eval);
+}
+