X-Git-Url: http://git.eversberg.eu/gitweb.cgi?p=lcr.git;a=blobdiff_plain;f=macro.h;h=08c8a9bd603b1be4e0904813b3f35868f756b33d;hp=9e4bcd1ccd3eac35b31d80081d6325d5ac93c279;hb=a1c8b8d89f31cbf5ac4151ac65e9e6545e75713a;hpb=03cb33b51da2596a2ef4373debe52ccde8e5cc7b diff --git a/macro.h b/macro.h index 9e4bcd1..08c8a9b 100644 --- a/macro.h +++ b/macro.h @@ -13,7 +13,7 @@ /* safe strcpy/strncpy */ -#define SCPY(dst, src) scpy(dst, src, sizeof(dst)) +#define SCPY(dst, src) scpy((char *)dst, src, sizeof(dst)) static inline void scpy(char *dst, const char *src, unsigned int siz) { strncpy(dst, src, siz); @@ -22,16 +22,16 @@ static inline void scpy(char *dst, const char *src, unsigned int siz) /* safe strcat/strncat */ -#define SCAT(dst, src) scat(dst, src, sizeof(dst)-strlen(dst)-1) +#define SCAT(dst, src) scat((char *)dst, src, sizeof(dst)) static inline void scat(char *dst, const char *src, unsigned int siz) { - strncat(dst, src, siz); + strncat(dst, src, siz-strlen(dst)-1); dst[siz-1] = '\0'; } /* safe concat of a byte */ -#define SCCAT(dst, src) sccat(dst, src, sizeof(dst)) +#define SCCAT(dst, src) sccat((char *)dst, src, sizeof(dst)) static inline void sccat(char *dst, char chr, unsigned int siz) { if (strlen(dst) < siz-1) @@ -43,7 +43,7 @@ static inline void sccat(char *dst, char chr, unsigned int siz) /* safe sprintf/snprintf */ -#define SPRINT(dst, fmt, arg...) sprint(dst, sizeof(dst), fmt, ## arg) +#define SPRINT(dst, fmt, arg...) sprint((char *)dst, sizeof(dst), fmt, ## arg) static inline void sprint(char *dst, unsigned int siz, const char *fmt, ...) { va_list args; @@ -63,9 +63,9 @@ static inline void sprint(char *dst, unsigned int siz, const char *fmt, ...) #define UNPRINT snprintf #define VUNPRINT vsnprintf +#define FATAL(fmt, arg...) _fatal(__FILE__, __FUNCTION__, __LINE__, fmt, ##arg) /* fatal error with error message and exit */ -#define FATAL(fmt, arg...) fatal(__FUNCTION__, __LINE__, fmt, ##arg) -static inline void fatal(const char *function, int line, const char *fmt, ...) +static inline void _fatal(const char *file, const char *function, int line, const char *fmt, ...) { va_list args; char buffer[256]; @@ -74,23 +74,23 @@ static inline void fatal(const char *function, int line, const char *fmt, ...) vsnprintf(buffer, sizeof(buffer), fmt, args); va_end(args); buffer[sizeof(buffer)-1] = '\0'; - fprintf(stderr, "FATAL ERROR in function %s, line %d: %s", function, line, buffer); + fprintf(stderr, "FATAL ERROR in function %s/%s, line %d: %s", file, function, line, buffer); fprintf(stderr, "This error is not recoverable, must exit here.\n"); #ifdef DEBUG_FUNC - debug(function, line, "FATAL ERROR", buffer); - debug(function, line, "FATAL ERROR", (char *)"This error is not recoverable, must exit here.\n"); + debug(file, function, line, "FATAL", buffer); + debug(file, function, line, "FATAL", (char *)"This error is not recoverable, must exit here.\n"); #endif exit(EXIT_FAILURE); } /* memory allocation with setting to zero */ -#define MALLOC(size) _malloc(size, __FUNCTION__, __LINE__) -static inline void *_malloc(unsigned int size, const char *function, int line) +#define MALLOC(size) _malloc(size, __FILE__, __FUNCTION__, __LINE__) +static inline void *_malloc(unsigned int size, const char *file, const char *function, int line) { void *addr; addr = malloc(size); if (!addr) - fatal(function, line, "No memory for %d bytes.\n", size); + _fatal(file, function, line, "No memory for %d bytes.\n", size); memset(addr, 0, size); return addr; }