strlcpy 和 strlcat 源码

strlcpy.cpp
None.gifsize_t strlcpy(  char *dst,  const  char *src, size_t siz )
ExpandedBlockStart.gif {
InBlock.gif    char*            d = dst;
InBlock.gif    const char*        s = src;
InBlock.gif    size_t            n = siz;
InBlock.gif
InBlock.gif    if (s == 0 || d == 0) return 0;
InBlock.gif
ExpandedSubBlockStart.gif    /* Copy as many bytes as will fit */
InBlock.gif    if (n != 0 && --n != 0)
ExpandedSubBlockStart.gif    {
InBlock.gif        do
ExpandedSubBlockStart.gif        {
InBlock.gif            if ((*d++ = *s++) == 0)
InBlock.gif                break;
ExpandedSubBlockEnd.gif        } while (--n != 0);
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedSubBlockStart.gif    /* Not enough room in dst, add NUL and traverse rest of src */
InBlock.gif    if (n == 0)
ExpandedSubBlockStart.gif    {
InBlock.gif        if (siz != 0)
ExpandedSubBlockStart.gif            *d = '\0';                /* NUL-terminate dst */
InBlock.gif        while (*s++)
InBlock.gif            ;
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedSubBlockStart.gif    return(s - src - 1);        /* count does not include NUL */
ExpandedBlockEnd.gif}


strlcat.cpp
None.gifsize_t strlcat(  char* dst,  const  char* src, size_t siz )
ExpandedBlockStart.gif {
InBlock.gif    char*        d = dst;
InBlock.gif    const char*    s = src;
InBlock.gif    size_t        n = siz;
InBlock.gif    size_t        dlen;
InBlock.gif
InBlock.gif    if (s == 0 || d == 0) return 0;
InBlock.gif
InBlock.gif    while (n-- != 0 && *d != '\0')
ExpandedSubBlockStart.gif    {
InBlock.gif        d++;
ExpandedSubBlockEnd.gif    }
InBlock.gif    dlen = d - dst;
InBlock.gif    n = siz - dlen;
InBlock.gif
InBlock.gif    if (n == 0)
ExpandedSubBlockStart.gif    {
InBlock.gif        return(dlen + strlen(s));
ExpandedSubBlockEnd.gif    }
InBlock.gif    while (*s != '\0')
ExpandedSubBlockStart.gif    {
InBlock.gif        if (n != 1)
ExpandedSubBlockStart.gif        {
InBlock.gif            *d++ = *s;
InBlock.gif            n--;
ExpandedSubBlockEnd.gif        }
InBlock.gif        s++;
ExpandedSubBlockEnd.gif    }
InBlock.gif    *d = '\0';
InBlock.gif
InBlock.gif    return(dlen + (s - src));
ExpandedBlockEnd.gif}


略作了修改,增加了对空指针的判断.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值