Merge pull request #20 from R41z/master

fix division by zero in vol_perc, and hostname() rewrite
This commit is contained in:
Aaron Marcher 2016-10-09 13:50:02 +02:00 committed by GitHub
commit 5e7aa2a321

View file

@ -271,14 +271,10 @@ hostname(void)
char buf[HOST_NAME_MAX]; char buf[HOST_NAME_MAX];
FILE *fp; FILE *fp;
fp = fopen("/proc/sys/kernel/hostname", "r"); if (gethostname(buf, sizeof(buf)) == -1) {
if (fp == NULL) { warn(1, "hostname");
warn("Failed to open file /proc/sys/kernel/hostname");
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
fgets(buf, sizeof(buf), fp);
buf[strlen(buf)-1] = '\0';
fclose(fp);
return smprintf("%s", buf); return smprintf("%s", buf);
} }
@ -501,7 +497,10 @@ vol_perc(const char *card)
snd_mixer_selem_id_free(s_elem); snd_mixer_selem_id_free(s_elem);
snd_mixer_close(handle); snd_mixer_close(handle);
return smprintf("%d%%", ((uint_fast16_t)(vol * 100) / max)); if (max == 0)
return smprintf("%d%%", 0);
else
return smprintf("%d%%", ((uint_fast16_t)(vol * 100) / max));
} }
static char * static char *