Good evening,
I created a status bar with official link on
suckless.org. I used gettemperature pattern with readline (from the
page) and it works fine.
Anyway, when I control memory leak with
valgrind, 7 bytes are lost due to gettemperature (it increases every 3
seconds sleep). I tried to add free(), but it doesn't work. I don't
understand.
Maybe you could take a look.
#define _BSD_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <X11/Xlib.h>
#define CPUTEMP "/sys/class/hwmon/hwmon0/", "temp1_input"
#define ZIKCOL "\03%s\x01"
#define KERCOL "\03%s\x01"
#define TEMPCOL "\02%02.0f°C\x01"
#define TIMECOL "\x03%^a %d %^b\x04 / \x03%H:%M\x01 "
char *tzber = "Europe/Berlin";
static Display *dpy;
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if (ret == NULL) {
perror("malloc");
exit(1);
}
va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}
void
settz(char *tzname)
{
setenv("TZ", tzname, 1);
}
char *
mktimes(char *fmt, char *tzname)
{
char buf[129];
time_t tim;
struct tm *timtm;
memset(buf, 0, sizeof(buf));
tim = time(NULL);
timtm = localtime(&tim);
if (timtm == NULL) {
perror("localtime");
exit(1);
}
if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
fprintf(stderr, "strftime == 0\n");
exit(1);
}
return smprintf("%s", buf);
}
void
setstatus(char *str)
{
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
char *
readfile(char *base, char *file)
{
char *path, line[513];
FILE *fd;
memset(line, 0, sizeof(line));
path = smprintf("%s/%s", base, file);
fd = fopen(path, "r");
if (fd == NULL)
return NULL;
free(path);
if (fgets(line, sizeof(line)-1, fd) == NULL)
return NULL;
fclose(fd);
return smprintf("%s", line);
}
char*
kernel(void)
{
struct utsname unake;
uname(&unake);
return smprintf(KERCOL, unake.release);
}
char *
gettemperature(char *base, char *sensor)
{
char *co;
co = readfile(base, sensor);
if (co == NULL)
return smprintf("");
return smprintf(TEMPCOL, atof(co) / 1000);
}
char*
mocinfo(void)
{
FILE *fp;
char title[72];
char *p;
fp = popen("mocp -Q %title", "r");
if (!fp)
return 0;
fgets(title, sizeof(title), fp);
p = strrchr(title, '\n');
if (p)
*p = '\0';
return smprintf(ZIKCOL, title);
pclose(fp);
}
int
main(void)
{
char *status;
char *kerv;
char *temp;
char *tmdat;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
return 1;
}
for (;;sleep(3)) {
kerv = kernel();
temp = gettemperature(CPUTEMP);
char *mocz = mocinfo();
tmdat = mktimes(TIMECOL, tzber);
status = smprintf("M:%s K:%s TCPU:%s %s", mocz, kerv, temp,
tmdat);
setstatus(status);
free(kerv);
free(mocz);
free(temp);
free(tmdat);
free(status);
}
XCloseDisplay(dpy);
return 0;
}
==9634== Memcheck, a memory error detector
==9634== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==9634== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==9634== Command: ./dwmstatus_leg
==9634==
==9634==
==9634== HEAP SUMMARY:
==9634== in use at exit: 62,693 bytes in 27 blocks
==9634== total heap usage: 74 allocs, 47 frees, 68,149 bytes allocated
==9634==
==9634== 7 bytes in 1 blocks are definitely lost in loss record 2 of 26
==9634== at 0x4C2C04B: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9634== by 0x400D71: smprintf (dwmstatus_leg.c:37)
==9634== by 0x40101C: readfile (dwmstatus_leg.c:104)
==9634== by 0x40107C: gettemperature (dwmstatus_leg.c:120)
==9634== by 0x40119F: main (dwmstatus_leg.c:165)
==9634==
==9634== LEAK SUMMARY:
==9634== definitely lost: 7 bytes in 1 blocks
==9634== indirectly lost: 0 bytes in 0 blocks
==9634== possibly lost: 0 bytes in 0 blocks
==9634== still reachable: 62,686 bytes in 26 blocks
==9634== suppressed: 0 bytes in 0 blocks
==9634== Reachable blocks (those to which a pointer was found) are not shown.
==9634== To see them, rerun with: --leak-check=full --show-reachable=yes
==9634==
==9634== For counts of detected and suppressed errors, rerun with: -v
==9634== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)