Restore format check for sprintf2.
[doldaconnect.git] / include / utils.h
index 4eada80..a1176a6 100644 (file)
@@ -21,8 +21,6 @@
 
 #include <stdarg.h>
 #include <stdlib.h>
-#include <malloc.h>
-#include <assert.h>
 #ifdef DAEMON
 #include "log.h"
 #endif
@@ -36,9 +34,9 @@ struct wcspair {
 
 /* "Safe" functions */
 #ifdef DAEMON
-#define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %i)", __FUNCTION__, __FILE__, __LINE__, (size))
-#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
-#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
+#define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %zi)", __FUNCTION__, __FILE__, __LINE__, (size))
+#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM((ssize_t)(size)); abort(); (void *)0;}):__result__;})
+#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM((ssize_t)(size)); abort(); (void *)0;}):__result__;})
 #define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
 #define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
 #else
@@ -55,7 +53,6 @@ struct cbchain_ ## name { \
     int (*func)(args, void *data); \
     void (*destroy)(void *data); \
     void *data; \
-    int running, free; \
 } * name
 
 #define GCBCHAIN(name, args...) \
@@ -76,7 +73,7 @@ extern int swprintf (wchar_t *__restrict __s, size_t __n,
 
 char *vsprintf2(char *format, va_list al);
 char *sprintf2(char *format, ...)
-#if defined(__GNUC__) && 0
+#if defined(__GNUC__)
     __attribute__ ((format (printf, 1, 2)))
 #endif
 ;
@@ -103,7 +100,7 @@ char *base32encode(char *data, size_t datalen);
 char *base32decode(char *data, size_t *datalen);
 void _freeparr(void **arr);
 int _parrlen(void **arr);
-char *findfile(char *gname, char *uname, char *homedir, int filldef);
+char *findfile(char *name, char *homedir, int filldef);
 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
 void freewcspair(struct wcspair *pair, struct wcspair **list);
 wchar_t *wpfind(struct wcspair *list, wchar_t *key);
@@ -131,7 +128,6 @@ do { \
 do { \
     struct cbchain_ ## name *__new_cb__; \
     __new_cb__ = smalloc(sizeof(*__new_cb__)); \
-    __new_cb__->running = __new_cb__->free = 0; \
     __new_cb__->func = funca; \
     __new_cb__->destroy = destroya; \
     __new_cb__->data = dataa; \
@@ -143,12 +139,11 @@ do { \
     (obj)->name = __new_cb__; \
 } while(0)
 
-#define CBUNREG(obj, name, funca, dataa) \
-({ \
+#define CBUNREG(obj, name, dataa) \
+do { \
     struct cbchain_ ## name *__cur__; \
-    int __found__ = 0; \
     for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
-        if(((void *)(__cur__->func) == (void *)(funca)) && (__cur__->data == (void *)(dataa))) { \
+        if(__cur__->data == (dataa)) { \
             if(__cur__->destroy != NULL) \
                 __cur__->destroy(__cur__->data); \
             if(__cur__->prev != NULL) \
@@ -158,11 +153,10 @@ do { \
             if(__cur__ == (obj)->name) \
                 (obj)->name = __cur__->next; \
             free(__cur__); \
-            __found__ = 1; \
             break; \
         } \
     } \
-__found__;})
+} while(0)
 
 #define GCBREG(name, funca, dataa) \
 do { \
@@ -185,30 +179,18 @@ do { \
     struct cbchain_ ## name *__cur__; \
     while((__cur__ = (obj)->name) != NULL) { \
         (obj)->name = __cur__->next; \
-        if(__cur__->running) { \
-            __cur__->free = 1; \
-        } else { \
-            if(__cur__->destroy != NULL) \
-                __cur__->destroy(__cur__->data); \
-            free(__cur__); \
-       } \
+        if(__cur__->destroy != NULL) \
+            __cur__->destroy(__cur__->data); \
+        free(__cur__); \
     } \
 } while(0)
 
 #define CBCHAINDOCB(obj, name, args...) \
 do { \
     struct cbchain_ ## name *__cur__, *__next__; \
-    int __res__; \
     for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
         __next__ = __cur__->next; \
-        __cur__->running = 1; \
-        __res__ = __cur__->func(args, __cur__->data); \
-        __cur__->running = 0; \
-        if(__cur__->free) { \
-            free(__cur__); \
-            break; \
-        } \
-        if(__res__) { \
+        if(__cur__->func(args, __cur__->data)) { \
             if(__cur__->next != NULL) \
                 __cur__->next->prev = __cur__->prev; \
             if(__cur__->prev != NULL) \