2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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.
26 #include <netinet/in.h>
29 #define SYSLOG_NAMES 1
37 * Removes all leading and trailing whitespaces of a string.
38 * @param String The string to remove whitespaces from.
41 ngt_TrimStr(char *String)
45 assert(String != NULL);
49 /* Remove whitespaces at the beginning of the string ... */
50 while (*start == ' ' || *start == '\t' ||
51 *start == '\n' || *start == '\r')
59 /* ... and at the end: */
60 end = strchr(start, '\0');
62 while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
66 /* New trailing NULL byte */
69 memmove(String, start, (size_t)(end - start)+1);
74 * Convert a string to uppercase letters.
77 ngt_UpperStr(char *String)
81 assert(String != NULL);
85 *ptr = toupper((int)*ptr);
93 * Convert a string to lowercase letters.
96 ngt_LowerStr(char *String)
100 assert(String != NULL);
104 *ptr = tolower((int)*ptr);
112 ngt_TrimLastChr( char *String, const char Chr)
114 /* If last character in the string matches Chr, remove it.
115 * Empty strings are handled correctly. */
119 assert(String != NULL);
121 len = strlen(String);
127 if(String[len] == Chr)
129 } /* ngt_TrimLastChr */
133 * Fill a String with random chars
136 ngt_RandomStr(char *String, const size_t len)
138 static const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$&'()*+,-./:;<=>?@[\\]^_`";
142 assert(String != NULL);
144 gettimeofday(&t, NULL);
145 #ifndef HAVE_ARC4RANDOM
146 srand((unsigned)(t.tv_usec * t.tv_sec));
148 for (i = 0; i < len; ++i) {
149 String[i] = chars[rand() % (sizeof(chars) - 1)];
152 for (i = 0; i < len; ++i)
153 String[i] = chars[arc4random() % (sizeof(chars) - 1)];
158 } /* ngt_RandomStr */
164 #ifndef INTERNAL_MARK
167 typedef struct _code {
173 CODE facilitynames[] = {
175 { "auth", LOG_AUTH },
178 { "authpriv", LOG_AUTHPRIV },
181 { "cron", LOG_CRON },
184 { "daemon", LOG_DAEMON },
193 { "mail", LOG_MAIL },
196 { "news", LOG_NEWS },
199 { "uucp", LOG_UUCP },
202 { "user", LOG_USER },
205 { "local0", LOG_LOCAL0 },
206 { "local1", LOG_LOCAL1 },
207 { "local2", LOG_LOCAL2 },
208 { "local3", LOG_LOCAL3 },
209 { "local4", LOG_LOCAL4 },
210 { "local5", LOG_LOCAL5 },
211 { "local6", LOG_LOCAL6 },
212 { "local7", LOG_LOCAL7 },
221 ngt_SyslogFacilityName(int Facility)
224 while(facilitynames[i].c_name) {
225 if (facilitynames[i].c_val == Facility)
226 return facilitynames[i].c_name;
234 ngt_SyslogFacilityID(char *Name, int DefaultFacility)
237 while(facilitynames[i].c_name) {
238 if (strcasecmp(facilitynames[i].c_name, Name) == 0)
239 return facilitynames[i].c_val;
242 return DefaultFacility;