strndup.c
来自「卡内基梅隆大学MaCallum开发的文本分类系统」· C语言 代码 · 共 33 行
C
33 行
/* Implementation of strndup() for libc's that don't have it. */#if !HAVE_STRNDUP#include <stdio.h>#include <string.h>#include <sys/types.h>#include <malloc.h>/* Find the length of STRING, but scan at most MAXLEN characters. If no '\0' terminator is found in that many characters, return MAXLEN. */size_tstrnlen (const char *string, size_t maxlen){ const char *end = memchr (string, '\0', maxlen); return end ? end - string : maxlen;}char *strndup (const char *s, size_t n){ size_t len = strnlen (s, n); char *new = malloc (len + 1); if (new == NULL) return NULL; new[len] = '\0'; return memcpy (new, s, len);}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?