Hi3519a编译工具链string.h中为啥没有strlcpy和strlcat

1.前言

    今天从hi3520dv300移植代码到hi3519av100中,原来代码中使用高频率strlcpy函数,而在hi3519a中却报没有定义的错误。通过查看编译链中string.h中定义,hi3519av100确实没有。网上资料说strlcpy和strlcat是BSD的C库函数,glibc维护者一直拒绝将其加入,并不属于ANSI C,至今也还不是标准。为了和之前海思芯片编译工具兼容性,故添加到海思sdk的include中。

2.源码实现

 

/**
* Copyright (C), 2016, 
* All rights reserved.
*
* @file      my_string.h
* @brief     解决hi3519a中没有strlcpy和 strlcat函数,为了与其他海思编译器兼容特引入该头文件
* @author    ronbin
* @date      
*/

#ifndef __MY_STRING_H__
#define __MY_STRING_H__

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */


/*
 * Copy src to string dst of size siz.  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz == 0).
 * Returns strlen(src); if retval >= siz, truncation occurred.
 */

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


/*
 * Appends src to string dst of size siz (unlike strncat, siz is the
 * full size of dst, not space left).  At most siz-1 charact
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 * If retval >= siz, truncation occurred.
 */
size_t
strlcat(char *dst, const char *src, size_t siz)
{
    register char *d = dst;
    register const char *s = src;
    register size_t n = siz;
    size_t dlen;
 
    if (s == 0 || d == 0) return 0;
 
    /* Find the end of dst and adjust bytes left but don't go past end */
    while (n-- != 0 && *d != '\0')
        d++;
    dlen = d - dst;
    n = siz - dlen;
 
    if (n == 0)
        return(dlen + strlen(s));
    while (*s != '\0') {
        if (n != 1) {
            *d++ = *s;
            n--;
        }
        s++;
    }
    *d = '\0';
 
    return(dlen + (s - src));   /* count does not include NUL */
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */


#endif /**__MY_STRING_H__*/

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值