chiark / gitweb /
Commit 2.4.5-5 as unpacked
[inn-innduct.git] / lib / strlcpy.c
1 /*  $Id: strlcpy.c 5681 2002-08-29 04:07:50Z rra $
2 **
3 **  Replacement for a missing strlcpy.
4 **
5 **  Written by Russ Allbery <rra@stanford.edu>
6 **  This work is hereby placed in the public domain by its author.
7 **
8 **  Provides the same functionality as the *BSD function strlcpy, originally
9 **  developed by Todd Miller and Theo de Raadt.  strlcpy works similarly to
10 **  strncpy, except saner and simpler.  The result is always nul-terminated
11 **  even if the source string is longer than the destination string, and the
12 **  total space required is returned.  The destination string is not
13 **  nul-filled like strncpy does, just nul-terminated.
14 */
15
16 #include "config.h"
17 #include "clibrary.h"
18
19 /* If we're running the test suite, rename strlcpy to avoid conflicts with
20    the system version. */
21 #if TESTING
22 # define strlcpy test_strlcpy
23 size_t test_strlcpy(char *, const char *, size_t);
24 #endif
25
26 size_t
27 strlcpy(char *dst, const char *src, size_t size)
28 {
29     size_t length, copy;
30
31     length = strlen(src);
32     if (size > 0) {
33         copy = (length >= size) ? size - 1 : length;
34         memcpy(dst, src, copy);
35         dst[copy] = '\0';
36     }
37     return length;
38 }