-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.c
303 lines (259 loc) · 6.61 KB
/
str.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include "logger.h"
#include "str.h"
#include "support.h"
char *str_dup(const char *str) {
register char *ptr = NULL;
if (str == NULL)
return NULL;
if ((ptr = strdup(str)) == NULL)
Log(LOG_ERROR, "%s:strdup %d:%s", __FUNCTION__, errno, strerror(errno));
return ptr;
}
void str_tohex(char *dst, const char *src, unsigned int length) {
char hex[17] = "0123456789abcdef";
unsigned int i;
for (i = 0; i < length; i++) {
dst[2 * i] = hex[(src[i] & 0xf0) >> 4];
dst[2 * i + 1] = hex[(src[i] & 0x0f)];
}
dst[2 * i] = 0;
}
/* Sentinel - IRC Statistical and Operator Services
** s_string.c - String manipulation functions
**
** Copyright W. Campbell and others. See README for more details
** Some code Copyright: Jonathan George, Kai Seidler, ircd-hybrid Team,
** IRCnet IRCD developers.
**
** $Id: s_string.c,v 1.4 2003/11/06 20:41:53 wcampbel Exp $
*/
/* Our own ctime function */
/* Modified for l10n - 12 f\xe9v 2002 -Hwy */
char *str_ctime(time_t stuff) {
static char buf[32];
struct tm *ltm = localtime(&stuff);
static char *gcc_sucks = "%c";
/*
* %c is the POSIX localized replacement for ctime()
*/
/*
* gcc complains about some non-BSD implementations not using
* ** a 4 digit year in some locales. The warning can be ignored.
* **
* ** The lameness in this function is the only way I know of that
* ** the warning can be ignored.
* **
* ** Some versions of gcc support -Wno-y2k, but apparently it's
* ** only gcc3 and higher. Using a custom -W parameter will cause
* ** issues when run with another compiler, so for now, this lameness
* ** stays.
*/
strftime(buf, 32, gcc_sucks, ltm);
return buf;
}
/* tokenize splits apart a message (in our case with the origin and command
** picked off already...
*/
int str_tokenize(char *message, char **parv) {
char *pos = NULL;
char *next;
int count = 0;
if (!message) {
/*
* Something is seriously wrong...bail
*/
return -1;
}
/*
* First we find out if there's a : in the message, save that string
* ** somewhere so we can set it to the last param in parv
* ** Also make sure there's a space before it...if not, then we're
* ** screwed
*/
pos = message;
while (TRUE) {
if ((pos = strchr(pos, ':'))) {
pos--;
if (*pos != ' ') {
/*
* There SHOULDN'T be a problem with this...
*/
pos += 2;
continue;
}
*pos = '\0';
/*
* This is BAD - get rid of the space before the :
*/
pos++;
*pos = '\0';
/*
* VERY BAD...over now though
*/
pos++;
break;
} else {
break;
}
}
/*
* Now we take the beginning of message and find all the spaces...
* ** set them to \0 and use 'next' to go through the string
*/
next = message;
parv[0] = message;
count = 1;
while (*next) {
if (count == MAXPARA - 1) {
/*
* We've reached one less than our max limit
* ** to handle the parameter we already ripped off
*/
Log(LOG_DEBUG, "DEBUG: Reached the MAXPARA limit!");
return count;
}
if (*next == ' ') {
*next = '\0';
next++;
/*
* Eat any additional spaces
*/
while (*next == ' ')
next++;
/*
* If it's the end of the string, it's simply
* ** an extra space before the :parameter. Here we
* ** break.
*/
if (*next == '\0')
break;
parv[count] = next;
count++;
} else {
next++;
}
}
if (pos) {
parv[count] = pos;
count++;
}
return count;
}
/* A generic tokenizer, tokenizes based on a specified character */
int str_tokenize_generic(char delim, int size, char *message, char **parv) {
char *next;
int count;
if (!message) {
/*
* Something is seriously wrong...bail
*/
parv[0] = NULL;
return 0;
}
/*
* Now we take the beginning of message and find all the spaces...
* ** set them to \0 and use 'next' to go through the string
*/
next = message;
parv[0] = next;
count = 1;
while (*next) {
/*
* This is fine here, since we don't have a :delimited
* ** parameter like tokenize
*/
if (count == size) {
/*
* We've reached our limit
*/
Log(LOG_DEBUG, "Reached the size limit!");
return count;
}
if (*next == delim) {
*next = '\0';
next++;
/*
* Eat any additional delimiters
*/
while (*next == delim)
next++;
/*
* If it's the end of the string, it's simply
* ** an extra space at the end. Here we break.
*/
if (*next == '\0')
break;
parv[count] = next;
count++;
} else {
next++;
}
}
return count;
}
void str_parv_expand(char *dest, int max, int initial, int parc, char *parv[]) {
int i;
if (parv[initial])
strlcpy(dest, parv[initial], max);
for (i = initial + 1; i < parc; i++) {
strlcat(dest, " ", max);
strlcat(dest, parv[i], max);
}
}
char *str_tolower(char *s) {
char *t;
for (t = s; *t != '\0'; t++)
*t = tolower(*t);
return s;
}
/* Return 1 if the string s is all lower case, return 0 if not */
int stris_lower(char *s) {
char *t;
if (s == NULL)
return 1;
for (t = s; *t != '\0'; t++) {
if (islower((int)*t) == 0) {
return 0;
}
}
return 1;
}
/* Remove CR and LF from the line input. ASSUME that the line
** does NOT contain CR or LF in the middle
*/
void str_strip(char *line) {
char *c;
if ((c = strchr(line, '\r'))) {
/*
* If we receive a CR, then we can just terminate the
* ** call here
*/
*c = '\0';
return;
}
if ((c = strchr(line, '\n'))) {
*c = '\0';
return;
}
}
char *str_unquote(char *str) {
char *tmp;
if (str && *str == '"')
str++;
if ((tmp = index(str, '"')))
*tmp = '\0';
return str;
}
char *str_whitespace_skip(char *str) {
/*
* Skip over white space
*/
while (*str && (*str == ' ' || *str == '\t'))
str++;
return str;
}