aboutsummaryrefslogtreecommitdiffstats
path: root/src/librandom/random_string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/librandom/random_string.c')
-rw-r--r--src/librandom/random_string.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/librandom/random_string.c b/src/librandom/random_string.c
new file mode 100644
index 0000000..c87c8c2
--- /dev/null
+++ b/src/librandom/random_string.c
@@ -0,0 +1,66 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+
+#ifdef SKALIBS_HASARC4RANDOM
+
+#include <stdlib.h>
+#include <skalibs/random.h>
+
+void random_string (char *s, unsigned int n)
+{
+ arc4random_buf(s, n) ;
+}
+
+#else
+#ifdef SKALIBS_HASGETRANDOM
+
+#include <skalibs/nonposix.h>
+#include <sys/types.h>
+#include <sys/syscall.h>
+#include <skalibs/random.h>
+
+static int getrandom (void *buf, size_t buflen, unsigned int flags)
+{
+ return syscall(SYS_getrandom, buf, buflen, flags) ;
+}
+
+void random_string (char *s, unsigned int n)
+{
+ while (n)
+ {
+ register int r = getrandom(s, n, 0) ;
+ if (r >= 0)
+ {
+ s += r ;
+ n -= r ;
+ }
+ }
+}
+
+#else
+#ifdef SKALIBS_HASDEVURANDOM
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/random.h>
+#include "random-internal.h"
+
+void random_string (char *s, unsigned int n)
+{
+ unsigned int r = allread(random_fd, s, n) ;
+ if (r < n) surf(&surf_here, s+r, n-r) ;
+}
+
+#else /* default */
+
+#include <skalibs/random.h>
+#include "random-internal.h"
+
+void random_string (char *s, unsigned int n)
+{
+ surf(&surf_here, s, n) ;
+}
+
+#endif
+#endif
+#endif