tool.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <string.h>
  21. #include <netinet/in.h>
  22. #ifdef SYSLOG
  23. #define SYSLOG_NAMES 1
  24. #include <syslog.h>
  25. #endif
  26. #include "exp.h"
  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 = 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 = 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. #ifdef SYSLOG
  101. #ifndef INTERNAL_MARK
  102. #ifndef _code
  103. typedef struct _code {
  104. char *c_name;
  105. int c_val;
  106. } CODE;
  107. #endif
  108. CODE facilitynames[] = {
  109. #ifdef LOG_AUTH
  110. { "auth", LOG_AUTH },
  111. #endif
  112. #ifdef LOG_AUTHPRIV
  113. { "authpriv", LOG_AUTHPRIV },
  114. #endif
  115. #ifdef LOG_CRON
  116. { "cron", LOG_CRON },
  117. #endif
  118. #ifdef LOG_DAEMON
  119. { "daemon", LOG_DAEMON },
  120. #endif
  121. #ifdef LOG_FTP
  122. { "ftp", LOG_FTP },
  123. #endif
  124. #ifdef LOG_LPR
  125. { "lpr", LOG_LPR },
  126. #endif
  127. #ifdef LOG_MAIL
  128. { "mail", LOG_MAIL },
  129. #endif
  130. #ifdef LOG_NEWS
  131. { "news", LOG_NEWS },
  132. #endif
  133. #ifdef LOG_UUCP
  134. { "uucp", LOG_UUCP },
  135. #endif
  136. #ifdef LOG_USER
  137. { "user", LOG_USER },
  138. #endif
  139. #ifdef LOG_LOCAL7
  140. { "local0", LOG_LOCAL0 },
  141. { "local1", LOG_LOCAL1 },
  142. { "local2", LOG_LOCAL2 },
  143. { "local3", LOG_LOCAL3 },
  144. { "local4", LOG_LOCAL4 },
  145. { "local5", LOG_LOCAL5 },
  146. { "local6", LOG_LOCAL6 },
  147. { "local7", LOG_LOCAL7 },
  148. #endif
  149. { 0, -1 }
  150. };
  151. #endif
  152. GLOBAL const char*
  153. ngt_SyslogFacilityName(int Facility)
  154. {
  155. int i = 0;
  156. while(facilitynames[i].c_name) {
  157. if (facilitynames[i].c_val == Facility)
  158. return facilitynames[i].c_name;
  159. i++;
  160. }
  161. return "unknown";
  162. }
  163. GLOBAL int
  164. ngt_SyslogFacilityID(char *Name, int DefaultFacility)
  165. {
  166. int i = 0;
  167. while(facilitynames[i].c_name) {
  168. if (strcasecmp(facilitynames[i].c_name, Name) == 0)
  169. return facilitynames[i].c_val;
  170. i++;
  171. }
  172. return DefaultFacility;
  173. }
  174. #endif
  175. /* -eof- */