gpt4 book ai didi

c - 如何将字符串复制到用户并在 Linux 内核读取函数中使用 offp

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:11 24 4
gpt4 key购买 nike

声明:

static char status[128] = "off\0";

并实现了一个read函数:

static ssize_t read_proc(struct file *filep, char __user *buf,
size_t len, loff_t *offp)
{
ssize_t cnt = strlen(status), ret;

ret = copy_to_user(buf, status, cnt);
*offp += cnt;
return cnt;
}
  • 如何考虑 offp
  • 目前它不断地打印 status 到屏幕

最佳答案

感谢这里的评论,我想到了以下实现,我相信这是使用 offp 的正确方法:

static ssize_t read_proc(struct file *filep, char __user *buf,
size_t len, loff_t *offp)
{

ssize_t cnt = strlen(status), ret;

/* ret contains the amount of chare wasn't successfully written to `buf` */
ret = copy_to_user(buf, status, cnt);
*offp += cnt - ret;

/* Making sure there are no left bytes of data to send user */
if (*offp > cnt)
return 0;
else
return cnt;
}

关于c - 如何将字符串复制到用户并在 Linux 内核读取函数中使用 offp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com