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