summaryrefslogtreecommitdiff
path: root/deprecated-ngircd/src/tool/tool.c
blob: 35c1ee67253302831e90314ac2c9c8ce02f5e82a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * ngIRCd -- The Next Generation IRC Daemon
 * Copyright (c)2001-2018 Alexander Barton (alex@barton.de) and Contributors.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * Please read the file COPYING, README and AUTHORS for more information.
 */

#include "portab.h"

/**
 * @file
 * Tool functions
 */

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include <netinet/in.h>

#ifdef SYSLOG
#define SYSLOG_NAMES 1
#include <syslog.h>
#endif

#include "tool.h"


/**
 * Removes all leading and trailing whitespaces of a string.
 * @param String The string to remove whitespaces from.
 */
GLOBAL void
ngt_TrimStr(char *String)
{
	char *start, *end;

	assert(String != NULL);

	start = String;

	/* Remove whitespaces at the beginning of the string ... */
	while (*start == ' ' || *start == '\t' ||
	       *start == '\n' || *start == '\r')
		start++;

	if (!*start) {
		*String = '\0';
		return;
	}

	/* ... and at the end: */
	end = strchr(start, '\0');
	end--;
	while ((*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')
	       && end >= start)
		end--;

	/* New trailing NULL byte */
	*(++end) = '\0';

	memmove(String, start, (size_t)(end - start)+1);
} /* ngt_TrimStr */


/**
 * Convert a string to uppercase letters.
 */
GLOBAL char *
ngt_UpperStr(char *String)
{
	char *ptr;

	assert(String != NULL);

	ptr = String;
	while(*ptr) {
		*ptr = (char)toupper(*ptr);
		ptr++;
	}
	return String;
} /* ngt_UpperStr */


/**
 * Convert a string to lowercase letters.
 */
GLOBAL char *
ngt_LowerStr(char *String)
{
	char *ptr;

	assert(String != NULL);

	ptr = String;
	while(*ptr) {
		*ptr = (char)tolower(*ptr);
		ptr++;
	}
	return String;
} /* ngt_LowerStr */


GLOBAL void
ngt_TrimLastChr( char *String, const char Chr)
{
	/* If last character in the string matches Chr, remove it.
	 * Empty strings are handled correctly. */

	size_t len;

	assert(String != NULL);

	len = strlen(String);
	if(len == 0)
		return;

	len--;

	if(String[len] == Chr)
		String[len] = '\0';
} /* ngt_TrimLastChr */


/**
 * Fill a String with random chars
 */
GLOBAL char *
ngt_RandomStr(char *String, const size_t len)
{
	static const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!\"#$&'()*+,-./:;<=>?@[\\]^_`";
	struct timeval t;
	size_t i;

	assert(String != NULL);

	gettimeofday(&t, NULL);
#ifndef HAVE_ARC4RANDOM
	srand((unsigned)(t.tv_usec * t.tv_sec));

	for (i = 0; i < len; ++i) {
		String[i] = chars[rand() % (sizeof(chars) - 1)];
	}
#else
	for (i = 0; i < len; ++i)
		String[i] = chars[arc4random() % (sizeof(chars) - 1)];
#endif
	String[len] = '\0';

	return String;
} /* ngt_RandomStr */


#ifdef SYSLOG


#ifndef INTERNAL_MARK

#ifndef _code
typedef struct _code {
        char    *c_name;
        int     c_val;
} CODE;
#endif

CODE facilitynames[] = {
#ifdef LOG_AUTH
	{ "auth",	LOG_AUTH },
#endif
#ifdef LOG_AUTHPRIV
	{ "authpriv",	LOG_AUTHPRIV },
#endif
#ifdef LOG_CRON
	{ "cron", 	LOG_CRON },
#endif
#ifdef LOG_DAEMON
	{ "daemon",	LOG_DAEMON },
#endif
#ifdef LOG_FTP
	{ "ftp",	LOG_FTP },
#endif
#ifdef LOG_LPR
	{ "lpr",	LOG_LPR },
#endif
#ifdef LOG_MAIL
	{ "mail",	LOG_MAIL },
#endif
#ifdef LOG_NEWS
	{ "news",	LOG_NEWS },
#endif
#ifdef LOG_UUCP
	{ "uucp",	LOG_UUCP },
#endif
#ifdef LOG_USER
	{ "user",	LOG_USER },
#endif
#ifdef LOG_LOCAL7
	{ "local0",	LOG_LOCAL0 },
	{ "local1",	LOG_LOCAL1 },
	{ "local2",	LOG_LOCAL2 },
	{ "local3",	LOG_LOCAL3 },
	{ "local4",	LOG_LOCAL4 },
	{ "local5",	LOG_LOCAL5 },
	{ "local6",	LOG_LOCAL6 },
	{ "local7",	LOG_LOCAL7 },
#endif
	{ 0,		-1 }
};

#endif


GLOBAL const char*
ngt_SyslogFacilityName(int Facility)
{
	int i = 0;
	while(facilitynames[i].c_name) {
		if (facilitynames[i].c_val == Facility)
			return facilitynames[i].c_name;
		i++;
	}
	return "unknown";
}


GLOBAL int
ngt_SyslogFacilityID(char *Name, int DefaultFacility)
{
	int i = 0;
	while(facilitynames[i].c_name) {
		if (strcasecmp(facilitynames[i].c_name, Name) == 0)
			return facilitynames[i].c_val;
		i++;
	}
	return DefaultFacility;
}


#endif


/* -eof- */