seq_file仅为读取文件提供帮助程序.写入没有类似的帮助程序,但是用手实现.write回调可迭代数据并不困难:
与读取不同,您可以在.write回调中删除文件的位置处理,假设用户始终写入开头,或者可选地,写入结尾(使用O_APPEND文件的控制标志).
其次,与阅读不同,您可以假设用户立即写入1,2或更多元素的内容,但不是半元素.
最简单的方法是允许单个元素写:
size_t write(struct file* file,const char __user* buf,size_t size,loff_t* pos)
{
copy_from_user(str,buf,size); // Copy string from user space
my_elem = my_parse(str,size); // Parse string
if(file->f_flags & O_APPEND) {
list_add_tail(my_elem,&my_list);//Append element to the end of list
}
else {
clear_my_list(); // Clear old content of the list
list_add_tail(my_elem,&my_list);// Add single element to it.
}
(void)pos; //Do not use file position at all
return count; // Return number of bytes passed by the user
}
如果用户想要写几个元素,例如,从硬盘上的文件中写入,则任何shell都能够通过例如新行分割该文件,并且逐行地将行提供给你的proc文件.