X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ftool%2Ftool.c;h=8fe6e05e9fa74d84b7675a29b7d9b3d0a3cba058;hb=259c314d142abd6f9295047c116235cfdd119563;hp=9e2907494b4c9402a79d7b2b1792f5bc3003a590;hpb=3a826b774a10a069d2f18b1a07842acf8da8fc49;p=ngircd-alex.git diff --git a/src/tool/tool.c b/src/tool/tool.c index 9e290749..8fe6e05e 100644 --- a/src/tool/tool.c +++ b/src/tool/tool.c @@ -7,18 +7,21 @@ * 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" -#include "imp.h" +/** + * @file + * Tool functions + */ + #include #include #include +#include #include +#include #include @@ -27,7 +30,6 @@ #include #endif -#include "exp.h" #include "tool.h" @@ -80,7 +82,7 @@ ngt_UpperStr(char *String) ptr = String; while(*ptr) { - *ptr = toupper(*ptr); + *ptr = toupper((int)*ptr); ptr++; } return String; @@ -99,7 +101,7 @@ ngt_LowerStr(char *String) ptr = String; while(*ptr) { - *ptr = tolower(*ptr); + *ptr = tolower((int)*ptr); ptr++; } return String; @@ -127,6 +129,35 @@ 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) +{ + static const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$&'()*+,-./:;<=>?@[\\]^_`"; + struct timeval t; + size_t i; + + assert(String != NULL); + + gettimeofday(&t, NULL); +#ifndef HAVE_ARC4RANDOM + srand((unsigned)(t.tv_usec * t.tv_sec)); + + for (i = 0; i < len; ++i) { + String[i] = chars[rand() % (sizeof(chars) - 1)]; + } +#else + for (i = 0; i < len; ++i) + String[i] = chars[arc4random() % (sizeof(chars) - 1)]; +#endif + String[len] = '\0'; + + return String; +} /* ngt_RandomStr */ + + #ifdef SYSLOG @@ -186,8 +217,8 @@ CODE facilitynames[] = { #endif -GLOBAL const char -*ngt_SyslogFacilityName(int Facility) +GLOBAL const char* +ngt_SyslogFacilityName(int Facility) { int i = 0; while(facilitynames[i].c_name) {