/src/net-snmp/snmplib/strlcpy.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2003 Sun Microsystems, Inc. All rights reserved. |
3 | | * Use is subject to license terms specified in the COPYING file |
4 | | * distributed with the Net-SNMP package. |
5 | | */ |
6 | | #include <net-snmp/net-snmp-config.h> |
7 | | |
8 | | #ifdef HAVE_STRING_H |
9 | | #include <string.h> |
10 | | #else |
11 | | #include <strings.h> |
12 | | #endif |
13 | | |
14 | | #include <sys/types.h> |
15 | | |
16 | | #include <net-snmp/library/system.h> |
17 | | |
18 | | /* |
19 | | * Copies src to the dest buffer. The copy will never overflow the dest buffer |
20 | | * and dest will always be null terminated, len is the size of the dest buffer. |
21 | | * |
22 | | * Returns the length of the src buffer. |
23 | | */ |
24 | | size_t |
25 | | strlcpy(char *dest, const char *src, size_t len) |
26 | 648 | { |
27 | 648 | size_t src_len = strlen(src); |
28 | 648 | size_t new_len; |
29 | | |
30 | 648 | if (len == 0) { |
31 | 0 | return (src_len); |
32 | 0 | } |
33 | | |
34 | 648 | if (src_len >= len) { |
35 | 0 | new_len = len - 1; |
36 | 648 | } else { |
37 | 648 | new_len = src_len; |
38 | 648 | } |
39 | | |
40 | 648 | memcpy(dest, src, new_len); |
41 | 648 | dest[new_len] = '\0'; |
42 | 648 | return (src_len); |
43 | 648 | } |