From abfffc973699180ceae9e238e213cdec1353a1b0 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 16 Jan 2006 13:32:54 +0000 Subject: [PATCH] Fixed stk_strndup(): it works on non-terminated input strings now and without an off-by-one bug :) --- lib/stkstring.c | 4 ++-- lib/stkstring.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/stkstring.c b/lib/stkstring.c index c7ac9ef5..ccc689da 100644 --- a/lib/stkstring.c +++ b/lib/stkstring.c @@ -72,9 +72,9 @@ stk_hexdump_internal(char *dst, byte *src, uns n) int main(void) { - char *a = stk_strdup("are"); + char *a = stk_strndup("are!",3); a = stk_strcat(a, " the "); - a = stk_strmulticat(a, "Jabberwock, ", "my", NULL); + a = stk_strmulticat(a, stk_strdup("Jabberwock, "), "my", NULL); char *arr[] = { a, " son" }; a = stk_strarraycat(arr, 2); a = stk_printf("Bew%s!", a); diff --git a/lib/stkstring.h b/lib/stkstring.h index 7314d983..8f58aec7 100644 --- a/lib/stkstring.h +++ b/lib/stkstring.h @@ -1,7 +1,7 @@ /* * UCW Library -- Strings Allocated on the Stack * - * (c) 2005 Martin Mares + * (c) 2005--2006 Martin Mares * (c) 2005 Tomas Valla * * This software may be freely distributed and used according to the terms @@ -12,7 +12,7 @@ #include #define stk_strdup(s) ({ char *_s=(s); uns _l=strlen(_s)+1; char *_x=alloca(_l); memcpy(_x, _s, _l); _x; }) -#define stk_strndup(s,n) ({ char *_s=(s); uns _n=(n); uns _k = strlen(_s); uns _l=MIN(_n,_k)+1; char *_x=alloca(_l); memcpy(_x, _s, _l); _x[_l]=0; _x; }) +#define stk_strndup(s,n) ({ char *_s=(s); uns _l=strnlen(_s,(n)); char *_x=alloca(_l+1); memcpy(_x, _s, _l); _x[_l]=0; _x; }) #define stk_strcat(s1,s2) ({ char *_s1=(s1); char *_s2=(s2); uns _l1=strlen(_s1); uns _l2=strlen(_s2); char *_x=alloca(_l1+_l2+1); memcpy(_x,_s1,_l1); memcpy(_x+_l1,_s2,_l2+1); _x; }) #define stk_strmulticat(s...) ({ char *_s[]={s}; char *_x=alloca(stk_array_len(_s, ARRAY_SIZE(_s)-1)); stk_array_copy(_x, _s, ARRAY_SIZE(_s)-1); _x; }) #define stk_strarraycat(s,n) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)); stk_array_copy(_x, _s, _n); _x; }) -- 2.39.2