Added strlcat/cpy functions to windows code. strl-imp
authorKrista Grothoff <krista@pep-project.org>
Fri, 22 Jul 2016 16:20:26 +0200
branchstrl-imp
changeset 9204b31d5d0e4d0
parent 919 5d02ed589daa
child 921 2013f282d2f1
Added strlcat/cpy functions to windows code.
src/platform_windows.cpp
     1.1 --- a/src/platform_windows.cpp	Fri Jul 22 16:15:52 2016 +0200
     1.2 +++ b/src/platform_windows.cpp	Fri Jul 22 16:20:26 2016 +0200
     1.3 @@ -233,6 +233,32 @@
     1.4      return dst;
     1.5  }
     1.6  
     1.7 +size_t strlcpy(char* dst, const	char* src, size_t size) {
     1.8 +    size_t retval = strlen(src);
     1.9 +    size_t size_to_copy = (retval < size ? retval : size - 1);
    1.10 +    
    1.11 +    // strlcpy doc says src and dst not allowed to overlap, as
    1.12 +    // it's undefined. So this is acceptable:
    1.13 +    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    1.14 +    dst[size_to_copy] = '\0';
    1.15 +    return retval;
    1.16 +}
    1.17 +size_t strlcat(char* dst, const	char* src, size_t size) {
    1.18 +    size_t start_len = strnlen(dst, size);
    1.19 +    if (start_len == size)
    1.20 +        return size; // no copy, no null termination in size bytes, according to spec
    1.21 +    
    1.22 +    size_t add_len = strlen(src);
    1.23 +    size_t retval = start_len + add_len;
    1.24 +    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
    1.25 +    
    1.26 +    // strlcat doc says src and dst not allowed to overlap, as
    1.27 +    // it's undefined. So this is acceptable:
    1.28 +    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
    1.29 +    dst[start_len + size_to_copy] = '\0';
    1.30 +    return retval;
    1.31 +}
    1.32 +
    1.33  int mkstemp(char *templ)
    1.34  {
    1.35      char *pathname = _mktemp(templ);