summaryrefslogtreecommitdiff
path: root/src/libstddjb
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2018-11-14 17:08:47 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2018-11-14 17:08:47 +0000
commit661329653329949929b043f5fc45821aa314face (patch)
treeb114775c7d8e0cf1d53f8b1fbc657d492f4bd444 /src/libstddjb
parent9575ac2abacc87c4167d419e5a1fa1d650dee11d (diff)
downloadskalibs-661329653329949929b043f5fc45821aa314face.tar.xz
Add env_dump()
Diffstat (limited to 'src/libstddjb')
-rw-r--r--src/libstddjb/env_dump.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/libstddjb/env_dump.c b/src/libstddjb/env_dump.c
new file mode 100644
index 0000000..e15c6be
--- /dev/null
+++ b/src/libstddjb/env_dump.c
@@ -0,0 +1,51 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <skalibs/bytestr.h>
+#include <skalibs/env.h>
+#include <skalibs/djbunix.h>
+
+ /* XXX: breaks layering, but really openat() should be supported everywhere */
+#include <skalibs/unix-transactional.h>
+
+#define SUFFIX ":envdump:XXXXXX"
+
+int env_dump (char const *dir, mode_t mode, char const *const *envp)
+{
+ int fd ;
+ size_t dirlen = strlen(dir) ;
+ char tmpdir[dirlen + sizeof(SUFFIX)] ;
+ memcpy(tmpdir, dir, dirlen) ;
+ memcpy(tmpdir + dirlen, SUFFIX, sizeof(SUFFIX)) ;
+ if (!mkdtemp(tmpdir)) return 0 ;
+ fd = open_read(tmpdir) ;
+ if (fd == -1) goto err ;
+ for (; *envp ; envp++)
+ {
+ size_t len = str_chr(*envp, '=') ;
+ size_t vallen = strlen(*envp + len + 1) ;
+ char fn[len + 1] ;
+ memcpy(fn, *envp, len + 1) ;
+ len = openwritenclose_at(fd, fn, *envp + len + 1, vallen) ;
+ if (len < vallen) goto cerr ;
+ }
+ fd_close(fd) ;
+ if (chmod(tmpdir, mode) == -1) goto err ;
+ if (rename(tmpdir, dir) == -1) goto err ;
+ return 1 ;
+
+ cerr:
+ fd_close(fd) ;
+ err:
+ {
+ int e = errno ;
+ rm_rf(tmpdir) ;
+ errno = e ;
+ }
+ return 0 ;
+}