simplify smprintf by using vasprintf
authorDaniel Walter <d.walter@0x90.at>
Thu, 10 Mar 2016 10:49:48 +0000 (11:49 +0100)
committerDaniel Walter <d.walter@0x90.at>
Thu, 10 Mar 2016 10:49:48 +0000 (11:49 +0100)
config.mk
slstatus.c

index 75ba48227c8bcbe3d5b2fa9c5d24e59176429885..4888003d672aaba9d3fc79a43c42984ffbd8b9b9 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -15,7 +15,7 @@ INCS = -I. -I/usr/include -I${X11INC}
 LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lasound
 
 # flags
-CPPFLAGS = -DVERSION=\"${VERSION}\"
+CPPFLAGS = -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
 CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
 #CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
 LDFLAGS = -g ${LIBS}
index 42e5751784e5b61f85145570c8bd52f8f9fc09ea..214f66712c760b43dcfe052620be360133b86f49 100644 (file)
@@ -40,21 +40,10 @@ 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) {
-        fprintf(stderr, "Malloc error.");
-        exit(1);
-    }
-
+    char *ret = NULL;
     va_start(fmtargs, fmt);
-    vsnprintf(ret, len, fmt, fmtargs);
+    if (vasprintf(&ret, fmt, fmtargs) < 0)
+        return NULL;
     va_end(fmtargs);
 
     return ret;