summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2018-12-08 15:10:09 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2018-12-08 15:10:09 +0000
commit30d0c173d87b92a9ad2f3c1b643463a894abb1d9 (patch)
tree544c0917404a5ee3bf1c5f4faceeb51cff084b8d /src
parent2f017fbdd689e025437783cc8d675a7d304b37c3 (diff)
downloadskalibs-30d0c173d87b92a9ad2f3c1b643463a894abb1d9.tar.xz
Really add mkfootemp and autosurf, I guess ?
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/functypes.h3
-rw-r--r--src/include/skalibs/posixplz.h1
-rw-r--r--src/libposixplz/mkLtemp.c10
-rw-r--r--src/libposixplz/mkbtemp.c21
-rw-r--r--src/libposixplz/mkctemp.c21
-rw-r--r--src/libposixplz/mkfiletemp.c22
-rw-r--r--src/libposixplz/mklinktemp.c21
-rw-r--r--src/libposixplz/mkltemp.c10
-rw-r--r--src/libposixplz/mkptemp.c20
-rw-r--r--src/libposixplz/posixplz-internal.h7
-rw-r--r--src/librandom/autosurf.c18
-rw-r--r--src/librandom/autosurf_name.c10
-rw-r--r--src/librandom/random_oklist.c6
13 files changed, 170 insertions, 0 deletions
diff --git a/src/include/skalibs/functypes.h b/src/include/skalibs/functypes.h
index 9c77f22..bd2b1e1 100644
--- a/src/include/skalibs/functypes.h
+++ b/src/include/skalibs/functypes.h
@@ -49,4 +49,7 @@ typedef alliovfunc_t *alliovfunc_t_ref ;
typedef int createfunc_t (char const *, mode_t, void *) ;
typedef createfunc_t *createfunc_t_ref ;
+typedef int linkfunc_t (char const *, char const *) ;
+typedef linkfunc_t *linkfunc_t_ref ;
+
#endif
diff --git a/src/include/skalibs/posixplz.h b/src/include/skalibs/posixplz.h
index b54faa0..f1b54d3 100644
--- a/src/include/skalibs/posixplz.h
+++ b/src/include/skalibs/posixplz.h
@@ -48,6 +48,7 @@ extern pid_t doublefork (void) ;
extern int touch (char const *) ;
extern int mkfiletemp (char *, createfunc_t_ref, mode_t, void *) ;
+extern int mklinktemp (char const *, char *, linkfunc_t_ref) ;
extern int mkptemp (char *) ;
extern int mkltemp (char const *, char *) ;
extern int mkLtemp (char const *, char *) ;
diff --git a/src/libposixplz/mkLtemp.c b/src/libposixplz/mkLtemp.c
new file mode 100644
index 0000000..5c9ccb0
--- /dev/null
+++ b/src/libposixplz/mkLtemp.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/posixplz.h>
+
+int mkLtemp (char const *src, char *dst)
+{
+ return mklinktemp(src, dst, &link) ;
+}
diff --git a/src/libposixplz/mkbtemp.c b/src/libposixplz/mkbtemp.c
new file mode 100644
index 0000000..c4c62df
--- /dev/null
+++ b/src/libposixplz/mkbtemp.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+static int f (char const *fn, mode_t mode, void *data)
+{
+ dev_t *devp = data ;
+ int r = mknod(fn, (mode & 00777) | S_IFBLK, *devp) ;
+ if (r == -1) return -1 ;
+ r = open_readb(fn) ;
+ if (r == -1) unlink_void(fn) ;
+ return r ;
+}
+
+int mkbtemp (char *s, mode_t mode, dev_t dev)
+{
+ return mkfiletemp(s, &f, mode, &dev) ;
+}
diff --git a/src/libposixplz/mkctemp.c b/src/libposixplz/mkctemp.c
new file mode 100644
index 0000000..69035fb
--- /dev/null
+++ b/src/libposixplz/mkctemp.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+static int f (char const *fn, mode_t mode, void *data)
+{
+ dev_t *devp = data ;
+ int r = mknod(fn, (mode & 00777) | S_IFCHR, *devp) ;
+ if (r == -1) return -1 ;
+ r = open_readb(fn) ;
+ if (r == -1) unlink_void(fn) ;
+ return r ;
+}
+
+int mkctemp (char *s, mode_t mode, dev_t dev)
+{
+ return mkfiletemp(s, &f, mode, &dev) ;
+}
diff --git a/src/libposixplz/mkfiletemp.c b/src/libposixplz/mkfiletemp.c
new file mode 100644
index 0000000..4eaf151
--- /dev/null
+++ b/src/libposixplz/mkfiletemp.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <string.h>
+#include <errno.h>
+
+#include <skalibs/surf.h>
+#include <skalibs/posixplz.h>
+
+int mkfiletemp (char *s, createfunc_t_ref f, mode_t mode, void *data)
+{
+ size_t len = strlen(s) ;
+ size_t xlen = 0 ;
+ int r ;
+ for (; xlen < len ; xlen++) if (s[len - 1 - xlen] != 'X') break ;
+ if (xlen < 6) return (errno = EINVAL, -1) ;
+ do
+ {
+ autosurf_name(s + len - xlen, xlen) ;
+ r = (*f)(s, mode, data) ;
+ } while (r == -1 && errno == EEXIST) ;
+ return r ;
+}
diff --git a/src/libposixplz/mklinktemp.c b/src/libposixplz/mklinktemp.c
new file mode 100644
index 0000000..d1bee4b
--- /dev/null
+++ b/src/libposixplz/mklinktemp.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+#include "posixplz-internal.h"
+
+static int f (char const *dst, mode_t mode, void *data)
+{
+ linkarg_t *la = data ;
+ (void)mode ;
+ return (*la->lf)(la->src, dst) ;
+}
+
+int mklinktemp (char const *src, char *dst, linkfunc_t_ref lf)
+{
+ linkarg_t la = { .lf = lf, .src = src } ;
+ return mkfiletemp(dst, &f, 0600, &la) ;
+}
diff --git a/src/libposixplz/mkltemp.c b/src/libposixplz/mkltemp.c
new file mode 100644
index 0000000..2edec8e
--- /dev/null
+++ b/src/libposixplz/mkltemp.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/posixplz.h>
+
+int mkltemp (char const *src, char *dst)
+{
+ return mklinktemp(src, dst, &symlink) ;
+}
diff --git a/src/libposixplz/mkptemp.c b/src/libposixplz/mkptemp.c
new file mode 100644
index 0000000..42bc715
--- /dev/null
+++ b/src/libposixplz/mkptemp.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+static int f (char const *fn, mode_t mode, void *data)
+{
+ int r = mkfifo(fn, mode) ;
+ if (r == -1) return -1 ;
+ r = open_readb(fn) ;
+ if (r == -1) unlink_void(fn) ;
+ return r ;
+}
+
+int mkptemp (char *s)
+{
+ return mkfiletemp(s, &f, 0600, 0) ;
+}
diff --git a/src/libposixplz/posixplz-internal.h b/src/libposixplz/posixplz-internal.h
index c3da73a..544b020 100644
--- a/src/libposixplz/posixplz-internal.h
+++ b/src/libposixplz/posixplz-internal.h
@@ -3,6 +3,13 @@
#ifndef POSIXPLZ_INTERNAL_H
#define POSIXPLZ_INTERNAL_H
+typedef struct linkarg_s linkarg_t, *linkarg_t_ref ;
+struct linkarg_s
+{
+ linkfunc_t_ref lf ;
+ char const *src ;
+} ;
+
extern void execvep_internal (char const *, char const *const *, char const *const *, char const *) ;
#endif
diff --git a/src/librandom/autosurf.c b/src/librandom/autosurf.c
new file mode 100644
index 0000000..8775bef
--- /dev/null
+++ b/src/librandom/autosurf.c
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#include <skalibs/random.h>
+#include <skalibs/surf.h>
+
+void autosurf (char *s, size_t n)
+{
+ static SURFSchedule ctx = SURFSCHEDULE_ZERO ;
+ static int need4seed = 1 ;
+ if (need4seed)
+ {
+ char tmp[160] ;
+ random_makeseed(tmp) ;
+ surf_init(&ctx, tmp) ;
+ need4seed = 0 ;
+ }
+ return surf(&ctx, s, n) ;
+}
diff --git a/src/librandom/autosurf_name.c b/src/librandom/autosurf_name.c
new file mode 100644
index 0000000..d80e1f8
--- /dev/null
+++ b/src/librandom/autosurf_name.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <skalibs/surf.h>
+#include "random-internal.h"
+
+void autosurf_name (char *s, size_t n)
+{
+ autosurf(s, n) ;
+ while (n--) s[n] = random_oklist[s[n] & 63] ;
+}
diff --git a/src/librandom/random_oklist.c b/src/librandom/random_oklist.c
new file mode 100644
index 0000000..d79f745
--- /dev/null
+++ b/src/librandom/random_oklist.c
@@ -0,0 +1,6 @@
+/* ISC license. */
+
+#include "random-internal.h"
+
+static char const random_oklist_[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz-_0123456789abcdef" ;
+char const *random_oklist = random_oklist_ ;