net-snmp 5.7
strlcpy.c
00001 /*
00002  * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
00003  * Use is subject to license terms specified in the COPYING file
00004  * distributed with the Net-SNMP package.
00005  */
00006 #include <net-snmp/net-snmp-config.h>
00007 
00008 #if HAVE_STRING_H
00009 #include <string.h>
00010 #else
00011 #include <strings.h>
00012 #endif
00013 
00014 #include <sys/types.h>
00015 
00016 #include <net-snmp/library/system.h>
00017 
00018 /* 
00019  * Copies src to the dest buffer. The copy will never overflow the dest buffer
00020  * and dest will always be null terminated, len is the size of the dest buffer.
00021  *
00022  * Returns the length of the src buffer.
00023  */ 
00024 size_t 
00025 strlcpy(char *dest, const char *src, size_t len) 
00026 { 
00027         size_t src_len = strlen(src); 
00028         size_t new_len; 
00029 
00030         if (len == 0) {
00031                 return (src_len);
00032         }
00033 
00034         if (src_len >= len) {
00035                 new_len = len - 1;
00036         } else {
00037                 new_len = src_len;
00038         }
00039 
00040         memcpy(dest, src, new_len); 
00041         dest[new_len] = '\0'; 
00042         return (src_len); 
00043 }