Mercurial > sdl-ios-xcode
comparison src/stdlib/SDL_string.c @ 1379:c0a74f199ecf
Use only safe string functions
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 19 Feb 2006 23:46:34 +0000 |
parents | c71e05b4dc2e |
children | d910939febfa |
comparison
equal
deleted
inserted
replaced
1378:dc0e13e7e1ae | 1379:c0a74f199ecf |
---|---|
292 } | 292 } |
293 return len; | 293 return len; |
294 } | 294 } |
295 #endif | 295 #endif |
296 | 296 |
297 #ifndef HAVE_STRCPY | 297 #ifndef HAVE_STRLCPY |
298 char *SDL_strcpy(char *dst, const char *src) | 298 size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen) |
299 { | 299 { |
300 char *dstp = dst; | 300 size_t srclen = SDL_strlen(src); |
301 while ( *src ) { | 301 if ( maxlen > 0 ) { |
302 *dstp++ = *src++; | 302 size_t len = SDL_min(srclen, maxlen-1); |
303 } | 303 SDL_memcpy(dst, src, len); |
304 *dstp = '\0'; | 304 dst[len] = '\0'; |
305 | 305 } |
306 return dst; | 306 return srclen; |
307 } | 307 } |
308 #endif | 308 #endif |
309 | 309 |
310 #ifndef HAVE_STRNCPY | 310 #ifndef HAVE_STRLCAT |
311 char *SDL_strncpy(char *dst, const char *src, size_t maxlen) | 311 size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) |
312 { | 312 { |
313 char *dstp = dst; | 313 size_t dstlen = SDL_strlen(dst); |
314 while ( maxlen-- && *src ) { | 314 size_t srclen = SDL_strlen(src); |
315 *dstp++ = *src++; | 315 if ( dstlen < maxlen ) { |
316 } | 316 SDL_strlcpy(dst+dstlen, src, maxlen-dstlen); |
317 *dstp = '\0'; | 317 } |
318 | 318 return dstlen+srclen; |
319 return dst; | |
320 } | 319 } |
321 #endif | 320 #endif |
322 | 321 |
323 #ifndef HAVE_STRDUP | 322 #ifndef HAVE_STRDUP |
324 char *SDL_strdup(const char *string) | 323 char *SDL_strdup(const char *string) |
325 { | 324 { |
326 size_t len = SDL_strlen(string); | 325 size_t len = SDL_strlen(string)+1; |
327 char *newstr = SDL_malloc(len+1); | 326 char *newstr = SDL_malloc(len); |
328 if ( newstr ) { | 327 if ( newstr ) { |
329 SDL_strcpy(newstr, string); | 328 SDL_strlcpy(newstr, string, len); |
330 } | 329 } |
331 return newstr; | 330 return newstr; |
332 } | 331 } |
333 #endif | 332 #endif |
334 | 333 |
910 SDL_ltoa(value, num, radix); | 909 SDL_ltoa(value, num, radix); |
911 size = SDL_strlen(num); | 910 size = SDL_strlen(num); |
912 if ( size > maxlen ) { | 911 if ( size > maxlen ) { |
913 size = maxlen; | 912 size = maxlen; |
914 } | 913 } |
915 SDL_strncpy(text, num, size); | 914 SDL_strlcpy(text, num, size); |
916 | 915 |
917 return size; | 916 return size; |
918 } | 917 } |
919 static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen) | 918 static size_t SDL_PrintUnsignedLong(char *text, unsigned long value, int radix, size_t maxlen) |
920 { | 919 { |
924 SDL_ultoa(value, num, radix); | 923 SDL_ultoa(value, num, radix); |
925 size = SDL_strlen(num); | 924 size = SDL_strlen(num); |
926 if ( size > maxlen ) { | 925 if ( size > maxlen ) { |
927 size = maxlen; | 926 size = maxlen; |
928 } | 927 } |
929 SDL_strncpy(text, num, size); | 928 SDL_strlcpy(text, num, size); |
930 | 929 |
931 return size; | 930 return size; |
932 } | 931 } |
933 #ifdef SDL_HAS_64BIT_TYPE | 932 #ifdef SDL_HAS_64BIT_TYPE |
934 static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen) | 933 static size_t SDL_PrintLongLong(char *text, Sint64 value, int radix, size_t maxlen) |
939 SDL_lltoa(value, num, radix); | 938 SDL_lltoa(value, num, radix); |
940 size = SDL_strlen(num); | 939 size = SDL_strlen(num); |
941 if ( size > maxlen ) { | 940 if ( size > maxlen ) { |
942 size = maxlen; | 941 size = maxlen; |
943 } | 942 } |
944 SDL_strncpy(text, num, size); | 943 SDL_strlcpy(text, num, size); |
945 | 944 |
946 return size; | 945 return size; |
947 } | 946 } |
948 static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen) | 947 static size_t SDL_PrintUnsignedLongLong(char *text, Uint64 value, int radix, size_t maxlen) |
949 { | 948 { |
953 SDL_ulltoa(value, num, radix); | 952 SDL_ulltoa(value, num, radix); |
954 size = SDL_strlen(num); | 953 size = SDL_strlen(num); |
955 if ( size > maxlen ) { | 954 if ( size > maxlen ) { |
956 size = maxlen; | 955 size = maxlen; |
957 } | 956 } |
958 SDL_strncpy(text, num, size); | 957 SDL_strlcpy(text, num, size); |
959 | 958 |
960 return size; | 959 return size; |
961 } | 960 } |
962 #endif /* SDL_HAS_64BIT_TYPE */ | 961 #endif /* SDL_HAS_64BIT_TYPE */ |
963 static size_t SDL_PrintFloat(char *text, double arg, size_t maxlen) | 962 static size_t SDL_PrintFloat(char *text, double arg, size_t maxlen) |