]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/tool/tool.c
ngt_RandomStr(): Add implicit cast to "unsigned".
[ngircd-alex.git] / src / tool / tool.c
index dbdb49a2d9904d97d1fe7369c95b56780ac6da8d..df10918893348a4a1ef4205938e00db49eddb6b7 100644 (file)
@@ -7,18 +7,22 @@
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
- *
- * Tool functions
  */
 
-
 #include "portab.h"
 
+/**
+ * @file
+ * Tool functions
+ */
+
 #include "imp.h"
 #include <assert.h>
 #include <ctype.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <sys/time.h>
 
 #include <netinet/in.h>
 
@@ -127,6 +131,34 @@ ngt_TrimLastChr( char *String, const char Chr)
 } /* ngt_TrimLastChr */
 
 
+/**
+ * Fill a String with random chars
+ */
+GLOBAL char *
+ngt_RandomStr( char *String, const size_t len)
+{
+       assert(String != NULL);
+
+       static const char chars[] = 
+               "0123456789ABCDEFGHIJKLMNO"
+               "PQRSTUVWXYZabcdefghijklmn"
+               "opqrstuvwxyz!\"#$&'()*+,-"
+               "./:;<=>?@[\\]^_`";
+
+       struct timeval t;
+       gettimeofday(&t, NULL);
+       srand((unsigned)(t.tv_usec * t.tv_sec));
+
+       for (size_t i = 0; i < len; ++i) {
+               String[i] = chars[rand() % (sizeof(chars) - 1)];
+       }
+
+       String[len] = '\0';
+
+       return String;
+} /* ngt_RandomStr */
+
+
 #ifdef SYSLOG