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