美文网首页
Linux编程--strncpy与stpncpy、memcpy的

Linux编程--strncpy与stpncpy、memcpy的

作者: None_Ling | 来源:发表于2019-04-25 21:37 被阅读0次

    背景

    在通过fd查找对应的文件路径的过程中,会用到如下函数:

    • snprintf:从/proc/self/fd/71中读取内容,保存到buf
    • readlink:由于从fd中获取的内容有可能是软连接\硬链接,所以需要通过readlink函数来找到链接对应的真正的路径

    代码如下:

    char buf[1024];
    char file_path[1024];
    // 通过snprintf将`/proc/self/fd/71`中的软\硬链接读取到buf中
    snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
    // 通过readlink将链接对应的全路径保存到file_path变量中,返回值则是路径的长度
    ssize_t read_link_result = readlink(buf, file_path, sizeof(file_path));
    

    由于file_path初始化为1024个字符,接着,就需要从file_path中截取相应大小的字符串,才能找到对应的文件路径了,否则就是乱码。

    方案

    截取字符串有下面三种方法:

    • strncpy \ strcpy
    • stpncpy \ stpcpy
    • memcpy

    strcpy和stpcpy

    • 函数原型
    #include <string.h>
    char * strcpy(char *restrict dst, const char *restrict src);
    char * strncpy(char *restrict dst, const char *restrict src, size_t n);
    char * stpcpy(char *dst, const char *src);
    char * stpncpy(char *restrict dst, const char *restrict src, size_t n);
    
    • 作用

    strcpystpcpy:将src的字符串复制到dst中,包括\0终止字符也会复制。

    strncpystpncpy:将src的字符串复制到dst中,但是复制最多n个字符。如果src的长度小于n的话,则剩下的dst中的字符会用\0充满。否则,dst不会停止填充。

    • 返回值

    strcpystrncpy的返回值为:dst的指针。

    stpcpystpncpy的返回值为:dst中的\0终止符的指针。如果stpncpy的最后一个字符不是\0的话,则会返回dst[n]的指针。

    memcpy

    • 函数原型
    #include <string.h>
    
    void * memcpy(void *restrict dst, const void *restrict src, size_t n);
    
    • 作用

    拷贝src中的n个字符到dst中。

    如果srcdst有重叠的话,那么该操作就会出错。

    • 返回值

    返回dst的指针。

    使用注意事项

    在JNI调用的过程中,发现并不如注释所描述的那样,读取的字符数与实际的大小不符合。

     char buf[1024];
     char file_path[1024];
     snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd);
     ssize_t read_link_result = readlink(buf, file_path, sizeof(file_path - 1));
     if (read_link_result > 0) {
           char des[read_link_result];
           // 从file_path中copy read_link_result个字符到des中
           strncpy(des, file_path, read_link_result);
           //des[read_link_result] = '\0';
           LOGE(TAG, "File Close fd:%d  size:%ld  des:%s  sizeof:%d", fd, read_link_result, des, strlen(des));
    }
    
    

    输出结果:

    File Close fd:59  size:8  des:/sys/dev��f/u  sizeof:13
    

    结果还是需要在返回字符长度的位置,手动改成终止字符\0。也就是:

    des[read_link_result] = '\0';
    

    打开这行注释就可以正确获取字符串了。

    相关文章

      网友评论

          本文标题:Linux编程--strncpy与stpncpy、memcpy的

          本文链接:https://www.haomeiwen.com/subject/nustnqtx.html