1 | /* $Id: crt_string.cpp,v 1.3 2000-11-21 23:48:49 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 |
|
---|
28 |
|
---|
29 | DEFAULT_DEBUG_CHANNEL(crtdll);
|
---|
30 |
|
---|
31 | /*********************************************************************
|
---|
32 | * _strcmpi (CRTDLL.280)
|
---|
33 | */
|
---|
34 | void CDECL CRTDLL__strcmpi( LPCSTR s1, LPCSTR s2 )
|
---|
35 | {
|
---|
36 | dprintf2(("CRTDLL: _strcmpi(%08xh, %08xh)\n",
|
---|
37 | s1,
|
---|
38 | s2));
|
---|
39 |
|
---|
40 | lstrcmpiA( s1, s2 );
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************
|
---|
45 | * CRTDLL__strdate (CRTDLL.281)
|
---|
46 | */
|
---|
47 | char * CDECL CRTDLL__strdate( char *buf )
|
---|
48 | {
|
---|
49 | dprintf2(("CRTDLL: _strdate\n"));
|
---|
50 | return(_strdate(buf));
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************
|
---|
55 | * _stricmp (CRTDLL.285)
|
---|
56 | */
|
---|
57 | int CDECL CRTDLL__stricmp(const LPSTR str1,
|
---|
58 | const LPSTR str2)
|
---|
59 | {
|
---|
60 | dprintf2(("CRTDLL: _stricmp(%s,%s)\n",
|
---|
61 | str1,
|
---|
62 | str2));
|
---|
63 |
|
---|
64 | return (stricmp(str1, str2));
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | /*********************************************************************
|
---|
70 | * CRTDLL__stricoll (CRTDLL.286)
|
---|
71 | */
|
---|
72 | int CDECL CRTDLL__stricoll( const char *s1, const char *s2 )
|
---|
73 | {
|
---|
74 | dprintf2(("CRTDLL: _stricoll\n"));
|
---|
75 | return stricmp(s1,s2);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /*********************************************************************
|
---|
80 | * _strlwr (CRTDLL.288)
|
---|
81 | */
|
---|
82 | CHAR * CDECL CRTDLL__strlwr(char *x)
|
---|
83 | {
|
---|
84 | char *y =x;
|
---|
85 |
|
---|
86 | dprintf2(("CRTDLL: _strlwr got %s\n", x));
|
---|
87 | while (*y) {
|
---|
88 | if ((*y > 0x40) && (*y< 0x5b))
|
---|
89 | *y = *y + 0x20;
|
---|
90 | y++;
|
---|
91 | }
|
---|
92 | dprintf2((" returned %s\n", x));
|
---|
93 |
|
---|
94 | return x;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /*********************************************************************
|
---|
99 | * CRTDLL__strnicmp (CRTDLL.291)
|
---|
100 | */
|
---|
101 | int CDECL CRTDLL__strnicmp( LPCSTR s1, LPCSTR s2, INT n )
|
---|
102 | {
|
---|
103 | dprintf2(("CRTDLL: _strnicmp (%s,%s,%d)\n",
|
---|
104 | s1,
|
---|
105 | s2,
|
---|
106 | n));
|
---|
107 |
|
---|
108 | // @@@PH: sure it's not a UNICODE API?
|
---|
109 | return (lstrncmpiA(s1,s2,n));
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /*********************************************************************
|
---|
115 | * CRTDLL__strtime (CRTDLL.297)
|
---|
116 | */
|
---|
117 | char * CDECL CRTDLL__strtime( char *buf )
|
---|
118 | {
|
---|
119 | dprintf2(("CRTDLL: _strtime\n"));
|
---|
120 | return (_strtime(buf));
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /*********************************************************************
|
---|
125 | * _strupr (CRTDLL.298)
|
---|
126 | */
|
---|
127 | LPSTR CDECL CRTDLL__strupr(LPSTR x)
|
---|
128 | {
|
---|
129 | dprintf2(("CRTDLL: _strupr(%s)\n",
|
---|
130 | x));
|
---|
131 |
|
---|
132 | LPSTR y=x;
|
---|
133 |
|
---|
134 | while (*y)
|
---|
135 | {
|
---|
136 | *y=toupper(*y);
|
---|
137 | y++;
|
---|
138 | }
|
---|
139 | return x;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /*********************************************************************
|
---|
144 | * strcat (CRTDLL.459)
|
---|
145 | */
|
---|
146 | LPSTR CDECL CRTDLL_strcat( LPSTR str1,
|
---|
147 | const LPSTR str2)
|
---|
148 | {
|
---|
149 | dprintf2(("CRTDLL: strcat\n"));
|
---|
150 |
|
---|
151 | return (strcat(str1, str2));
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /*********************************************************************
|
---|
156 | * strchr (CRTDLL.460)
|
---|
157 | */
|
---|
158 | LPSTR CDECL CRTDLL_strchr(const LPSTR str,
|
---|
159 | int i)
|
---|
160 | {
|
---|
161 | dprintf2(("CRTDLL: strchr(%s,%08xh)\n",
|
---|
162 | str,
|
---|
163 | i));
|
---|
164 |
|
---|
165 | return (strchr(str, i));
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /*********************************************************************
|
---|
170 | * strcmp (CRTDLL.461)
|
---|
171 | */
|
---|
172 | int CDECL CRTDLL_strcmp(const LPSTR str1,
|
---|
173 | const LPSTR str2)
|
---|
174 | {
|
---|
175 | dprintf2(("CRTDLL: strcmp(%s,%s)\n",
|
---|
176 | str1,
|
---|
177 | str2));
|
---|
178 |
|
---|
179 | return (strcmp(str1, str2));
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /*********************************************************************
|
---|
184 | * strcoll (CRTDLL.462)
|
---|
185 | */
|
---|
186 | int CDECL CRTDLL_strcoll( const char *s1, const char *s2 )
|
---|
187 | {
|
---|
188 | dprintf2(("CRTDLL: strcoll\n"));
|
---|
189 | return strcoll(s1, s2);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /*********************************************************************
|
---|
194 | * strcpy (CRTDLL.463)
|
---|
195 | */
|
---|
196 | LPSTR CDECL CRTDLL_strcpy( LPSTR str1,
|
---|
197 | const LPSTR str2)
|
---|
198 | {
|
---|
199 | dprintf2(("CRTDLL: strcpy\n"));
|
---|
200 |
|
---|
201 | return (strcpy(str1, str2));
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | /*********************************************************************
|
---|
206 | * strcspn (CRTDLL.464)
|
---|
207 | */
|
---|
208 | size_t CDECL CRTDLL_strcspn(const LPSTR str1,
|
---|
209 | LPSTR str2)
|
---|
210 | {
|
---|
211 | dprintf2(("CRTDLL: strcspn(%s,%s)\n",
|
---|
212 | str1,
|
---|
213 | str2));
|
---|
214 |
|
---|
215 | return (strcspn(str1, str2));
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /*********************************************************************
|
---|
220 | * strftime (CRTDLL.466)
|
---|
221 | */
|
---|
222 | size_t CDECL CRTDLL_strftime( char *s, size_t maxsiz, const char *fmt, const struct tm *tp )
|
---|
223 | {
|
---|
224 | dprintf2(("CRTDLL: strftime\n"));
|
---|
225 | return strftime(s, maxsiz, fmt, tp);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /*********************************************************************
|
---|
230 | * strlen (CRTDLL.467)
|
---|
231 | */
|
---|
232 | size_t CDECL CRTDLL_strlen(const LPSTR str)
|
---|
233 | {
|
---|
234 | dprintf2(("CRTDLL: strlen(%s)\n",
|
---|
235 | str));
|
---|
236 |
|
---|
237 | return (strlen(str));
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /*********************************************************************
|
---|
242 | * strncat (CRTDLL.468)
|
---|
243 | */
|
---|
244 | LPSTR CDECL CRTDLL_strncat( LPSTR str1,
|
---|
245 | const LPSTR str2,
|
---|
246 | size_t i)
|
---|
247 | {
|
---|
248 | dprintf2(("CRTDLL: strncat(%s,%s,%08xh)\n",
|
---|
249 | str1,
|
---|
250 | str2,
|
---|
251 | i));
|
---|
252 |
|
---|
253 | return (strncat(str1, str2, i));
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /*********************************************************************
|
---|
258 | * strncmp (CRTDLL.469)
|
---|
259 | */
|
---|
260 | int CDECL CRTDLL_strncmp(const LPSTR str1,
|
---|
261 | const LPSTR str2,
|
---|
262 | size_t i)
|
---|
263 | {
|
---|
264 | dprintf2(("CRTDLL: strncmp(%s,%s,%08xh)\n",
|
---|
265 | str1,
|
---|
266 | str2,
|
---|
267 | i));
|
---|
268 |
|
---|
269 | return (strncmp(str1, str2, i));
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /*********************************************************************
|
---|
274 | * strncpy (CRTDLL.470)
|
---|
275 | */
|
---|
276 | LPSTR CDECL CRTDLL_strncpy(const LPSTR str1,
|
---|
277 | const LPSTR str2,
|
---|
278 | size_t i)
|
---|
279 | {
|
---|
280 | dprintf2(("CRTDLL: strncpy(%s,%s,%08xh)\n",
|
---|
281 | str1,
|
---|
282 | str2,
|
---|
283 | i));
|
---|
284 |
|
---|
285 | return (strncpy(str1, str2, i));
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | /*********************************************************************
|
---|
290 | * strpbrk (CRTDLL.471)
|
---|
291 | */
|
---|
292 | LPSTR CDECL CRTDLL_strpbrk(const LPSTR str1,
|
---|
293 | const LPSTR str2)
|
---|
294 | {
|
---|
295 | dprintf2(("CRTDLL: strpbrk(%s,%s)\n",
|
---|
296 | str1,
|
---|
297 | str2));
|
---|
298 |
|
---|
299 | return (strpbrk(str1, str2));
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /*********************************************************************
|
---|
304 | * strrchr (CRTDLL.472)
|
---|
305 | */
|
---|
306 | LPSTR CDECL CRTDLL_strrchr(const LPSTR str,
|
---|
307 | size_t i)
|
---|
308 | {
|
---|
309 | dprintf2(("CRTDLL: strrchr(%s,%08xh)\n",
|
---|
310 | str,
|
---|
311 | i));
|
---|
312 |
|
---|
313 | return (strrchr(str, i));
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /*********************************************************************
|
---|
318 | * strspn (CRTDLL.473)
|
---|
319 | */
|
---|
320 | size_t CDECL CRTDLL_strspn(const LPSTR str1,
|
---|
321 | const LPSTR str2)
|
---|
322 | {
|
---|
323 | dprintf2(("CRTDLL: strspn(%s,%s)\n",
|
---|
324 | str1,
|
---|
325 | str2));
|
---|
326 |
|
---|
327 | return (strspn(str1, str2));
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | /*********************************************************************
|
---|
332 | * strstr (CRTDLL.474)
|
---|
333 | */
|
---|
334 | LPSTR CDECL CRTDLL_strstr(const LPSTR str1,
|
---|
335 | const LPSTR str2)
|
---|
336 | {
|
---|
337 | dprintf2(("CRTDLL: strstr(%s,%s)\n",
|
---|
338 | str1,
|
---|
339 | str2));
|
---|
340 |
|
---|
341 | return (strstr(str1, str2));
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | /*********************************************************************
|
---|
346 | * strtod (CRTDLL.475)
|
---|
347 | */
|
---|
348 | double CDECL CRTDLL_strtod( const char *nptr, char **endptr )
|
---|
349 | {
|
---|
350 | dprintf2(("CRTDLL: strtod\n"));
|
---|
351 | return strtod(nptr, endptr);
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | /*********************************************************************
|
---|
356 | * strtok (CRTDLL.476)
|
---|
357 | */
|
---|
358 | char * CDECL CRTDLL_strtok( char *s1, const char *s2 )
|
---|
359 | {
|
---|
360 | dprintf2(("CRTDLL: strtok\n"));
|
---|
361 | return strtok(s1, s2);
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | /*********************************************************************
|
---|
366 | * strtol (CRTDLL.477)
|
---|
367 | */
|
---|
368 | long int CDECL CRTDLL_strtol( const char *nptr, char **endptr, int base )
|
---|
369 | {
|
---|
370 | dprintf2(("CRTDLL: strtol\n"));
|
---|
371 | return strtol(nptr, endptr, base);
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /*********************************************************************
|
---|
376 | * strtoul (CRTDLL.478)
|
---|
377 | */
|
---|
378 | unsigned long CDECL CRTDLL_strtoul( const char *nptr, char **endptr, int base )
|
---|
379 | {
|
---|
380 | dprintf2(("CRTDLL: strtoul\n"));
|
---|
381 | return strtoul(nptr, endptr, base);
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | /*********************************************************************
|
---|
386 | * strxfrm (CRTDLL.479)
|
---|
387 | */
|
---|
388 | size_t CDECL CRTDLL_strxfrm( char *s1, const char *s2, size_t n )
|
---|
389 | {
|
---|
390 | dprintf2(("CRTDLL: strxfrm\n"));
|
---|
391 | return strxfrm(s1, s2, n);
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | /* INTERNAL: CRTDLL_malloc() based strndup */
|
---|
396 | LPSTR __CRTDLL__strndup(LPSTR buf, INT size);
|
---|
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 | }
|
---|