X-Git-Url: https://git.saurik.com/apple/libplatform.git/blobdiff_plain/e39c3b4b1d65e3593d0e74f799230d571e52d4fa..438624e04d57192b9d4dd7a6083a0ccd59a5a95f:/src/string/generic/strncpy.c diff --git a/src/string/generic/strncpy.c b/src/string/generic/strncpy.c new file mode 100644 index 0000000..1dc1367 --- /dev/null +++ b/src/string/generic/strncpy.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2011 Apple, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#include + +#if !_PLATFORM_OPTIMIZED_STRNCPY + +char * +_platform_strncpy(char * restrict dst, const char * restrict src, size_t maxlen) { + const size_t srclen = _platform_strnlen(src, maxlen); + if (srclen < maxlen) { + // The stpncpy() and strncpy() functions copy at most maxlen + // characters from src into dst. + _platform_memmove(dst, src, srclen); + // If src is less than maxlen characters long, the remainder + // of dst is filled with '\0' characters. + _platform_memset(dst+srclen, 0, maxlen-srclen); + } else { + // Otherwise, dst is not terminated. + _platform_memmove(dst, src, maxlen); + } + // The strcpy() and strncpy() functions return dst. + return dst; +} + +#if VARIANT_STATIC +char * +strncpy(char * restrict dst, const char * restrict src, size_t maxlen) { + return _platform_strncpy(dst, src, maxlen); +} +#endif + +#endif