11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1990, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * Chris Torek. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes */ 321541Srgrimes 33116189Sobrien#include <sys/cdefs.h> 34116189Sobrien__FBSDID("$FreeBSD: stable/11/sys/libkern/strncpy.c 319284 2017-05-31 05:45:06Z delphij $"); 35116189Sobrien 36102281Sjhb#include <sys/libkern.h> 371541Srgrimes 381541Srgrimes/* 391541Srgrimes * Copy src to dst, truncating or null-padding to always copy n bytes. 401541Srgrimes * Return dst. 411541Srgrimes */ 421541Srgrimeschar * 43110605Shsustrncpy(char * __restrict dst, const char * __restrict src, size_t n) 441541Srgrimes{ 451541Srgrimes if (n != 0) { 46319284Sdelphij char *d = dst; 47319284Sdelphij const char *s = src; 481541Srgrimes 491541Srgrimes do { 501541Srgrimes if ((*d++ = *s++) == 0) { 511541Srgrimes /* NUL pad the remaining n-1 bytes */ 521541Srgrimes while (--n != 0) 531541Srgrimes *d++ = 0; 541541Srgrimes break; 551541Srgrimes } 561541Srgrimes } while (--n != 0); 571541Srgrimes } 581541Srgrimes return (dst); 591541Srgrimes} 60