1 | /* $Id: string.c,v 1.1 2000-11-22 01:11:01 phaller Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * The C RunTime DLL
|
---|
5 | *
|
---|
6 | * Implements C run-time functionality as known from UNIX.
|
---|
7 | *
|
---|
8 | * Partialy based on Wine
|
---|
9 | *
|
---|
10 | * Copyright 1996,1998 Marcus Meissner
|
---|
11 | * Copyright 1996 Jukka Iivonen
|
---|
12 | * Copyright 1997 Uwe Bonnes
|
---|
13 | * Copyright 1999-2000 Jens Wiessner
|
---|
14 | *
|
---|
15 | * Implementation Notes:
|
---|
16 | * MT Safe.
|
---|
17 | */
|
---|
18 |
|
---|
19 | //#include <odin.h>
|
---|
20 | //#include <os2win.h>
|
---|
21 | //#include <ctype.h>
|
---|
22 | //#include <heapstring.h>
|
---|
23 | #include <string.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | #include "crtdll.h"
|
---|
27 | #include "winuser.h"
|
---|
28 |
|
---|
29 |
|
---|
30 | DEFAULT_DEBUG_CHANNEL(crtdll);
|
---|
31 |
|
---|
32 | /*********************************************************************
|
---|
33 | * _strcmpi (CRTDLL.280)
|
---|
34 | */
|
---|
35 | void CDECL CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
|
---|
36 | {
|
---|
37 | dprintf2(("CRTDLL: _strcmpi(%08xh, %08xh)\n",
|
---|
38 | s1,
|
---|
39 | s2));
|
---|
40 |
|
---|
41 | lstrcmpiA( s1, s2 );
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************
|
---|
46 | * CRTDLL__strdate (CRTDLL.281)
|
---|
47 | */
|
---|
48 | char * CDECL CRTDLL__strdate( char *buf )
|
---|
49 | {
|
---|
50 | dprintf2(("CRTDLL: _strdate\n"));
|
---|
51 | return(_strdate(buf));
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************
|
---|
56 | * _stricmp (CRTDLL.285)
|
---|
57 | */
|
---|
58 | int CDECL CRTDLL__stricmp(const LPSTR str1,
|
---|
59 | const LPSTR str2)
|
---|
60 | {
|
---|
61 | dprintf2(("CRTDLL: _stricmp(%s,%s)\n",
|
---|
62 | str1,
|
---|
63 | str2));
|
---|
64 |
|
---|
65 | return (stricmp(str1, str2));
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************
|
---|
71 | * CRTDLL__stricoll (CRTDLL.286)
|
---|
72 | */
|
---|
73 | int CDECL CRTDLL__stricoll( const char *s1, const char *s2 )
|
---|
74 | {
|
---|
75 | dprintf2(("CRTDLL: _stricoll\n"));
|
---|
76 | return stricmp(s1,s2);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /*********************************************************************
|
---|
81 | * _strlwr (CRTDLL.288)
|
---|
82 | */
|
---|
83 | CHAR * CDECL CRTDLL__strlwr(char *x)
|
---|
84 | {
|
---|
85 | char *y =x;
|
---|
86 |
|
---|
87 | dprintf2(("CRTDLL: _strlwr got %s\n", x));
|
---|
88 | while (*y) {
|
---|
89 | if ((*y > 0x40) && (*y< 0x5b))
|
---|
90 | *y = *y + 0x20;
|
---|
91 | y++;
|
---|
92 | }
|
---|
93 | dprintf2((" returned %s\n", x));
|
---|
94 |
|
---|
95 | return x;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /*********************************************************************
|
---|
100 | * CRTDLL__strnicmp (CRTDLL.291)
|
---|
101 | */
|
---|
102 | int CDECL CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
|
---|
103 | {
|
---|
104 | dprintf2(("CRTDLL: _strnicmp (%s,%s,%d)\n",
|
---|
105 | s1,
|
---|
106 | s2,
|
---|
107 | n));
|
---|
108 |
|
---|
109 | // @@@PH: sure it's not a UNICODE API?
|
---|
110 | return (lstrncmpiA(s1,s2,n));
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 | /*********************************************************************
|
---|
116 | * CRTDLL__strtime (CRTDLL.297)
|
---|
117 | */
|
---|
118 | char * CDECL CRTDLL__strtime( char *buf )
|
---|
119 | {
|
---|
120 | dprintf2(("CRTDLL: _strtime\n"));
|
---|
121 | return (_strtime(buf));
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /*********************************************************************
|
---|
126 | * _strupr (CRTDLL.298)
|
---|
127 | */
|
---|
128 | LPSTR CDECL CRTDLL__strupr(LPSTR x)
|
---|
129 | {
|
---|
130 | LPSTR y=x;
|
---|
131 |
|
---|
132 | dprintf2(("CRTDLL: _strupr(%s)\n",
|
---|
133 | x));
|
---|
134 |
|
---|
135 | while (*y)
|
---|
136 | {
|
---|
137 | *y=toupper(*y);
|
---|
138 | y++;
|
---|
139 | }
|
---|
140 | return x;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /*********************************************************************
|
---|
145 | * strcat (CRTDLL.459)
|
---|
146 | */
|
---|
147 | LPSTR CDECL CRTDLL_strcat( LPSTR str1,
|
---|
148 | const LPSTR str2)
|
---|
149 | {
|
---|
150 | dprintf2(("CRTDLL: strcat\n"));
|
---|
151 |
|
---|
152 | return (strcat(str1, str2));
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /*********************************************************************
|
---|
157 | * strchr (CRTDLL.460)
|
---|
158 | */
|
---|
159 | LPSTR CDECL CRTDLL_strchr(const LPSTR str,
|
---|
160 | int i)
|
---|
161 | {
|
---|
162 | dprintf2(("CRTDLL: strchr(%s,%08xh)\n",
|
---|
163 | str,
|
---|
164 | i));
|
---|
165 |
|
---|
166 | return (strchr(str, i));
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /*********************************************************************
|
---|
171 | * strcmp (CRTDLL.461)
|
---|
172 | */
|
---|
173 | int CDECL CRTDLL_strcmp(const LPSTR str1,
|
---|
174 | const LPSTR str2)
|
---|
175 | {
|
---|
176 | dprintf2(("CRTDLL: strcmp(%s,%s)\n",
|
---|
177 | str1,
|
---|
178 | str2));
|
---|
179 |
|
---|
180 | return (strcmp(str1, str2));
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /*********************************************************************
|
---|
185 | * strcoll (CRTDLL.462)
|
---|
186 | */
|
---|
187 | int CDECL CRTDLL_strcoll( const char *s1, const char *s2 )
|
---|
188 | {
|
---|
189 | dprintf2(("CRTDLL: strcoll\n"));
|
---|
190 | return strcoll(s1, s2);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /*********************************************************************
|
---|
195 | * strcpy (CRTDLL.463)
|
---|
196 | */
|
---|
197 | LPSTR CDECL CRTDLL_strcpy( LPSTR str1,
|
---|
198 | const LPSTR str2)
|
---|
199 | {
|
---|
200 | dprintf2(("CRTDLL: strcpy\n"));
|
---|
201 |
|
---|
202 | return (strcpy(str1, str2));
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /*********************************************************************
|
---|
207 | * strcspn (CRTDLL.464)
|
---|
208 | */
|
---|
209 | size_t CDECL CRTDLL_strcspn(const LPSTR str1,
|
---|
210 | LPSTR str2)
|
---|
211 | {
|
---|
212 | dprintf2(("CRTDLL: strcspn(%s,%s)\n",
|
---|
213 | str1,
|
---|
214 | str2));
|
---|
215 |
|
---|
216 | return (strcspn(str1, str2));
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /*********************************************************************
|
---|
221 | * strftime (CRTDLL.466)
|
---|
222 | */
|
---|
223 | size_t CDECL CRTDLL_strftime( char *s, size_t maxsiz, const char *fmt, const struct tm *tp )
|
---|
224 | {
|
---|
225 | dprintf2(("CRTDLL: strftime\n"));
|
---|
226 | return strftime(s, maxsiz, fmt, tp);
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /*********************************************************************
|
---|
231 | * strlen (CRTDLL.467)
|
---|
232 | */
|
---|
233 | size_t CDECL CRTDLL_strlen(const LPSTR str)
|
---|
234 | {
|
---|
235 | dprintf2(("CRTDLL: strlen(%s)\n",
|
---|
236 | str));
|
---|
237 |
|
---|
238 | return (strlen(str));
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | /*********************************************************************
|
---|
243 | * strncat (CRTDLL.468)
|
---|
244 | */
|
---|
245 | LPSTR CDECL CRTDLL_strncat( LPSTR str1,
|
---|
246 | const LPSTR str2,
|
---|
247 | size_t i)
|
---|
248 | {
|
---|
249 | dprintf2(("CRTDLL: strncat(%s,%s,%08xh)\n",
|
---|
250 | str1,
|
---|
251 | str2,
|
---|
252 | i));
|
---|
253 |
|
---|
254 | return (strncat(str1, str2, i));
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /*********************************************************************
|
---|
259 | * strncmp (CRTDLL.469)
|
---|
260 | */
|
---|
261 | int CDECL CRTDLL_strncmp(const LPSTR str1,
|
---|
262 | const LPSTR str2,
|
---|
263 | size_t i)
|
---|
264 | {
|
---|
265 | dprintf2(("CRTDLL: strncmp(%s,%s,%08xh)\n",
|
---|
266 | str1,
|
---|
267 | str2,
|
---|
268 | i));
|
---|
269 |
|
---|
270 | return (strncmp(str1, str2, i));
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | /*********************************************************************
|
---|
275 | * strncpy (CRTDLL.470)
|
---|
276 | */
|
---|
277 | LPSTR CDECL CRTDLL_strncpy(const LPSTR str1,
|
---|
278 | const LPSTR str2,
|
---|
279 | size_t i)
|
---|
280 | {
|
---|
281 | dprintf2(("CRTDLL: strncpy(%s,%s,%08xh)\n",
|
---|
282 | str1,
|
---|
283 | str2,
|
---|
284 | i));
|
---|
285 |
|
---|
286 | return (strncpy(str1, str2, i));
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | /*********************************************************************
|
---|
291 | * strpbrk (CRTDLL.471)
|
---|
292 | */
|
---|
293 | LPSTR CDECL CRTDLL_strpbrk(const LPSTR str1,
|
---|
294 | const LPSTR str2)
|
---|
295 | {
|
---|
296 | dprintf2(("CRTDLL: strpbrk(%s,%s)\n",
|
---|
297 | str1,
|
---|
298 | str2));
|
---|
299 |
|
---|
300 | return (strpbrk(str1, str2));
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /*********************************************************************
|
---|
305 | * strrchr (CRTDLL.472)
|
---|
306 | */
|
---|
307 | LPSTR CDECL CRTDLL_strrchr(const LPSTR str,
|
---|
308 | size_t i)
|
---|
309 | {
|
---|
310 | dprintf2(("CRTDLL: strrchr(%s,%08xh)\n",
|
---|
311 | str,
|
---|
312 | i));
|
---|
313 |
|
---|
314 | return (strrchr(str, i));
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | /*********************************************************************
|
---|
319 | * strspn (CRTDLL.473)
|
---|
320 | */
|
---|
321 | size_t CDECL CRTDLL_strspn(const LPSTR str1,
|
---|
322 | const LPSTR str2)
|
---|
323 | {
|
---|
324 | dprintf2(("CRTDLL: strspn(%s,%s)\n",
|
---|
325 | str1,
|
---|
326 | str2));
|
---|
327 |
|
---|
328 | return (strspn(str1, str2));
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | /*********************************************************************
|
---|
333 | * strstr (CRTDLL.474)
|
---|
334 | */
|
---|
335 | LPSTR CDECL CRTDLL_strstr(const LPSTR str1,
|
---|
336 | const LPSTR str2)
|
---|
337 | {
|
---|
338 | dprintf2(("CRTDLL: strstr(%s,%s)\n",
|
---|
339 | str1,
|
---|
340 | str2));
|
---|
341 |
|
---|
342 | return (strstr(str1, str2));
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /*********************************************************************
|
---|
347 | * strtod (CRTDLL.475)
|
---|
348 | */
|
---|
349 | double CDECL CRTDLL_strtod( const char *nptr, char **endptr )
|
---|
350 | {
|
---|
351 | dprintf2(("CRTDLL: strtod\n"));
|
---|
352 | return strtod(nptr, endptr);
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | /*********************************************************************
|
---|
357 | * strtok (CRTDLL.476)
|
---|
358 | */
|
---|
359 | char * CDECL CRTDLL_strtok( char *s1, const char *s2 )
|
---|
360 | {
|
---|
361 | dprintf2(("CRTDLL: strtok\n"));
|
---|
362 | return strtok(s1, s2);
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | /*********************************************************************
|
---|
367 | * strtol (CRTDLL.477)
|
---|
368 | */
|
---|
369 | long int CDECL CRTDLL_strtol( const char *nptr, char **endptr, int base )
|
---|
370 | {
|
---|
371 | dprintf2(("CRTDLL: strtol\n"));
|
---|
372 | return strtol(nptr, endptr, base);
|
---|
373 | }
|
---|
374 |
|
---|
375 |
|
---|
376 | /*********************************************************************
|
---|
377 | * strtoul (CRTDLL.478)
|
---|
378 | */
|
---|
379 | unsigned long CDECL CRTDLL_strtoul( const char *nptr, char **endptr, int base )
|
---|
380 | {
|
---|
381 | dprintf2(("CRTDLL: strtoul\n"));
|
---|
382 | return strtoul(nptr, endptr, base);
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | /*********************************************************************
|
---|
387 | * strxfrm (CRTDLL.479)
|
---|
388 | */
|
---|
389 | size_t CDECL CRTDLL_strxfrm( char *s1, const char *s2, size_t n )
|
---|
390 | {
|
---|
391 | dprintf2(("CRTDLL: strxfrm\n"));
|
---|
392 | return strxfrm(s1, s2, n);
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | /* INTERNAL: CRTDLL_malloc() based strndup */
|
---|
397 | LPSTR __CRTDLL__strndup(LPSTR buf, INT size)
|
---|
398 | {
|
---|
399 | char* ret;
|
---|
400 | int len = strlen(buf);
|
---|
401 | int max_len;
|
---|
402 |
|
---|
403 | max_len = size <= len? size : len + 1;
|
---|
404 |
|
---|
405 | ret = (char*)CRTDLL_malloc(max_len);
|
---|
406 | if (ret)
|
---|
407 | {
|
---|
408 | memcpy(ret,buf,max_len);
|
---|
409 | ret[max_len] = 0;
|
---|
410 | }
|
---|
411 | return ret;
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 | /*********************************************************************
|
---|
416 | * _strdec (CRTDLL.282)
|
---|
417 | *
|
---|
418 | * Return the byte before str2 while it is >= to str1.
|
---|
419 | *
|
---|
420 | * PARAMS
|
---|
421 | * str1 [in] Terminating string
|
---|
422 | *
|
---|
423 | * sre2 [in] string to start searching from
|
---|
424 | *
|
---|
425 | * RETURNS
|
---|
426 | * The byte before str2, or str1, whichever is greater
|
---|
427 | *
|
---|
428 | * NOTES
|
---|
429 | * This function is implemented as tested with windows, which means
|
---|
430 | * it does not have a terminating condition. It always returns
|
---|
431 | * the byte before str2. Use with extreme caution!
|
---|
432 | */
|
---|
433 | LPSTR CDECL CRTDLL__strdec(LPSTR str1, LPSTR str2)
|
---|
434 | {
|
---|
435 | /* Hmm. While the docs suggest that the following should work... */
|
---|
436 | /* return (str2<=str1?0:str2-1); */
|
---|
437 | /* ...Version 2.50.4170 (NT) from win98 constantly decrements! */
|
---|
438 | return str2-1;
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | /*********************************************************************
|
---|
443 | * _strdup (CRTDLL.285)
|
---|
444 | *
|
---|
445 | * Duplicate a string.
|
---|
446 | */
|
---|
447 | LPSTR CDECL CRTDLL__strdup(LPCSTR ptr)
|
---|
448 | {
|
---|
449 | LPSTR ret = (LPSTR)CRTDLL_malloc(strlen(ptr)+1);
|
---|
450 | if (ret) strcpy( ret, ptr );
|
---|
451 | return ret;
|
---|
452 | }
|
---|
453 |
|
---|
454 |
|
---|
455 | /*********************************************************************
|
---|
456 | * _strinc (CRTDLL.287)
|
---|
457 | *
|
---|
458 | * Return a pointer to the next character in a string
|
---|
459 | */
|
---|
460 | LPSTR CDECL CRTDLL__strinc(LPSTR str)
|
---|
461 | {
|
---|
462 | return str+1;
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 | /*********************************************************************
|
---|
467 | * _strnextc (CRTDLL.290)
|
---|
468 | *
|
---|
469 | * Return an unsigned int from a string.
|
---|
470 | */
|
---|
471 | UINT CDECL CRTDLL__strnextc(LPCSTR str)
|
---|
472 | {
|
---|
473 | return (UINT)*str;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | /*********************************************************************
|
---|
478 | * _strninc (CRTDLL.292)
|
---|
479 | *
|
---|
480 | * Return a pointer to the 'n'th character in a string
|
---|
481 | */
|
---|
482 | LPSTR CDECL CRTDLL__strninc(LPSTR str, INT n)
|
---|
483 | {
|
---|
484 | return str+n;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | /*********************************************************************
|
---|
489 | * _strnset (CRTDLL.293)
|
---|
490 | *
|
---|
491 | * Fill a string with a character up to a certain length
|
---|
492 | */
|
---|
493 | LPSTR CDECL CRTDLL__strnset(LPSTR str, INT c, INT len)
|
---|
494 | {
|
---|
495 | if (len > 0 && str)
|
---|
496 | while (*str && len--)
|
---|
497 | *str++ = c;
|
---|
498 | return str;
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | /*********************************************************************
|
---|
503 | * _strrev (CRTDLL.294)
|
---|
504 | *
|
---|
505 | * Reverse a string in place
|
---|
506 | */
|
---|
507 | LPSTR CDECL CRTDLL__strrev (LPSTR str)
|
---|
508 | {
|
---|
509 | LPSTR p1;
|
---|
510 | LPSTR p2;
|
---|
511 |
|
---|
512 | if (str && *str)
|
---|
513 | for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
|
---|
514 | {
|
---|
515 | *p1 ^= *p2;
|
---|
516 | *p2 ^= *p1;
|
---|
517 | *p1 ^= *p2;
|
---|
518 | }
|
---|
519 |
|
---|
520 | return str;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /*********************************************************************
|
---|
524 | * _strset (CRTDLL.295)
|
---|
525 | *
|
---|
526 | * Fill a string with a value.
|
---|
527 | */
|
---|
528 | LPSTR CDECL CRTDLL__strset (LPSTR str, INT set)
|
---|
529 | {
|
---|
530 | char *ptr = str;
|
---|
531 |
|
---|
532 | while (*ptr)
|
---|
533 | *ptr++ = set;
|
---|
534 |
|
---|
535 | return str;
|
---|
536 | }
|
---|
537 |
|
---|
538 |
|
---|
539 | /*********************************************************************
|
---|
540 | * _strncnt (CRTDLL.289)
|
---|
541 | *
|
---|
542 | * Return the length of a string or the maximum given length.
|
---|
543 | */
|
---|
544 | LONG CDECL CRTDLL__strncnt(LPSTR str, LONG max)
|
---|
545 | {
|
---|
546 | LONG len = strlen(str);
|
---|
547 | return (len > max? max : len);
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | /*********************************************************************
|
---|
552 | * _strspnp (CRTDLL.296)
|
---|
553 | *
|
---|
554 | */
|
---|
555 | LPSTR CDECL CRTDLL__strspnp(LPSTR str1, LPSTR str2)
|
---|
556 | {
|
---|
557 | str1 += strspn(str1,str2);
|
---|
558 | return *str1? str1 : 0;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | /*********************************************************************
|
---|
563 | * _swab (CRTDLL.299)
|
---|
564 | *
|
---|
565 | * Copy from source to dest alternating bytes (i.e 16 bit big-to-little
|
---|
566 | * endian or vice versa).
|
---|
567 | */
|
---|
568 | void CDECL CRTDLL__swab(LPSTR src, LPSTR dst, INT len)
|
---|
569 | {
|
---|
570 | //_swab(s1, s2, i);
|
---|
571 |
|
---|
572 | if (len > 1)
|
---|
573 | {
|
---|
574 | len = (unsigned)len >> 1;
|
---|
575 |
|
---|
576 | while (len--) {
|
---|
577 | *dst++ = src[1];
|
---|
578 | *dst++ = *src++;
|
---|
579 | src++;
|
---|
580 | }
|
---|
581 | }
|
---|
582 | }
|
---|