369ae54e6ac43c41ff0afbb91ff22df798d9f736
[reactos.git] / freeldr / freeldr / rtl / string.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <rtl.h>
21
22 int strlen(char *str)
23 {
24 int len;
25
26 for(len=0; str[len] != '\0'; len++);
27
28 return len;
29 }
30
31 char *strcpy(char *dest, char *src)
32 {
33 char *ret = dest;
34
35 while(*src)
36 *dest++ = *src++;
37 *dest = 0;
38
39 return ret;
40 }
41
42 char *strncpy(char *dest, char *src, size_t count)
43 {
44 char *ret = dest;
45
46 while((*src) && (count--))
47 *dest++ = *src++;
48
49 if (count)
50 *dest = 0;
51
52 return ret;
53 }
54
55 char *strcat(char *dest, char *src)
56 {
57 char *ret = dest;
58
59 while(*dest)
60 dest++;
61
62 while(*src)
63 *dest++ = *src++;
64 *dest = 0;
65
66 return ret;
67 }
68
69 char *strncat(char *dst, const char *src, size_t n)
70 {
71 if (n != 0)
72 {
73 char *d = dst;
74 const char *s = src;
75
76 while (*d != 0)
77 d++;
78 do
79 {
80 if ((*d = *s++) == 0)
81 break;
82 d++;
83 }
84 while (--n != 0);
85 *d = 0;
86 }
87
88 return dst;
89 }
90
91 char *strchr(const char *s, int c)
92 {
93 char cc = c;
94 while (*s)
95 {
96 if (*s == cc)
97 return (char *)s;
98 s++;
99 }
100 if (cc == 0)
101 return (char *)s;
102 return 0;
103 }
104
105 char *strrchr(const char *s, int c)
106 {
107 char cc = c;
108 const char *sp=(char *)0;
109 while (*s)
110 {
111 if (*s == cc)
112 sp = s;
113 s++;
114 }
115 if (cc == 0)
116 sp = s;
117 return (char *)sp;
118 }
119
120 int strcmp(const char *string1, const char *string2)
121 {
122 while(*string1 == *string2)
123 {
124 if(*string1 == 0)
125 return 0;
126
127 string1++;
128 string2++;
129 }
130
131 return *(unsigned const char *)string1 - *(unsigned const char *)(string2);
132 }
133
134 int stricmp(const char *string1, const char *string2)
135 {
136 while(tolower(*string1) == tolower(*string2))
137 {
138 if(*string1 == 0)
139 return 0;
140
141 string1++;
142 string2++;
143 }
144
145 return (int)tolower(*string1) - (int)tolower(*string2);
146 }
147
148 int strnicmp(const char *string1, const char *string2, size_t length)
149 {
150 if (length == 0)
151 return 0;
152 do
153 {
154 if (toupper(*string1) != toupper(*string2++))
155 return toupper(*(unsigned const char *)string1) - toupper(*(unsigned const char *)--string2);
156 if (*string1++ == 0)
157 break;
158 }
159 while (--length != 0);
160 return 0;
161 }
162
163 int strncmp(const char *string1, const char *string2, size_t length)
164 {
165 if (length == 0)
166 return 0;
167 do
168 {
169 if (*string1 != *string2++)
170 return *(unsigned const char *)string1 - *(unsigned const char *)--string2;
171 if (*string1++ == 0)
172 break;
173 }
174 while (--length != 0);
175 return 0;
176 }