shell32: Always clone the return pidl to avoid a double free if the selection is...
[wine] / dlls / kernel32 / string.c
CommitLineData
fc99ddd9
DT
1/*
2 * Kernel string functions
0799c1a7 3 *
65e7196f
AJ
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
ae500130 6 * Copyright 2001 Dmitry Timoshkov for CodeWeavers
0799c1a7
AJ
7 *
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.
12 *
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.
17 *
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
360a3f91 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
fc99ddd9
DT
21 */
22
402b79a1
AJ
23#include "config.h"
24#include "wine/port.h"
25
fc99ddd9 26#include <ctype.h>
e37c6e18 27#include <stdarg.h>
fc99ddd9
DT
28#include <string.h>
29
3b2019ba 30#define WINE_NO_INLINE_STRING
e37c6e18 31#include "windef.h"
fc99ddd9 32#include "winbase.h"
65e7196f
AJ
33#include "wine/unicode.h"
34#include "wine/exception.h"
fc99ddd9
DT
35
36
65e7196f 37/***********************************************************************
65e7196f 38 * lstrcatA (KERNEL32.@)
b8d9b619 39 * lstrcat (KERNEL32.@)
65e7196f
AJ
40 */
41LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
42{
43 __TRY
44 {
45 strcat( dst, src );
46 }
ae964ac8 47 __EXCEPT_PAGE_FAULT
65e7196f
AJ
48 {
49 SetLastError( ERROR_INVALID_PARAMETER );
50 return NULL;
51 }
52 __ENDTRY
53 return dst;
54}
55
56
57/***********************************************************************
58 * lstrcatW (KERNEL32.@)
59 */
60LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
61{
62 __TRY
63 {
64 strcatW( dst, src );
65 }
ae964ac8 66 __EXCEPT_PAGE_FAULT
65e7196f
AJ
67 {
68 SetLastError( ERROR_INVALID_PARAMETER );
69 return NULL;
70 }
71 __ENDTRY
72 return dst;
73}
74
75
65e7196f 76/***********************************************************************
65e7196f 77 * lstrcpyA (KERNEL32.@)
b8d9b619 78 * lstrcpy (KERNEL32.@)
65e7196f
AJ
79 */
80LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
81{
82 __TRY
83 {
84 /* this is how Windows does it */
85 memmove( dst, src, strlen(src)+1 );
86 }
ae964ac8 87 __EXCEPT_PAGE_FAULT
65e7196f
AJ
88 {
89 SetLastError( ERROR_INVALID_PARAMETER );
90 return NULL;
91 }
92 __ENDTRY
93 return dst;
94}
95
96
97/***********************************************************************
98 * lstrcpyW (KERNEL32.@)
99 */
100LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
101{
102 __TRY
103 {
104 strcpyW( dst, src );
105 }
ae964ac8 106 __EXCEPT_PAGE_FAULT
65e7196f
AJ
107 {
108 SetLastError( ERROR_INVALID_PARAMETER );
109 return NULL;
110 }
111 __ENDTRY
112 return dst;
113}
114
115
b81d9e7c 116/***********************************************************************
b81d9e7c 117 * lstrcpynA (KERNEL32.@)
b8d9b619 118 * lstrcpyn (KERNEL32.@)
b81d9e7c
AJ
119 *
120 * Note: this function differs from the UNIX strncpy, it _always_ writes
121 * a terminating \0.
122 *
123 * Note: n is an INT but Windows treats it as unsigned, and will happily
124 * copy a gazillion chars if n is negative.
125 */
126LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
127{
128 __TRY
129 {
130 LPSTR d = dst;
131 LPCSTR s = src;
132 UINT count = n;
133
134 while ((count > 1) && *s)
135 {
136 count--;
137 *d++ = *s++;
138 }
139 if (count) *d = 0;
140 }
ae964ac8 141 __EXCEPT_PAGE_FAULT
b81d9e7c
AJ
142 {
143 SetLastError( ERROR_INVALID_PARAMETER );
144 return 0;
145 }
146 __ENDTRY
147 return dst;
148}
149
150
151/***********************************************************************
152 * lstrcpynW (KERNEL32.@)
153 *
154 * Note: this function differs from the UNIX strncpy, it _always_ writes
155 * a terminating \0
156 *
157 * Note: n is an INT but Windows treats it as unsigned, and will happily
158 * copy a gazillion chars if n is negative.
159 */
160LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
161{
162 __TRY
163 {
164 LPWSTR d = dst;
165 LPCWSTR s = src;
166 UINT count = n;
167
168 while ((count > 1) && *s)
169 {
170 count--;
171 *d++ = *s++;
172 }
173 if (count) *d = 0;
174 }
ae964ac8 175 __EXCEPT_PAGE_FAULT
b81d9e7c
AJ
176 {
177 SetLastError( ERROR_INVALID_PARAMETER );
178 return 0;
179 }
180 __ENDTRY
181 return dst;
182}
183
184
65e7196f 185/***********************************************************************
65e7196f 186 * lstrlenA (KERNEL32.@)
b8d9b619 187 * lstrlen (KERNEL32.@)
65e7196f
AJ
188 */
189INT WINAPI lstrlenA( LPCSTR str )
190{
191 INT ret;
192 __TRY
193 {
194 ret = strlen(str);
195 }
ae964ac8 196 __EXCEPT_PAGE_FAULT
65e7196f
AJ
197 {
198 SetLastError( ERROR_INVALID_PARAMETER );
199 return 0;
200 }
201 __ENDTRY
202 return ret;
203}
204
205
206/***********************************************************************
207 * lstrlenW (KERNEL32.@)
208 */
209INT WINAPI lstrlenW( LPCWSTR str )
210{
211 INT ret;
212 __TRY
213 {
214 ret = strlenW(str);
215 }
ae964ac8 216 __EXCEPT_PAGE_FAULT
65e7196f
AJ
217 {
218 SetLastError( ERROR_INVALID_PARAMETER );
219 return 0;
220 }
221 __ENDTRY
222 return ret;
223}