tool.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. srand((unsigned)(t.tv_usec * t.tv_sec));
  114. for (i = 0; i < len; ++i) {
  115. String[i] = chars[rand() % (sizeof(chars) - 1)];
  116. }
  117. String[len] = '\0';
  118. return String;
  119. } /* ngt_RandomStr */
  120. #ifdef SYSLOG
  121. #ifndef INTERNAL_MARK
  122. #ifndef _code
  123. typedef struct _code {
  124. char *c_name;
  125. int c_val;
  126. } CODE;
  127. #endif
  128. CODE facilitynames[] = {
  129. #ifdef LOG_AUTH
  130. { "auth", LOG_AUTH },
  131. #endif
  132. #ifdef LOG_AUTHPRIV
  133. { "authpriv", LOG_AUTHPRIV },
  134. #endif
  135. #ifdef LOG_CRON
  136. { "cron", LOG_CRON },
  137. #endif
  138. #ifdef LOG_DAEMON
  139. { "daemon", LOG_DAEMON },
  140. #endif
  141. #ifdef LOG_FTP
  142. { "ftp", LOG_FTP },
  143. #endif
  144. #ifdef LOG_LPR
  145. { "lpr", LOG_LPR },
  146. #endif
  147. #ifdef LOG_MAIL
  148. { "mail", LOG_MAIL },
  149. #endif
  150. #ifdef LOG_NEWS
  151. { "news", LOG_NEWS },
  152. #endif
  153. #ifdef LOG_UUCP
  154. { "uucp", LOG_UUCP },
  155. #endif
  156. #ifdef LOG_USER
  157. { "user", LOG_USER },
  158. #endif
  159. #ifdef LOG_LOCAL7
  160. { "local0", LOG_LOCAL0 },
  161. { "local1", LOG_LOCAL1 },
  162. { "local2", LOG_LOCAL2 },
  163. { "local3", LOG_LOCAL3 },
  164. { "local4", LOG_LOCAL4 },
  165. { "local5", LOG_LOCAL5 },
  166. { "local6", LOG_LOCAL6 },
  167. { "local7", LOG_LOCAL7 },
  168. #endif
  169. { 0, -1 }
  170. };
  171. #endif
  172. GLOBAL const char*
  173. ngt_SyslogFacilityName(int Facility)
  174. {
  175. int i = 0;
  176. while(facilitynames[i].c_name) {
  177. if (facilitynames[i].c_val == Facility)
  178. return facilitynames[i].c_name;
  179. i++;
  180. }
  181. return "unknown";
  182. }
  183. GLOBAL int
  184. ngt_SyslogFacilityID(char *Name, int DefaultFacility)
  185. {
  186. int i = 0;
  187. while(facilitynames[i].c_name) {
  188. if (strcasecmp(facilitynames[i].c_name, Name) == 0)
  189. return facilitynames[i].c_val;
  190. i++;
  191. }
  192. return DefaultFacility;
  193. }
  194. #endif
  195. /* -eof- */