Update the address of the Free Software Foundation.
[wine.git] / dlls / ntdll / string.c
blob874ead3d96d95228019b35041ba54e959045cc74
1 /*
2 * NTDLL string functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 * Copyright 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <search.h>
33 #include "windef.h"
34 #include "winternl.h"
37 /*********************************************************************
38 * memchr (NTDLL.@)
40 void * __cdecl NTDLL_memchr( const void *ptr, int c, size_t n )
42 return memchr( ptr, c, n );
46 /*********************************************************************
47 * memcmp (NTDLL.@)
49 int __cdecl NTDLL_memcmp( const void *ptr1, const void *ptr2, size_t n )
51 return memcmp( ptr1, ptr2, n );
55 /*********************************************************************
56 * memcpy (NTDLL.@)
58 * NOTES
59 * Behaves like memmove.
61 void * __cdecl NTDLL_memcpy( void *dst, const void *src, size_t n )
63 return memmove( dst, src, n );
67 /*********************************************************************
68 * memmove (NTDLL.@)
70 void * __cdecl NTDLL_memmove( void *dst, const void *src, size_t n )
72 return memmove( dst, src, n );
76 /*********************************************************************
77 * memset (NTDLL.@)
79 void * __cdecl NTDLL_memset( void *dst, int c, size_t n )
81 return memset( dst, c, n );
85 /*********************************************************************
86 * bsearch (NTDLL.@)
88 void * __cdecl NTDLL_bsearch( const void *key, const void *base, size_t nmemb,
89 size_t size, int (*compar)(const void *, const void *) )
91 return bsearch( key, base, nmemb, size, compar );
95 /*********************************************************************
96 * qsort (NTDLL.@)
98 void __cdecl NTDLL_qsort( void *base, size_t nmemb, size_t size,
99 int(*compar)(const void *, const void *) )
101 return qsort( base, nmemb, size, compar );
105 /*********************************************************************
106 * _lfind (NTDLL.@)
108 void * __cdecl _lfind( const void *key, const void *base, size_t *nmemb,
109 size_t size, int(*compar)(const void *, const void *) )
111 return lfind( key, base, nmemb, size, compar );
115 /*********************************************************************
116 * strcat (NTDLL.@)
118 char * __cdecl NTDLL_strcat( char *dst, const char *src )
120 return strcat( dst, src );
124 /*********************************************************************
125 * strchr (NTDLL.@)
127 char * __cdecl NTDLL_strchr( const char *str, int c )
129 return strchr( str, c );
133 /*********************************************************************
134 * strcmp (NTDLL.@)
136 int __cdecl NTDLL_strcmp( const char *str1, const char *str2 )
138 return strcmp( str1, str2 );
142 /*********************************************************************
143 * strcpy (NTDLL.@)
145 char * __cdecl NTDLL_strcpy( char *dst, const char *src )
147 return strcpy( dst, src );
151 /*********************************************************************
152 * strcspn (NTDLL.@)
154 size_t __cdecl NTDLL_strcspn( const char *str, const char *reject )
156 return strcspn( str, reject );
160 /*********************************************************************
161 * strlen (NTDLL.@)
163 size_t __cdecl NTDLL_strlen( const char *str )
165 return strlen( str );
169 /*********************************************************************
170 * strncat (NTDLL.@)
172 char * __cdecl NTDLL_strncat( char *dst, const char *src, size_t len )
174 return strncat( dst, src, len );
178 /*********************************************************************
179 * strncmp (NTDLL.@)
181 int __cdecl NTDLL_strncmp( const char *str1, const char *str2, size_t len )
183 return strncmp( str1, str2, len );
187 /*********************************************************************
188 * strncpy (NTDLL.@)
190 char * __cdecl NTDLL_strncpy( char *dst, const char *src, size_t len )
192 return strncpy( dst, src, len );
196 /*********************************************************************
197 * strpbrk (NTDLL.@)
199 char * __cdecl NTDLL_strpbrk( const char *str, const char *accept )
201 return strpbrk( str, accept );
205 /*********************************************************************
206 * strrchr (NTDLL.@)
208 char * __cdecl NTDLL_strrchr( const char *str, int c )
210 return strrchr( str, c );
214 /*********************************************************************
215 * strspn (NTDLL.@)
217 size_t __cdecl NTDLL_strspn( const char *str, const char *accept )
219 return strspn( str, accept );
223 /*********************************************************************
224 * strstr (NTDLL.@)
226 char * __cdecl NTDLL_strstr( const char *haystack, const char *needle )
228 return strstr( haystack, needle );
232 /*********************************************************************
233 * _memccpy (NTDLL.@)
235 void * __cdecl _memccpy( void *dst, const void *src, int c, size_t n )
237 return memccpy( dst, src, c, n );
241 /*********************************************************************
242 * _memicmp (NTDLL.@)
244 * Compare two blocks of memory as strings, ignoring case.
246 * PARAMS
247 * s1 [I] First string to compare to s2
248 * s2 [I] Second string to compare to s1
249 * len [I] Number of bytes to compare
251 * RETURNS
252 * An integer less than, equal to, or greater than zero indicating that
253 * s1 is less than, equal to or greater than s2 respectively.
255 * NOTES
256 * Any Nul characters in s1 or s2 are ignored. This function always
257 * compares up to len bytes or the first place where s1 and s2 differ.
259 INT __cdecl _memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
261 int ret = 0;
262 while (len--)
264 if ((ret = tolower(*s1) - tolower(*s2))) break;
265 s1++;
266 s2++;
268 return ret;
272 /*********************************************************************
273 * _stricmp (NTDLL.@)
274 * _strcmpi (NTDLL.@)
276 int __cdecl _stricmp( LPCSTR str1, LPCSTR str2 )
278 return strcasecmp( str1, str2 );
282 /*********************************************************************
283 * _strnicmp (NTDLL.@)
285 int __cdecl _strnicmp( LPCSTR str1, LPCSTR str2, size_t n )
287 return strncasecmp( str1, str2, n );
291 /*********************************************************************
292 * _strupr (NTDLL.@)
294 * Convert a string to upper case.
296 * PARAMS
297 * str [I/O] String to convert
299 * RETURNS
300 * str. There is no error return, if str is NULL or invalid, this
301 * function will crash.
303 LPSTR __cdecl _strupr( LPSTR str )
305 LPSTR ret = str;
306 for ( ; *str; str++) *str = toupper(*str);
307 return ret;
311 /*********************************************************************
312 * _strlwr (NTDLL.@)
314 * Convert a string to lowercase
316 * PARAMS
317 * str [I/O] String to convert
319 * RETURNS
320 * str. There is no error return, if str is NULL or invalid, this
321 * function will crash.
323 LPSTR __cdecl _strlwr( LPSTR str )
325 LPSTR ret = str;
326 for ( ; *str; str++) *str = tolower(*str);
327 return ret;
331 /*********************************************************************
332 * tolower (NTDLL.@)
334 int __cdecl NTDLL_tolower( int c )
336 return tolower( c );
340 /*********************************************************************
341 * toupper (NTDLL.@)
343 int __cdecl NTDLL_toupper( int c )
345 return toupper( c );
349 /*********************************************************************
350 * isalnum (NTDLL.@)
352 int __cdecl NTDLL_isalnum( int c )
354 return isalnum( c );
358 /*********************************************************************
359 * isalpha (NTDLL.@)
361 int __cdecl NTDLL_isalpha( int c )
363 return isalpha( c );
367 /*********************************************************************
368 * iscntrl (NTDLL.@)
370 int __cdecl NTDLL_iscntrl( int c )
372 return iscntrl( c );
376 /*********************************************************************
377 * isdigit (NTDLL.@)
379 int __cdecl NTDLL_isdigit( int c )
381 return isdigit( c );
385 /*********************************************************************
386 * isgraph (NTDLL.@)
388 int __cdecl NTDLL_isgraph( int c )
390 return isgraph( c );
394 /*********************************************************************
395 * islower (NTDLL.@)
397 int __cdecl NTDLL_islower( int c )
399 return islower( c );
403 /*********************************************************************
404 * isprint (NTDLL.@)
406 int __cdecl NTDLL_isprint( int c )
408 return isprint( c );
412 /*********************************************************************
413 * ispunct (NTDLL.@)
415 int __cdecl NTDLL_ispunct( int c )
417 return ispunct( c );
421 /*********************************************************************
422 * isspace (NTDLL.@)
424 int __cdecl NTDLL_isspace( int c )
426 return isspace( c );
430 /*********************************************************************
431 * isupper (NTDLL.@)
433 int __cdecl NTDLL_isupper( int c )
435 return isupper( c );
439 /*********************************************************************
440 * isxdigit (NTDLL.@)
442 int __cdecl NTDLL_isxdigit( int c )
444 return isxdigit( c );
448 /*********************************************************************
449 * strtol (NTDLL.@)
451 long __cdecl NTDLL_strtol( const char *nptr, char **endptr, int base )
453 return strtol( nptr, endptr, base );
457 /*********************************************************************
458 * strtoul (NTDLL.@)
460 unsigned long __cdecl NTDLL_strtoul( const char *nptr, char **endptr, int base )
462 return strtoul( nptr, endptr, base );
466 /*********************************************************************
467 * atoi (NTDLL.@)
469 int __cdecl NTDLL_atoi( const char *nptr )
471 return atoi( nptr );
475 /*********************************************************************
476 * atol (NTDLL.@)
478 long __cdecl NTDLL_atol( const char *nptr )
480 return atol( nptr );
484 /*********************************************************************
485 * _ultoa (NTDLL.@)
487 * Convert an unsigned long integer to a string.
489 * RETURNS
490 * str.
492 * NOTES
493 * - Converts value to a Nul terminated string which is copied to str.
494 * - The maximum length of the copied str is 33 bytes.
495 * - Does not check if radix is in the range of 2 to 36.
496 * - If str is NULL it crashes, as the native function does.
498 char * __cdecl _ultoa(
499 unsigned long value, /* [I] Value to be converted */
500 char *str, /* [O] Destination for the converted value */
501 int radix) /* [I] Number base for conversion */
503 char buffer[33];
504 char *pos;
505 int digit;
507 pos = &buffer[32];
508 *pos = '\0';
510 do {
511 digit = value % radix;
512 value = value / radix;
513 if (digit < 10) {
514 *--pos = '0' + digit;
515 } else {
516 *--pos = 'a' + digit - 10;
517 } /* if */
518 } while (value != 0L);
520 memcpy(str, pos, &buffer[32] - pos + 1);
521 return str;
525 /*********************************************************************
526 * _ltoa (NTDLL.@)
528 * Convert a long integer to a string.
530 * RETURNS
531 * str.
533 * NOTES
534 * - Converts value to a Nul terminated string which is copied to str.
535 * - The maximum length of the copied str is 33 bytes. If radix
536 * is 10 and value is negative, the value is converted with sign.
537 * - Does not check if radix is in the range of 2 to 36.
538 * - If str is NULL it crashes, as the native function does.
540 char * __cdecl _ltoa(
541 long value, /* [I] Value to be converted */
542 char *str, /* [O] Destination for the converted value */
543 int radix) /* [I] Number base for conversion */
545 unsigned long val;
546 int negative;
547 char buffer[33];
548 char *pos;
549 int digit;
551 if (value < 0 && radix == 10) {
552 negative = 1;
553 val = -value;
554 } else {
555 negative = 0;
556 val = value;
557 } /* if */
559 pos = &buffer[32];
560 *pos = '\0';
562 do {
563 digit = val % radix;
564 val = val / radix;
565 if (digit < 10) {
566 *--pos = '0' + digit;
567 } else {
568 *--pos = 'a' + digit - 10;
569 } /* if */
570 } while (val != 0L);
572 if (negative) {
573 *--pos = '-';
574 } /* if */
576 memcpy(str, pos, &buffer[32] - pos + 1);
577 return str;
581 /*********************************************************************
582 * _itoa (NTDLL.@)
584 * Converts an integer to a string.
586 * RETURNS
587 * str.
589 * NOTES
590 * - Converts value to a '\0' terminated string which is copied to str.
591 * - The maximum length of the copied str is 33 bytes. If radix
592 * is 10 and value is negative, the value is converted with sign.
593 * - Does not check if radix is in the range of 2 to 36.
594 * - If str is NULL it crashes, as the native function does.
596 char * __cdecl _itoa(
597 int value, /* [I] Value to be converted */
598 char *str, /* [O] Destination for the converted value */
599 int radix) /* [I] Number base for conversion */
601 return _ltoa(value, str, radix);
605 /*********************************************************************
606 * _ui64toa (NTDLL.@)
608 * Converts a large unsigned integer to a string.
610 * RETURNS
611 * str.
613 * NOTES
614 * - Converts value to a '\0' terminated string which is copied to str.
615 * - The maximum length of the copied str is 65 bytes.
616 * - Does not check if radix is in the range of 2 to 36.
617 * - If str is NULL it crashes, as the native function does.
619 char * __cdecl _ui64toa(
620 ULONGLONG value, /* [I] Value to be converted */
621 char *str, /* [O] Destination for the converted value */
622 int radix) /* [I] Number base for conversion */
624 char buffer[65];
625 char *pos;
626 int digit;
628 pos = &buffer[64];
629 *pos = '\0';
631 do {
632 digit = value % radix;
633 value = value / radix;
634 if (digit < 10) {
635 *--pos = '0' + digit;
636 } else {
637 *--pos = 'a' + digit - 10;
638 } /* if */
639 } while (value != 0L);
641 memcpy(str, pos, &buffer[64] - pos + 1);
642 return str;
646 /*********************************************************************
647 * _i64toa (NTDLL.@)
649 * Converts a large integer to a string.
651 * RETURNS
652 * str.
654 * NOTES
655 * - Converts value to a Nul terminated string which is copied to str.
656 * - The maximum length of the copied str is 65 bytes. If radix
657 * is 10 and value is negative, the value is converted with sign.
658 * - Does not check if radix is in the range of 2 to 36.
659 * - If str is NULL it crashes, as the native function does.
661 * DIFFERENCES
662 * - The native DLL converts negative values (for base 10) wrong:
663 *| -1 is converted to -18446744073709551615
664 *| -2 is converted to -18446744073709551614
665 *| -9223372036854775807 is converted to -9223372036854775809
666 *| -9223372036854775808 is converted to -9223372036854775808
667 * The native msvcrt _i64toa function and our ntdll _i64toa function
668 * do not have this bug.
670 char * __cdecl _i64toa(
671 LONGLONG value, /* [I] Value to be converted */
672 char *str, /* [O] Destination for the converted value */
673 int radix) /* [I] Number base for conversion */
675 ULONGLONG val;
676 int negative;
677 char buffer[65];
678 char *pos;
679 int digit;
681 if (value < 0 && radix == 10) {
682 negative = 1;
683 val = -value;
684 } else {
685 negative = 0;
686 val = value;
687 } /* if */
689 pos = &buffer[64];
690 *pos = '\0';
692 do {
693 digit = val % radix;
694 val = val / radix;
695 if (digit < 10) {
696 *--pos = '0' + digit;
697 } else {
698 *--pos = 'a' + digit - 10;
699 } /* if */
700 } while (val != 0L);
702 if (negative) {
703 *--pos = '-';
704 } /* if */
706 memcpy(str, pos, &buffer[64] - pos + 1);
707 return str;
711 /*********************************************************************
712 * _atoi64 (NTDLL.@)
714 * Convert a string to a large integer.
716 * PARAMS
717 * str [I] String to be converted
719 * RETURNS
720 * Success: The integer value represented by str.
721 * Failure: 0. Note that this cannot be distinguished from a successful
722 * return, if the string contains "0".
724 * NOTES
725 * - Accepts: {whitespace} [+|-] {digits}
726 * - No check is made for value overflow, only the lower 64 bits are assigned.
727 * - If str is NULL it crashes, as the native function does.
729 LONGLONG __cdecl _atoi64( char *str )
731 ULONGLONG RunningTotal = 0;
732 char bMinus = 0;
734 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
735 str++;
736 } /* while */
738 if (*str == '+') {
739 str++;
740 } else if (*str == '-') {
741 bMinus = 1;
742 str++;
743 } /* if */
745 while (*str >= '0' && *str <= '9') {
746 RunningTotal = RunningTotal * 10 + *str - '0';
747 str++;
748 } /* while */
750 return bMinus ? -RunningTotal : RunningTotal;
754 /*********************************************************************
755 * sprintf (NTDLL.@)
757 int __cdecl NTDLL_sprintf( char *str, const char *format, ... )
759 int ret;
760 va_list valist;
761 va_start( valist, format );
762 ret = vsprintf( str, format, valist );
763 va_end( valist );
764 return ret;
768 /*********************************************************************
769 * vsprintf (NTDLL.@)
771 int __cdecl NTDLL_vsprintf( char *str, const char *format, va_list args )
773 return vsprintf( str, format, args );
777 /*********************************************************************
778 * _snprintf (NTDLL.@)
780 int __cdecl _snprintf( char *str, size_t len, const char *format, ... )
782 int ret;
783 va_list valist;
784 va_start( valist, format );
785 ret = vsnprintf( str, len, format, valist );
786 va_end( valist );
787 return ret;
791 /*********************************************************************
792 * _vsnprintf (NTDLL.@)
794 int __cdecl _vsnprintf( char *str, size_t len, const char *format, va_list args )
796 return vsnprintf( str, len, format, args );
800 /*********************************************************************
801 * sscanf (NTDLL.@)
803 int __cdecl NTDLL_sscanf( const char *str, const char *format, ... )
805 int ret;
806 va_list valist;
807 va_start( valist, format );
808 ret = vsscanf( str, format, valist );
809 va_end( valist );
810 return ret;
814 /*********************************************************************
815 * _splitpath (NTDLL.@)
817 * Split a path into its component pieces.
819 * PARAMS
820 * inpath [I] Path to split
821 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
822 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
823 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
824 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
826 * RETURNS
827 * Nothing.
829 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
830 char* fname, char * ext )
832 const char *p, *end;
834 if (inpath[0] && inpath[1] == ':')
836 if (drv)
838 drv[0] = inpath[0];
839 drv[1] = inpath[1];
840 drv[2] = 0;
842 inpath += 2;
844 else if (drv) drv[0] = 0;
846 /* look for end of directory part */
847 end = NULL;
848 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
850 if (end) /* got a directory */
852 if (dir)
854 memcpy( dir, inpath, end - inpath );
855 dir[end - inpath] = 0;
857 inpath = end;
859 else if (dir) dir[0] = 0;
861 /* look for extension: what's after the last dot */
862 end = NULL;
863 for (p = inpath; *p; p++) if (*p == '.') end = p;
865 if (!end) end = p; /* there's no extension */
867 if (fname)
869 memcpy( fname, inpath, end - inpath );
870 fname[end - inpath] = 0;
872 if (ext) strcpy( ext, end );