src/stdlib/SDL_string.c
changeset 1379 c0a74f199ecf
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
     1.1 --- a/src/stdlib/SDL_string.c	Sun Feb 19 23:38:57 2006 +0000
     1.2 +++ b/src/stdlib/SDL_string.c	Sun Feb 19 23:46:34 2006 +0000
     1.3 @@ -294,39 +294,38 @@
     1.4  }
     1.5  #endif
     1.6  
     1.7 -#ifndef HAVE_STRCPY
     1.8 -char *SDL_strcpy(char *dst, const char *src)
     1.9 +#ifndef HAVE_STRLCPY
    1.10 +size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen)
    1.11  {
    1.12 -    char *dstp = dst;
    1.13 -    while ( *src ) {
    1.14 -        *dstp++ = *src++;
    1.15 +    size_t srclen = SDL_strlen(src);
    1.16 +    if ( maxlen > 0 ) {
    1.17 +        size_t len = SDL_min(srclen, maxlen-1);
    1.18 +        SDL_memcpy(dst, src, len);
    1.19 +        dst[len] = '\0';
    1.20      }
    1.21 -    *dstp = '\0';
    1.22 -
    1.23 -    return dst;
    1.24 +    return srclen;
    1.25  }
    1.26  #endif
    1.27  
    1.28 -#ifndef HAVE_STRNCPY
    1.29 -char *SDL_strncpy(char *dst, const char *src, size_t maxlen)
    1.30 +#ifndef HAVE_STRLCAT
    1.31 +size_t SDL_strlcat(char *dst, const char *src, size_t maxlen)
    1.32  {
    1.33 -    char *dstp = dst;
    1.34 -    while ( maxlen-- && *src ) {
    1.35 -        *dstp++ = *src++;
    1.36 +    size_t dstlen = SDL_strlen(dst);
    1.37 +    size_t srclen = SDL_strlen(src);
    1.38 +    if ( dstlen < maxlen ) {
    1.39 +        SDL_strlcpy(dst+dstlen, src, maxlen-dstlen);
    1.40      }
    1.41 -    *dstp = '\0';
    1.42 -
    1.43 -    return dst;
    1.44 +    return dstlen+srclen;
    1.45  }
    1.46  #endif
    1.47  
    1.48  #ifndef HAVE_STRDUP
    1.49  char *SDL_strdup(const char *string)
    1.50  {
    1.51 -    size_t len = SDL_strlen(string);
    1.52 -    char *newstr = SDL_malloc(len+1);
    1.53 +    size_t len = SDL_strlen(string)+1;
    1.54 +    char *newstr = SDL_malloc(len);
    1.55      if ( newstr ) {
    1.56 -        SDL_strcpy(newstr, string);
    1.57 +        SDL_strlcpy(newstr, string, len);
    1.58      }
    1.59      return newstr;
    1.60  }
    1.61 @@ -912,7 +911,7 @@
    1.62      if ( size > maxlen ) {
    1.63          size = maxlen;
    1.64      }
    1.65 -    SDL_strncpy(text, num, size);
    1.66 +    SDL_strlcpy(text, num, size);
    1.67  
    1.68      return size;
    1.69  }
    1.70 @@ -926,7 +925,7 @@
    1.71      if ( size > maxlen ) {
    1.72          size = maxlen;
    1.73      }
    1.74 -    SDL_strncpy(text, num, size);
    1.75 +    SDL_strlcpy(text, num, size);
    1.76  
    1.77      return size;
    1.78  }
    1.79 @@ -941,7 +940,7 @@
    1.80      if ( size > maxlen ) {
    1.81          size = maxlen;
    1.82      }
    1.83 -    SDL_strncpy(text, num, size);
    1.84 +    SDL_strlcpy(text, num, size);
    1.85  
    1.86      return size;
    1.87  }
    1.88 @@ -955,7 +954,7 @@
    1.89      if ( size > maxlen ) {
    1.90          size = maxlen;
    1.91      }
    1.92 -    SDL_strncpy(text, num, size);
    1.93 +    SDL_strlcpy(text, num, size);
    1.94  
    1.95      return size;
    1.96  }