tool.c 4.2 KB

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