This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrkey.c
74 lines (66 loc) · 1.44 KB
/
strkey.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
/* $Id: strkey.c,v 4.9 2011/01/02 19:24:03 tom Exp $
*
* Some string handling routines
*/
#include <string.h>
#include <cproto.h>
#define LETTER(c) (isalnum(UCH(c)) || (c == '_') || (c == '$'))
/*
* Return a pointer to the first occurence of the given keyword in the string
* or NULL if not found. Unlike 'strstr()', which verifies that the match is
* against an identifier-token.
*/
char *
strkey(char *src, const char *key)
{
if (src != 0 && key != 0) {
register char *s = src, *d;
register size_t len = strlen(key);
while (*s) {
if (!LETTER(*s)) {
s++;
} else {
for (d = s; LETTER(*d); d++) ;
if ((d - s) == (int) len && !strncmp(s, key, len))
return s;
s = d;
}
}
}
return NULL;
}
/*
* Delete a specified keyword from a string if it appears there
*/
void
strcut(char *src, const char *key)
{
register char *s, *d;
if ((s = strkey(src, key)) != '\0') {
d = s + strlen(key);
while (*d != '\0' && !LETTER(*d))
d++;
while ((*s++ = *d++) != '\0') ;
}
}
/* Search for a substring within the given string.
* Return a pointer to the first occurence within the string,
* or NULL if not found.
*/
#if !HAVE_STRSTR
char *
strstr(char *src, char *key)
{
char *s;
int keylen;
if ((keylen = strlen(key)) == 0)
return src;
s = strchr(src, *key);
while (s != NULL) {
if (strncmp(s, key, keylen) == 0)
return s;
s = strchr(s + 1, *key);
}
return NULL;
}
#endif