aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/std/string.c
blob: 6f9649e0a799ab5e26940d56eadcd393007a8bd6 (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
/* 
 * Define constants used by other string services and case insensitive
 * compare and conversion functions missing in some libc distributions.
 * $Id$
 * Copyright (c) 1997 by Tycho Softworks.
 * For conditions of distribution and reuse see product license.
 */

#include <std/string.h>

#ifdef	STRLWR_F_MISSING

char    *strlwr(char *s)
{
	char    *old;

	if(!s)
		return NULL;
		
	old=s;
	while(*s = (char)tolower(*s))
		++s;
	
	return old;
}

char    *strupr(char *s)
{
	char    *old;

	if(!s)
		return NULL;
		
	old=s;
	while(*s = (char)toupper(*s))
		++s;
	return old;
}

#endif

#ifdef	STRDUP_F_MISSING

char	*strdup(str)
char	*str;
{
	char	*new = (char *)malloc(strlen(str) + 1);
	if(!new)
		return NULL;

	return strcpy(new, str);
}

#endif

#ifdef	STRICMP_F_MISSING

int stricmp(const char *s1, const char *s2)
{
	int t;

	while(*s1 && *s2)
	{
		if (t=tolower(*s1)-tolower(*s2))
			return  t;

		++s1;
		++s2;
	}
	return tolower(*s1)-tolower(*s2);
}

int strnicmp(const char *s1,const char *s2, size_t n)
{
	int t;

	while (n--)
	{
		if (t=tolower(*s1)-tolower(*s2))
			return  t;

		if (!*s1)
			return 0;

		++s1;
		++s2;
	}
	return 0;
}

#endif

#ifdef	STRISTR_F_MISSING

char	*stristr(char *s1, const char *s2)
{
	int	len = strlen(s2);
	int	count = strlen(s1) - len + 1;

	while(count--)
	{
		if(!strnicmp(s1, s2, len))
			return s1;
		++s1;
	}
	return NULL;
}

#endif