forked from taraszka/sheerdns-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrutil.c
112 lines (94 loc) · 2.33 KB
/
strutil.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
/*
* Author: Paul Sheer
* Modifications: Krzysztof Taraszka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "strutil.h"
void
string_wash (unsigned char *p) {
unsigned char *q;
int n;
assert (p);
for (n = 0, q = p; *q && n < MAX_RECORD_LEN; q++, n++)
if (!issuspect (*q))
*p++ = *q;
*p = '\0'; }
void
string_purify (unsigned char *p) {
unsigned char *q;
int n;
assert (p);
for (n = 0, q = p; *q && n < MAX_RECORD_LEN; q++, n++)
if (!isevil (*q))
*p++ = *q;
*p = '\0'; }
void
string_chomp (unsigned char *p) {
unsigned char *q, *t;
assert (p);
/* skip over leading spaces */
for (q = p; *q; q++)
if (!isspace (*q))
break;
/* skip over non-space */
for (t = p; *q;)
if (!isspace (*p++ = *q++))
t = p;
/* truncate trailing space */
*t = '\0'; }
/* this is quite possibly my favorite function in the whole world */
char **
string_split (const char *s, char c, size_t max, int multi) {
char *p, **a;
int n;
for (n = 0, p = (char *) s; *p && n < max; n++) {
while (*p && *p != c)
p++;
if (multi) {
while (*p && *p == c)
p++; }
else if (*p)
p++; }
if (!(a = malloc ((n + 1) * sizeof (char *) + strlen (s) + 1)))
return 0;
p = (char *) (&(a[n + 1]));
for (n = 0; *s && n < max; n++) {
a[n] = p;
while (*s && *s != c)
*p++ = *s++;
if (multi) {
*p = '\0';
while (*s && *s == c) {
*p++ = '\0';
s++; }}
else if (*s) {
*p++ = '\0';
if (*s == c)
s++; }
else {
*p = '\0'; }}
a[n] = 0;
return a; }
int
string_present (char *s, char **a) {
int i;
for (i = 0; a[i]; i++)
if (!strcmp (a[i], s))
return 1;
return 0; }