1c72fcc34Sopenharmony_ci/* 2c72fcc34Sopenharmony_ci * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> 3c72fcc34Sopenharmony_ci * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org> 4c72fcc34Sopenharmony_ci * 5c72fcc34Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 6c72fcc34Sopenharmony_ci * under the terms of the GNU General Public License as published by the 7c72fcc34Sopenharmony_ci * Free Software Foundation version 2 of the License. 8c72fcc34Sopenharmony_ci * 9c72fcc34Sopenharmony_ci * This program is distributed in the hope that it will be useful, but 10c72fcc34Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 11c72fcc34Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12c72fcc34Sopenharmony_ci * General Public License for more details. 13c72fcc34Sopenharmony_ci * 14c72fcc34Sopenharmony_ci * You should have received a copy of the GNU General Public License along 15c72fcc34Sopenharmony_ci * with this program; if not, write to the Free Software Foundation, Inc., 16c72fcc34Sopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17c72fcc34Sopenharmony_ci * 18c72fcc34Sopenharmony_ci */ 19c72fcc34Sopenharmony_ci 20c72fcc34Sopenharmony_ci#if defined(__GLIBC__) && !(defined(__UCLIBC__) && defined(__USE_BSD)) 21c72fcc34Sopenharmony_ci#if !(__GLIBC_PREREQ(2, 38)) 22c72fcc34Sopenharmony_cistatic size_t strlcpy(char *dst, const char *src, size_t size) 23c72fcc34Sopenharmony_ci{ 24c72fcc34Sopenharmony_ci size_t bytes = 0; 25c72fcc34Sopenharmony_ci char *q = dst; 26c72fcc34Sopenharmony_ci const char *p = src; 27c72fcc34Sopenharmony_ci char ch; 28c72fcc34Sopenharmony_ci 29c72fcc34Sopenharmony_ci while ((ch = *p++)) { 30c72fcc34Sopenharmony_ci if (bytes+1 < size) 31c72fcc34Sopenharmony_ci *q++ = ch; 32c72fcc34Sopenharmony_ci bytes++; 33c72fcc34Sopenharmony_ci } 34c72fcc34Sopenharmony_ci 35c72fcc34Sopenharmony_ci /* If size == 0 there is no space for a final null... */ 36c72fcc34Sopenharmony_ci if (size) 37c72fcc34Sopenharmony_ci *q = '\0'; 38c72fcc34Sopenharmony_ci return bytes; 39c72fcc34Sopenharmony_ci} 40c72fcc34Sopenharmony_ci 41c72fcc34Sopenharmony_cistatic size_t strlcat(char *dst, const char *src, size_t size) 42c72fcc34Sopenharmony_ci{ 43c72fcc34Sopenharmony_ci size_t bytes = 0; 44c72fcc34Sopenharmony_ci char *q = dst; 45c72fcc34Sopenharmony_ci const char *p = src; 46c72fcc34Sopenharmony_ci char ch; 47c72fcc34Sopenharmony_ci 48c72fcc34Sopenharmony_ci while (bytes < size && *q) { 49c72fcc34Sopenharmony_ci q++; 50c72fcc34Sopenharmony_ci bytes++; 51c72fcc34Sopenharmony_ci } 52c72fcc34Sopenharmony_ci if (bytes == size) 53c72fcc34Sopenharmony_ci return (bytes + strlen(src)); 54c72fcc34Sopenharmony_ci 55c72fcc34Sopenharmony_ci while ((ch = *p++)) { 56c72fcc34Sopenharmony_ci if (bytes+1 < size) 57c72fcc34Sopenharmony_ci *q++ = ch; 58c72fcc34Sopenharmony_ci bytes++; 59c72fcc34Sopenharmony_ci } 60c72fcc34Sopenharmony_ci 61c72fcc34Sopenharmony_ci *q = '\0'; 62c72fcc34Sopenharmony_ci return bytes; 63c72fcc34Sopenharmony_ci} 64c72fcc34Sopenharmony_ci#endif /* !(__GLIBC_PREREQ(2, 38)) */ 65c72fcc34Sopenharmony_ci#endif /* __GLIBC__ */ 66