tool.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Tool functions
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #include <netinet/in.h>
  24. #ifdef SYSLOG
  25. #define SYSLOG_NAMES 1
  26. #include <syslog.h>
  27. #endif
  28. #include "exp.h"
  29. #include "tool.h"
  30. /**
  31. * Removes all leading and trailing whitespaces of a string.
  32. * @param String The string to remove whitespaces from.
  33. */
  34. GLOBAL void
  35. ngt_TrimStr(char *String)
  36. {
  37. char *start, *end;
  38. assert(String != NULL);
  39. start = String;
  40. /* Remove whitespaces at the beginning of the string ... */
  41. while (*start == ' ' || *start == '\t' ||
  42. *start == '\n' || *start == '\r')
  43. start++;
  44. if (!*start) {
  45. *String = '\0';
  46. return;
  47. }
  48. /* ... and at the end: */
  49. end = strchr(start, '\0');
  50. end--;
  51. while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
  52. && end >= start)
  53. end--;
  54. /* New trailing NULL byte */
  55. *(++end) = '\0';
  56. memmove(String, start, (size_t)(end - start)+1);
  57. } /* ngt_TrimStr */
  58. /**
  59. * Convert a string to uppercase letters.
  60. */
  61. GLOBAL char *
  62. ngt_UpperStr(char *String)
  63. {
  64. char *ptr;
  65. assert(String != NULL);
  66. ptr = String;
  67. while(*ptr) {
  68. *ptr = toupper((int)*ptr);
  69. ptr++;
  70. }
  71. return String;
  72. } /* ngt_UpperStr */
  73. /**
  74. * Convert a string to lowercase letters.
  75. */
  76. GLOBAL char *
  77. ngt_LowerStr(char *String)
  78. {
  79. char *ptr;
  80. assert(String != NULL);
  81. ptr = String;
  82. while(*ptr) {
  83. *ptr = tolower((int)*ptr);
  84. ptr++;
  85. }
  86. return String;
  87. } /* ngt_LowerStr */
  88. GLOBAL void
  89. ngt_TrimLastChr( char *String, const char Chr)
  90. {
  91. /* If last character in the string matches Chr, remove it.
  92. * Empty strings are handled correctly. */
  93. size_t len;
  94. assert(String != NULL);
  95. len = strlen(String);
  96. if(len == 0)
  97. return;
  98. len--;
  99. if(String[len] == Chr)
  100. String[len] = '\0';
  101. } /* ngt_TrimLastChr */
  102. /**
  103. * Fill a String with random chars
  104. */
  105. GLOBAL char *
  106. ngt_RandomStr(char *String, const size_t len)
  107. {
  108. static const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$&'()*+,-./:;<=>?@[\\]^_`";
  109. struct timeval t;
  110. size_t i;
  111. assert(String != NULL);
  112. gettimeofday(&t, NULL);
  113. #ifndef HAVE_ARC4RANDOM
  114. srand((unsigned)(t.tv_usec * t.tv_sec));
  115. for (i = 0; i < len; ++i) {
  116. String[i] = chars[rand() % (sizeof(chars) - 1)];
  117. }
  118. #else
  119. for (i = 0; i < len; ++i)
  120. String[i] = chars[arc4random() % (sizeof(chars) - 1)];
  121. #endif
  122. String[len] = '\0';
  123. return String;
  124. } /* ngt_RandomStr */
  125. #ifdef SYSLOG
  126. #ifndef INTERNAL_MARK
  127. #ifndef _code
  128. typedef struct _code {
  129. char *c_name;
  130. int c_val;
  131. } CODE;
  132. #endif
  133. CODE facilitynames[] = {
  134. #ifdef LOG_AUTH
  135. { "auth", LOG_AUTH },
  136. #endif
  137. #ifdef LOG_AUTHPRIV
  138. { "authpriv", LOG_AUTHPRIV },
  139. #endif
  140. #ifdef LOG_CRON
  141. { "cron", LOG_CRON },
  142. #endif
  143. #ifdef LOG_DAEMON
  144. { "daemon", LOG_DAEMON },
  145. #endif
  146. #ifdef LOG_FTP
  147. { "ftp", LOG_FTP },
  148. #endif
  149. #ifdef LOG_LPR
  150. { "lpr", LOG_LPR },
  151. #endif
  152. #ifdef LOG_MAIL
  153. { "mail", LOG_MAIL },
  154. #endif
  155. #ifdef LOG_NEWS
  156. { "news", LOG_NEWS },
  157. #endif
  158. #ifdef LOG_UUCP
  159. { "uucp", LOG_UUCP },
  160. #endif
  161. #ifdef LOG_USER
  162. { "user", LOG_USER },
  163. #endif
  164. #ifdef LOG_LOCAL7
  165. { "local0", LOG_LOCAL0 },
  166. { "local1", LOG_LOCAL1 },
  167. { "local2", LOG_LOCAL2 },
  168. { "local3", LOG_LOCAL3 },
  169. { "local4", LOG_LOCAL4 },
  170. { "local5", LOG_LOCAL5 },
  171. { "local6", LOG_LOCAL6 },
  172. { "local7", LOG_LOCAL7 },
  173. #endif
  174. { 0, -1 }
  175. };
  176. #endif
  177. GLOBAL const char*
  178. ngt_SyslogFacilityName(int Facility)
  179. {
  180. int i = 0;
  181. while(facilitynames[i].c_name) {
  182. if (facilitynames[i].c_val == Facility)
  183. return facilitynames[i].c_name;
  184. i++;
  185. }
  186. return "unknown";
  187. }
  188. GLOBAL int
  189. ngt_SyslogFacilityID(char *Name, int DefaultFacility)
  190. {
  191. int i = 0;
  192. while(facilitynames[i].c_name) {
  193. if (strcasecmp(facilitynames[i].c_name, Name) == 0)
  194. return facilitynames[i].c_val;
  195. i++;
  196. }
  197. return DefaultFacility;
  198. }
  199. #endif
  200. /* -eof- */