add Q_strlcpy and Q_strlcat

This commit is contained in:
svdijk 2013-05-11 13:19:52 +02:00
parent 392c04bc1d
commit a26892cb90
2 changed files with 39 additions and 0 deletions

View file

@ -223,6 +223,10 @@ int Q_strncasecmp(char *s1, char *s2, int n);
/* portable string lowercase */ /* portable string lowercase */
char *Q_strlwr(char *s); char *Q_strlwr(char *s);
/* portable safe string copy/concatenate */
int Q_strlcpy(char *dst, const char *src, int size);
int Q_strlcat(char *dst, const char *src, int size);
/* ============================================= */ /* ============================================= */
short BigShort(short l); short BigShort(short l);

View file

@ -1132,6 +1132,41 @@ Q_strlwr ( char *s )
return ( p ); return ( p );
} }
int
Q_strlcpy(char *dst, const char *src, int size)
{
const char *s = src;
while (*s)
{
if (size > 1)
{
*dst++ = *s;
size--;
}
s++;
}
if (size > 0)
{
*dst = '\0';
}
return s - src;
}
int
Q_strlcat(char *dst, const char *src, int size)
{
char *d = dst;
while (*d)
{
d++;
}
return (d - dst) + Q_strlcpy(d, src, size - (d - dst));
}
/* /*
* ===================================================================== * =====================================================================
* *