]> diplodocus.org Git - nmh/blobdiff - sbr/m_rand.c
client.c: Move interface to own file.
[nmh] / sbr / m_rand.c
index 8544cfb24a59853be1988e083da8bb18b79203e1..1accdc89324dd295b4638b3f0c5b254546eb3d0d 100644 (file)
@@ -1,32 +1,35 @@
-/*
- * m_rand -- provides pseudorandom bytes
+/* m_rand.c -- provides pseudorandom bytes
  *
  * This code is Copyright (c) 2012, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
-#include <stdlib.h>  /* for abs(), srand(), rand(), arc4random() */
-#include <stdio.h>   /* for fopen(), fread(), fclose() */
-#include <unistd.h>  /* for getpid() */
-#include <time.h>    /* for time() */
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
 
-#include <config.h>
+#include "config.h"
+#include "m_rand.h"
 
 #if !HAVE_ARC4RANDOM
-static int seeded = 0;
+static bool seeded = false;
 #endif
 
 
 int
-m_rand (unsigned char *buf, size_t n) {
+m_rand (unsigned char *buf, size_t n)
+{
 #if !HAVE_ARC4RANDOM
   if (! seeded) {
     FILE *devurandom;
     unsigned int seed;
 
     if ((devurandom = fopen ("/dev/urandom", "r"))) {
-      if (fread (&seed, sizeof (seed), 1, devurandom) == 1) seeded = 1;
+      if (fread (&seed, sizeof (seed), 1, devurandom) == 1)
+          seeded = true;
       fclose (devurandom);
     }
 
@@ -36,7 +39,7 @@ m_rand (unsigned char *buf, size_t n) {
          arXiv:1005.4117v1 [physics.comp-ph], 22 May 2010, p. 19.
          time() and getpid() shouldn't fail on POSIX platforms. */
       seed = abs ((int) ((time (0) * 181 * ((getpid ()-83) * 359)) % 104729));
-      seeded = 1;
+      seeded = true;
     }
 
     srand (seed);