X-Git-Url: http://git.eversberg.eu/gitweb.cgi?p=lcr.git;a=blobdiff_plain;f=macro.h;h=d97c41f2eb25392120935a637127141d70808aa8;hp=ab0716c72886259f0d93db39f0fd0c9f75e9aa02;hb=8b70a9a5c2071c587ab4016dcbbb8e4bbf6da181;hpb=d2b113f2c4f11acfaee1b2e0fd2f03744a89f6d4 diff --git a/macro.h b/macro.h index ab0716c..d97c41f 100644 --- a/macro.h +++ b/macro.h @@ -14,7 +14,7 @@ /* safe strcpy/strncpy */ #define SCPY(dst, src) scpy(dst, src, sizeof(dst)) -static inline void scpy(char *dst, char *src, unsigned int siz) +static inline void scpy(char *dst, const char *src, unsigned int siz) { strncpy(dst, src, siz); dst[siz-1] = '\0'; @@ -22,8 +22,8 @@ static inline void scpy(char *dst, char *src, unsigned int siz) /* safe strcat/strncat */ -#define SCAT(dst, src) scat(dst, src, sizeof(dst)) -static inline void scat(char *dst, char *src, unsigned int siz) +#define SCAT(dst, src) scat(dst, src, sizeof(dst)-strlen(dst)-1) +static inline void scat(char *dst, const char *src, unsigned int siz) { strncat(dst, src, siz); dst[siz-1] = '\0'; @@ -44,7 +44,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) -static inline void sprint(char *dst, unsigned int siz, char *fmt, ...) +static inline void sprint(char *dst, unsigned int siz, const char *fmt, ...) { va_list args; @@ -65,7 +65,7 @@ static inline void sprint(char *dst, unsigned int siz, char *fmt, ...) /* 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, char *fmt, ...) +static inline void fatal(const char *function, int line, const char *fmt, ...) { va_list args; char buffer[256]; @@ -77,8 +77,8 @@ static inline void fatal(const char *function, int line, char *fmt, ...) fprintf(stderr, "FATAL ERROR in function %s, line %d: %s", 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", "This error is not recoverable, must exit here.\n"); + debug(function, line, "FATAL", buffer); + debug(function, line, "FATAL", (char *)"This error is not recoverable, must exit here.\n"); #endif exit(EXIT_FAILURE); } @@ -92,7 +92,7 @@ static inline void *_malloc(unsigned int size, const char *function, int line) if (!addr) fatal(function, line, "No memory for %d bytes.\n", size); memset(addr, 0, size); - return(addr); + return addr; } /* memory freeing with clearing memory to prevent using freed memory */ @@ -104,4 +104,20 @@ static inline void _free(void *addr, int size) free(addr); } +/* fill buffer and be sure that it's result is 0-terminated, also remove newline */ +#define GETLINE(buffer, fp) _getline(buffer, sizeof(buffer), fp) +static inline char *_getline(char *buffer, int size, FILE *fp) +{ + if (!fgets(buffer, size-1, fp)) + return NULL; + buffer[size-1] = '\0'; + if (!buffer[0]) + return buffer; + if (buffer[strlen(buffer)-1] == '\n') + buffer[strlen(buffer)-1] = '\0'; + if (buffer[strlen(buffer)-1] == '\r') + buffer[strlen(buffer)-1] = '\0'; + return buffer; +} +