aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libsimple.c75
-rw-r--r--libsimple.h23
-rw-r--r--strndup.c14
3 files changed, 93 insertions, 19 deletions
diff --git a/libsimple.c b/libsimple.c
index 03d7611..f9657c2 100644
--- a/libsimple.c
+++ b/libsimple.c
@@ -12,7 +12,8 @@ int libsimple_default_failure_exit = 1;
int
main(void)
{
- char buf[1024];
+ const char *cs;
+ char buf[1024], *s;
int intarray[10];
size_t i, n;
@@ -202,7 +203,7 @@ main(void)
stpcpy(buf, "abcxyz");
assert(libsimple_mempcpy(buf, "123", 3) == &buf[3]);
- /* assert(strcmpnul(buf, "123xyz")); FIXME */
+ assert(!strcmpnul(buf, "123xyz"));
assert(libsimple_streq("abc", "abc") == 1);
assert(libsimple_streq("abc", "ab") == 0);
@@ -558,6 +559,76 @@ main(void)
assert(libsimple_strncasecmpnul("Aa", "AA", 0) == 0);
assert(libsimple_strncasecmpnul("AA", "AA", 0) == 0);
+#ifdef libsimple_strdupa
+ cs = "";
+ s = libsimple_strdupa(cs);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, cs));
+
+ cs = "xyz";
+ s = libsimple_strdupa(cs);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, cs));
+#else
+ fprintf(stderr, "warning: libsimple_strdupa missing\n");
+#endif
+
+#ifdef libsimple_strndupa
+ cs = "";
+ s = libsimple_strndupa(cs, 5);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, ""));
+
+ cs = "xyz";
+
+ s = libsimple_strndupa(cs, 5);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, "xyz"));
+
+ s = libsimple_strndupa(cs, 4);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, "xyz"));
+
+ s = libsimple_strndupa(cs, 3);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, "xyz"));
+
+ s = libsimple_strndupa(cs, 2);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, "xy"));
+
+ s = libsimple_strndupa(cs, 1);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, "x"));
+
+ s = libsimple_strndupa(cs, 0);
+ assert(s);
+ assert(s != cs);
+ assert(!strcmp(s, ""));
+#else
+ fprintf(stderr, "warning: libsimple_strndupa missing\n");
+#endif
+
+#ifdef libsimple_memdupa
+ cs = "xyz";
+ for (n = 1; n < 4; n++) {
+ s = libsimple_memdupa(cs, n);
+ assert(s);
+ assert(s != cs);
+ assert(!memcmp(s, cs, n));
+ }
+#else
+ fprintf(stderr, "warning: libsimple_memdupa missing\n");
+#endif
+
return 0;
}
diff --git a/libsimple.h b/libsimple.h
index c163f76..4d3520e 100644
--- a/libsimple.h
+++ b/libsimple.h
@@ -558,22 +558,22 @@ static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n)
#endif
-#ifndef strdupa
-# if defined(__GNUC__) || defined(__clang__)
-# define strdupa(s) /* TODO test */\
+#if defined(__GNUC__) || defined(__clang__)
+# define libsimple_strdupa(s)\
({\
const char *__s = (s);\
size_t __n = strlen(__s) + 1;\
char *__r = alloca(__n);\
memcpy(__r, __s, __n);\
})
+# ifndef strdupa
+# define strdupa(s) libsimple_strdupa(s)
# endif
#endif
-#ifndef strdupa
-# if defined(__GNUC__) || defined(__clang__)
-# define strndupa(s, n) /* TODO test */\
+#if defined(__GNUC__) || defined(__clang__)
+# define libsimple_strndupa(s, n)\
({\
const char *__s = (s);\
size_t __n = (n);\
@@ -582,22 +582,25 @@ static inline void *libsimple_mempcpy(void *__d, const void *__s, size_t __n)
__n = __n < __m ? __n : __m;\
__r = alloca(__n + 1);\
memcpy(__r, __s, __n);\
- __r[n] = '\0';\
+ __r[__n] = '\0';\
__r;\
})
+# ifndef strndupa
+# define strndupa(s, n) libsimple_strndupa(s, n)
# endif
#endif
-#ifndef strdupa
-# if defined(__GNUC__) || defined(__clang__)
-# define memdupa(s, n) /* TODO test */\
+#if defined(__GNUC__) || defined(__clang__)
+# define libsimple_memdupa(s, n)\
({\
const char *__s = (s);\
size_t __n = (n);\
char *__r = alloca(__n);\
memcpy(__r, __s, __n);\
})
+# ifndef memdupa
+# define memdupa(s, n) libsimple_memdupa(s, n)
# endif
#endif
diff --git a/strndup.c b/strndup.c
index df2a69d..faa3bb1 100644
--- a/strndup.c
+++ b/strndup.c
@@ -4,7 +4,7 @@
char *
-libsimple_strndup(const char *s, size_t n) /* FIXME */
+libsimple_strndup(const char *s, size_t n)
{
char *ret;
size_t m = strlen(s);
@@ -25,29 +25,29 @@ libsimple_strndup(const char *s, size_t n) /* FIXME */
#include "test.h"
int
-main(void) /* TODO valgrind */
+main(void)
{
const char *s = "test";
void *p;
- p = libsimple_memdup(s, 5);
+ p = libsimple_strndup(s, 5);
assert(p && p != s);
assert(!strcmpnul(p, "test"));
memset(p, 0, 5);
assert(!strcmpnul(s, "test"));
free(p);
- p = libsimple_memdup(s, 3);
+ p = libsimple_strndup(s, 3);
assert(p && p != s);
assert(!strcmpnul(p, "tes"));
- memset(p, 0, 5);
+ memset(p, 0, 4);
assert(!strcmpnul(s, "test"));
free(p);
- p = libsimple_memdup(s, 0);
+ p = libsimple_strndup(s, 0);
assert(p && p != s);
assert(!strcmpnul(p, ""));
- memset(p, 0, 5);
+ memset(p, 0, 1);
assert(!strcmpnul(s, "test"));
free(p);