aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-08-26 19:09:45 +0200
committerMattias Andrée <maandree@kth.se>2018-08-26 19:09:45 +0200
commit2994cd6c2cb1103613f3116ef7c371a64ae5221f (patch)
treea816a9f72c971827bfae8ad70c52cc6b2db4f01d
parentRemove checks impossible situations (diff)
downloadlibsimple-2994cd6c2cb1103613f3116ef7c371a64ae5221f.tar.gz
libsimple-2994cd6c2cb1103613f3116ef7c371a64ae5221f.tar.bz2
libsimple-2994cd6c2cb1103613f3116ef7c371a64ae5221f.tar.xz
Add test for putenvf
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--Makefile1
-rw-r--r--libsimple.h2
-rw-r--r--vputenvf.c39
3 files changed, 36 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 048d690..d235867 100644
--- a/Makefile
+++ b/Makefile
@@ -88,6 +88,7 @@ TESTS =\
timespectostr.test\
timevaltostr.test\
vasprintf.test\
+ vputenvf.test\
libsimple.test
all: libsimple.a $(TESTS)
diff --git a/libsimple.h b/libsimple.h
index 371cd83..3a18365 100644
--- a/libsimple.h
+++ b/libsimple.h
@@ -1224,7 +1224,7 @@ void libsimple_envputenvf(int, const char *, va_list);
_LIBSIMPLE_GCC_ONLY(__attribute__((__nonnull__, __format__(__printf__, 1, 2))))
static inline int
-libsimple_putenvf(const char *__fmt, ...) /* TODO test */
+libsimple_putenvf(const char *__fmt, ...)
{
va_list __ap;
va_start(__ap, __fmt);
diff --git a/vputenvf.c b/vputenvf.c
index 5a70c6b..0d66b0c 100644
--- a/vputenvf.c
+++ b/vputenvf.c
@@ -1,9 +1,10 @@
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
+#ifndef TEST
int
-libsimple_vputenvf(const char *fmt, va_list ap) /* TODO test */
+libsimple_vputenvf(const char *fmt, va_list ap)
{
va_list ap2;
int n;
@@ -13,11 +14,39 @@ libsimple_vputenvf(const char *fmt, va_list ap) /* TODO test */
va_end(ap2);
if (n < 0)
return -1;
- if ((size_t)n == SIZE_MAX) {
- errno = ENOMEM;
- return -1;
- }
s = alloca((size_t)n + 1);
vsprintf(s, fmt, ap);
return putenv(s);
}
+
+
+#else
+#include "test.h"
+
+int
+main(void)
+{
+ unsetenv("X");
+ assert(!getenv("X"));
+ unsetenv("Y");
+ assert(!getenv("Y"));
+
+ putenvf("X=xyz");
+ assert(!strcmpnul(getenv("X"), "xyz"));
+ putenvf("Y=xyz");
+ assert(!strcmpnul(getenv("Y"), "xyz"));
+
+ putenvf("X=x%sz", "abc");
+ assert(!strcmpnul(getenv("X"), "xabcz"));
+ putenvf("Y=x%sz", "abc");
+ assert(!strcmpnul(getenv("Y"), "xabcz"));
+
+ putenvf("X=%ix%sz%i", 10, "abc", -11);
+ assert(!strcmpnul(getenv("X"), "10xabcz-11"));
+ putenvf("Y=%ix%sz%i", 10, "abc", -11);
+ assert(!strcmpnul(getenv("Y"), "10xabcz-11"));
+
+ return 0;
+}
+
+#endif