1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 20 unchanged lines hidden (view full) --- 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#if defined(LIBC_SCCS) && !defined(lint) 34static char sccsid[] = "@(#)strncpy.c 8.1 (Berkeley) 6/4/93"; 35#endif /* LIBC_SCCS and not lint */ 36#include <sys/cdefs.h> |
37__FBSDID("$FreeBSD: head/lib/libc/string/strncpy.c 188080 2009-02-03 17:58:20Z danger $"); |
38 39#include <string.h> 40 41/* 42 * Copy src to dst, truncating or null-padding to always copy n bytes. 43 * Return dst. 44 */ 45char * 46strncpy(char * __restrict dst, const char * __restrict src, size_t n) 47{ 48 if (n != 0) { 49 char *d = dst; 50 const char *s = src; 51 52 do { |
53 if ((*d++ = *s++) == '\0') { |
54 /* NUL pad the remaining n-1 bytes */ 55 while (--n != 0) |
56 *d++ = '\0'; |
57 break; 58 } 59 } while (--n != 0); 60 } 61 return (dst); 62} |