add a_strcpy() function to util.c
This commit is contained in:
parent
df75e01ce0
commit
9a97c13804
25
util.c
25
util.c
|
@ -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
1
util.h
|
@ -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)));
|
||||
|
|
Loading…
Reference in New Issue