aboutsummaryrefslogtreecommitdiffstats
path: root/nimisewi.c
diff options
context:
space:
mode:
authorla-ninpre <leobrekalini@gmail.com>2021-09-10 14:06:29 +0000
committerla-ninpre <leobrekalini@gmail.com>2021-09-10 14:06:29 +0000
commit60d1b0c720c58dc08b070dd9d39d72f93a07b028 (patch)
tree0bad69bc7864d2aeb2f9d6c6ddc5cd369589b66a /nimisewi.c
parente6e5718516d6258696287e24c1303898510ff091 (diff)
downloadnimisewi_c-60d1b0c720c58dc08b070dd9d39d72f93a07b028.tar.gz
nimisewi_c-60d1b0c720c58dc08b070dd9d39d72f93a07b028.zip
added support for arc4random_uniform functionv0.3.0
i know that it's probably an overkill for such stupid little program, but why not :D also refactored strlcat support a bit, to make sure that strncat function is used if strlcat could not be found. FossilOrigin-Name: 1b72489c7f0f5b72bac65f1469c9bec650d7ffdabfa4f88409e15c630ac5a52d
Diffstat (limited to 'nimisewi.c')
-rw-r--r--nimisewi.c63
1 files changed, 40 insertions, 23 deletions
diff --git a/nimisewi.c b/nimisewi.c
index bb42d2c..989e227 100644
--- a/nimisewi.c
+++ b/nimisewi.c
@@ -29,17 +29,22 @@
#include <config.h>
#include <err.h>
#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/time.h>
-#ifdef HAVE_BSD_STRING_H
-#include <bsd/string.h>
+#ifdef HAVE_BSD_STDLIB_H
+ #include <bsd/stdlib.h>
#else
-#include <string.h>
+ #include <stdlib.h>
#endif
-#include <sys/types.h>
-#include <sys/time.h>
+#ifdef HAVE_BSD_STRING_H
+ #include <bsd/string.h>
+#else
+ #include <string.h>
+#endif
#include "nimisewi.h"
#include "nimitoki.h" /* nimi_toki, suli_pi_nimi_toki */
@@ -53,6 +58,27 @@ struct nanpa_nimi {
size_t suli_pi_nimi_sewi; /* length of generated phrase */
};
+static uint32_t
+nsrand(uint32_t limit)
+{
+#ifdef HAVE_ARC4RANDOM_UNIFORM
+ return arc4random_uniform(limit);
+#else
+ return rand() % limit;
+#endif
+}
+
+static size_t
+catstr(char *dst, const char *src, size_t size)
+{
+#ifdef HAVE_STRLCAT
+ return strlcat(dst, src, size);
+#else
+ strncat(dst, src, size);
+ return strlen(dst);
+#endif
+}
+
static void
open_e_nanpa_sewi(void)
{
@@ -109,7 +135,7 @@ nanpa_nimi_pana_e_pi(struct nanpa_nimi *nn)
* - word1 word2 word3
* - word1 pi word2 word3
*/
- nn->pi_li_lon = rand() % 2;
+ nn->pi_li_lon = nsrand(2);
} else if (nn->nanpa_nimi > 2) {
nn->pi_li_lon = 1;
}
@@ -142,7 +168,7 @@ static struct nanpa_nimi
nn->nanpa_sewi_nimi = NULL;
nn->suli_pi_nimi_sewi = 0;
- nn->nanpa_nimi = (rand() % 6);
+ nn->nanpa_nimi = (nsrand(6));
/* to use with arbitrary wordlist, remove following function call */
nanpa_nimi_pana_e_pi(nn);
@@ -153,7 +179,7 @@ static struct nanpa_nimi
return NULL;
}
for (i = 0; i <= nn->nanpa_nimi; i++) {
- nn->nanpa_sewi_nimi[i] = rand() % suli_pi_nimi_toki;
+ nn->nanpa_sewi_nimi[i] = nsrand(suli_pi_nimi_toki);
}
if (nn->pi_li_lon) {
nn->nanpa_sewi_nimi[nn->nanpa_pi] = -1;
@@ -183,7 +209,9 @@ char
char *nimi_pana;
struct nanpa_nimi *nn;
+#ifndef HAVE_ARC4RANDOM_UNIFORM
open_e_nanpa_sewi();
+#endif
nn = pana_e_nanpa_nimi();
@@ -194,25 +222,14 @@ char
}
for (i = 0; i < nn->nanpa_nimi; i++) {
-#ifdef HAVE_STRLCAT
- strlcat(nimi_pana,
+ catstr(nimi_pana,
pana_e_nimi(nn->nanpa_sewi_nimi[i]),
nn->suli_pi_nimi_sewi);
- strlcat(nimi_pana, " ", nn->suli_pi_nimi_sewi);
-#else
- strcat(nimi_pana,
- pana_e_nimi(nn->nanpa_sewi_nimi[i]));
- strcat(nimi_pana, " ");
-#endif
+ catstr(nimi_pana, " ", nn->suli_pi_nimi_sewi);
}
-#ifdef HAVE_STRLCAT
- strlcat(nimi_pana,
+ catstr(nimi_pana,
pana_e_nimi(nn->nanpa_sewi_nimi[i]),
nn->suli_pi_nimi_sewi);
-#else
- strcat(nimi_pana,
- pana_e_nimi(nn->nanpa_sewi_nimi[i]));
-#endif
weka_e_nanpa_nimi(nn);