root/Whitix/trunk/lib/string.c

Revision 2029, 3.6 KB (checked in by mwhitworth, 3 years ago)

Add stpcpy function, that returns end of dest string.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 *
17 */
18
19#include <module.h>
20#include <string.h>
21#include <malloc.h>
22
23int strncpy(char* dest,char* src, int len)
24{
25        while (*src != '\0' && len--)
26                *dest++=*src++;
27
28        while (len--)
29                *dest++='\0';
30
31        return 0;
32}
33
34SYMBOL_EXPORT(strncpy);
35
36char* stpcpy(char* dest, char* src)
37{
38        while (*src != '\0')
39                *dest++=*src++;
40
41        return dest;
42}
43
44SYMBOL_EXPORT(stpcpy);
45
46int strncmp(const char* s1,const char* s2,int num)
47{
48        unsigned char res=0;
49
50    while (num)
51        {
52                if ((res=*s1-*s2++) || !*s1++)
53                        break;
54
55                --num;
56        }
57
58        return res;
59}
60
61SYMBOL_EXPORT(strncmp);
62
63int strnicmp(const char* s1,const char* s2,int num)
64{
65        unsigned char res=0;
66
67        while (num)
68        {
69                char a=toupper(*s1),b=toupper(*s2);
70                if ((res=a-b))
71                        break;
72
73                s1++; s2++;
74                --num;
75        }
76
77        return res;
78}
79
80SYMBOL_EXPORT(strnicmp);
81
82int stricmp(const char* s1, const char* s2)
83{
84        unsigned char res=0;
85
86        while (1)
87        {
88                char a = toupper(*s1), b = toupper(*s2);
89                if (( res = a - b ))
90                        break;
91
92                s1++; s2++;
93        }
94
95        return res;
96}
97
98SYMBOL_EXPORT(stricmp);
99
100int strlen(char* str)
101{
102        int ret=0;
103
104        while (*str++)
105                ++ret;
106
107        return ret;
108}
109
110SYMBOL_EXPORT(strlen);
111
112char* strcat(char *str1,const char *str2)
113{
114        char* target=str1;
115        while (*target)
116                target++;
117       
118        const char* src=str2;
119        while((*target++=*src++) != '\0');
120       
121        return str1;
122}
123
124SYMBOL_EXPORT(strcat);
125
126char* strncat(char* dest, const char* src, size_t count)
127{
128        char* tmp = dest;
129       
130        if (count)
131        {
132                while (*dest)
133                        dest++;
134                       
135                while ((*dest++ = *src++))
136                {
137                        if (!--count)
138                        {
139                                *dest = '\0';
140                                break;
141                        }
142                }
143        }
144       
145        return tmp;
146}
147
148SYMBOL_EXPORT(strncat);
149
150char* strchr(char* string,int c)
151{
152        for (; *string; string++)
153                if (*string == (char)c)
154                        return (char*)string;
155
156        return 0;
157}
158
159SYMBOL_EXPORT(strchr);
160
161char* strrchr(char* string,int c)
162{
163        for (string+=strlen(string); *string; string--)
164                if (*string == (char)c)
165                        return (char*)string;
166
167        return 0;
168}
169
170SYMBOL_EXPORT(strrchr);
171
172void* memset(void* pointer, unsigned int size,char value)
173{
174        char* iterate=(char*)pointer;
175       
176        while (size--)
177                *iterate++=value;
178
179        return pointer;
180}
181
182SYMBOL_EXPORT(memset);
183
184void* memsetw(void* pointer,int size,WORD value)
185{
186        WORD* iterate=(WORD*)pointer;
187
188        while (size--)
189                *iterate++=value;
190
191        return pointer;
192}
193
194SYMBOL_EXPORT(memsetw);
195
196int memcmp(const void *s1, const void *s2, int n)
197{
198        unsigned char* c1, *c2;
199        int res=0;
200
201        for (c1=(unsigned char*)s1, c2=(unsigned char*)s2; n; ++c1, ++c2, n--)
202        {
203                if ((res=*c1-*c2))
204                        break;
205        }
206
207        return res;
208}
209
210SYMBOL_EXPORT(memcmp);
211
212int toupper(char ch)
213{
214        if (ch>=97 && ch<=122)
215                ch-=0x20;
216   
217        return ch; 
218}
219
220SYMBOL_EXPORT(toupper);
221
222char* strdup(char* name)
223{
224        char* ret;
225       
226        ret = MemAlloc(strlen(name) + 1);
227        strncpy(ret, name, strlen(name));
228       
229        return ret;
230}
231
232SYMBOL_EXPORT(strdup);
233
234/* strcpy - mainly for use by Linux drivers; not recommended. */
235
236char* strcpy(char* dest, const char* src)
237{
238        char* save = dest;
239       
240        while (*src != '\0')
241                *dest++ = *src++;
242               
243        *dest='\0';
244       
245        return save;
246}
247
248SYMBOL_EXPORT(strcpy);
Note: See TracBrowser for help on using the browser.