X-Git-Url: http://git.eversberg.eu/gitweb.cgi?p=lcr.git;a=blobdiff_plain;f=macro.h;h=d97c41f2eb25392120935a637127141d70808aa8;hp=fb070ba9b65ee87b6afee0bab3b75d556e0336c2;hb=0af1aa359f134b8a15b2a04670788eda4885f88e;hpb=08aad9a8c5ad279759e0a870b1dd0d8159ce3444 diff --git a/macro.h b/macro.h index fb070ba..d97c41f 100644 --- a/macro.h +++ b/macro.h @@ -22,7 +22,7 @@ static inline void scpy(char *dst, const char *src, unsigned int siz) /* safe strcat/strncat */ -#define SCAT(dst, src) scat(dst, src, sizeof(dst)) +#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); @@ -77,8 +77,8 @@ static inline void fatal(const char *function, int line, const 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", (char *)"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; +} +