前两天看libsmi源代码,里面对strncpy函数进行了封装,感觉有些缺陷,虽然内部使用的函数,满足要求就可以了,但想了想发现自己它了解的也不是很多。
关于strncpy,《C程序设计语言》中是这样介绍的:“char *strncpy(s, ct, n),strncpy用于把字符串ct中最多n个字符拷贝到字符串s中,并返回s。如果ct中少于n个字符,那么就用’/0’补充。”
写了一个简单的程序对strncpy进行测试,测试程序如下:
#include #include #include #include #include #define MARK_LINE() line = __LINE__ int line = 0; jmp_buf mark; static void signal11(int sig) { printf("line %d: recv signal %d./n", line, sig); longjmp(mark, 1); } #define check_mem/ line = __LINE__; if (setjmp(mark) == 0) #define print_buf(buf, buflen, i)/ for (i = 0; i < buflen; i++)/ printf("%+02x ", buf[i]);/ printf("/n");/ int main() { int i; char buf[16]; char *s1 = "haha"; char *s2 = NULL; char *s3 = "12345678901234567890"; signal(SIGSEGV, signal11); for (i = 0; i < 15; i++) buf[i] = '.'; buf[15] = '/0'; check_mem { printf("strncpy return: %s/n", strncpy(buf, s2, 0)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s1, 4)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s1, 10)); } print_buf(buf, 16, i); check_mem { printf("strncpy return: %s/n", strncpy(buf, s1, -1)); } print_buf(buf, 16, i); check_mem { printf("strncpy return: %s/n", strncpy(buf, s1, 0)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s1, 2)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s3, 15)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s1, -1)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s2, -1)); } check_mem { printf("strncpy return: %s/n", strncpy(buf, s2, 1)); } exit(0); }
测试程序运行(gcc3.2.3+libc2.3.2)结果如下:
[huzhan@localhost log]$ ./test_strncpy
strncpy return: ...............
strncpy return: haha...........
strncpy return: haha
68 61 68 61 00 00 00 00 00 00 2e 2e 2e 2e 2e 00
line 41: recv signal 11.
68 61 68 61 00 00 00 00 00 00 00 00 00 00 00 00
strncpy return: haha
strncpy return: haha
strncpy return: 123456789012345
line 46: recv signal 11.
line 47: recv signal 11.
line 48: recv signal 11.
对于该函数比较明确的说明,可以参见百度百科中strncpy词条(查看)。