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
  • 391,730
  • 64
  • 469
  • 606
Himanshu Sourav
  • 700
  • 1
  • 10
  • 35

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___________
  • 60,014
  • 4
  • 34
  • 74
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,819
  • 12
  • 19