X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ftool%2Ftool.c;h=3b9cc255aed18141c38d6548804798bf396ff125;hb=61d1c864c55291c1f5f81f284e984b044fe2722f;hp=ef3fb5d73467355782698aa0b5c78ac44b50bad1;hpb=03628dbeaf40a9de34b3eb6d5bf6dd34eed8248c;p=ngircd-alex.git diff --git a/src/tool/tool.c b/src/tool/tool.c index ef3fb5d7..3b9cc255 100644 --- a/src/tool/tool.c +++ b/src/tool/tool.c @@ -20,7 +20,9 @@ #include #include #include +#include #include +#include #include @@ -82,7 +84,7 @@ ngt_UpperStr(char *String) ptr = String; while(*ptr) { - *ptr = toupper(*ptr); + *ptr = toupper((int)*ptr); ptr++; } return String; @@ -101,7 +103,7 @@ ngt_LowerStr(char *String) ptr = String; while(*ptr) { - *ptr = tolower(*ptr); + *ptr = tolower((int)*ptr); ptr++; } return String; @@ -129,6 +131,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