2

Does strncpy() not have proper equivalent in arm which will take both destination size and number of source characters to be copied,

strlcpy(char * /*dst*/, const char * /*src*/, size_t /*len*/);

so here we have to just use strlcpy() and hope source string is null terminated?

MS has provided one which is perfect (at least appears to be ;)):

StringCchCopyN(LPTSTR pszDest, size_t cchDest, LPCTSTR pszSrc, size_t cchSrc);

To the uninitiated, strncpy() is not secure (why strncpy is unsafe]1).

unwind
  • 378,987
  • 63
  • 458
  • 590
Himanshu Sourav
  • 679
  • 1
  • 8
  • 31

2 Answers2

0

You need to write your own ones

char *mystrncpy(char *dest, const char *src, size_t nbytes)
{
    char *svd = dest;

    if(nbytes)
    {
        dest[--nbytes] = 0;
        while(*src && nbytes-- )
             *dest++ = *src++;
    }
    return svd;
}

OR

char *mystrncpy(char *dest, const char *src, size_t destsize, size_t srcsize)
{
    memcpy(dest, src, destsize < srcsize ? destsize : srcsize);
    dest[destsize -1] = 0;
    return dest;
}
0___________
  • 47,100
  • 4
  • 27
  • 62
0

You could use snprintf(dest, size, "%s", source);

Note that source does need to be \0-terminated for snprintf, even if longer than size.

domen
  • 1,720
  • 11
  • 18