1. 程式人生 > >strncpy字串拷貝函式

strncpy字串拷貝函式

摘自linux核心4.11.1 原始碼string.c
linux/lib/string.c
Copyright (C) 1991, 1992  Linus Torvalds

標頭檔案:#include <string.h>
作用:將源字串src複製count個位元組至目標儲存區dest;如果源src超過count個位元組,結果不是NUL終止的,在src的長度小於count的情況下,dest的其餘部分將用%NUL填充。
引數:
dest:目標儲存區
src:源字串
count:複製的最大位元組數
返回值:
dest 目標儲存區的地址
/**
 * strncpy - Copy a length-limited, C-string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @count: The maximum number of bytes to copy
 *
 * The result is not %NUL-terminated if the source exceeds
 * @count bytes.
 *
 * In the case where the length of @src is less than  that  of
 * count, the remainder of @dest will be padded with %NUL.
 *
 */
char *strncpy(char *dest, const char *src, size_t count)
{
	char *tmp = dest;


	while (count) {
		if ((*tmp = *src) != 0)
			src++;
		tmp++;
		count--;
	}
	return dest;
}