add a_strcpy() function to util.c

This commit is contained in:
Julien Danjou 2007-09-24 16:44:36 +02:00
parent df75e01ce0
commit 9a97c13804
2 changed files with 26 additions and 0 deletions

25
util.c
View File

@ -168,3 +168,28 @@ ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
return len;
}
/** \brief safe strcpy.
*
* Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
* adding a final \c \\0 in \c dst.
*
* \param[in] dst destination buffer.
* \param[in] n size of the buffer. Negative sizes are allowed.
* \param[in] src source string.
* \return \c src \e length. If this value is \>= \c n then the copy was
* truncated.
*/
ssize_t a_strcpy(char *dst, ssize_t n, const char *src)
{
ssize_t len = a_strlen(src);
if (n > 0)
{
ssize_t dlen = MIN(n - 1, len);
memcpy(dst, src, dlen);
dst[dlen] = '\0';
}
return len;
}

1
util.h
View File

@ -137,6 +137,7 @@ static inline int a_strcmp(const char *a, const char *b)
}
ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1)));
ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1)));
void die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
void eprint(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));