我很想使用过时的“atoi”之类的东西,但事实证明我在内核空间中没有它。
我的模块的 write 方法中有这段代码。写入由我在用户空间控制,碰巧用户总是会以这种格式写一些东西:
“0.9 9.5 7.6”
因此 - 我有这段代码来解析它:
ssize_t write_info( struct file *filp, const char __user *buff, unsigned long len, void *data )
{
char *seedid;
char *low_in;
char *high_in;
char *dropper;
unsigned long long seedid_var;
double d_1;
double d_2;
printk(KERN_INFO "Whaddup u writin'?\n");
dropper = kstrdup(buff, 0);
seedid = strsep(&dropper, " ");
printk("HERE IS: %s\n\n", seedid);
sscanf(seedid, "%lld", &seedid_var);
printk("AND BACK AGAIN: %lld\n\n\n", seedid_var);
low_in = strsep(&dropper, " ");
printk("HERE IS: %s\n\n", low_in);
sscanf(low_in, "%f", &d_1);
printk("AND BACK AGAIN: %f\n\n", d_1);
high_in = strsep(&dropper, " ");
printk("HERE IS: %s\n\n", high_in);
sscanf(high_in, "%f", &d_2);
printk("AND BACK AGAIN: %f\n\n", d_2);
...
然后,我通过在它创建的 procfile 上回显(调用我的 write 方法)来触发我的模块,如下所示:
echo "0.9 9.8 3.4 " > name_of_my_proc
使用 dmesg:
[ 2211.808474] Whaddup u writin'?
[ 2211.808505] HERE IS: 0.9
[ 2211.808508]
[ 2211.808514] AND BACK AGAIN: 0
[ 2211.808516]
[ 2211.808517]
[ 2211.808520] HERE IS: 9.8
[ 2211.808522]
[ 2211.808524] AND BACK AGAIN: %f
[ 2211.808526]
[ 2211.808529] HERE IS: 3.4
[ 2211.808531]
[ 2211.808533] AND BACK AGAIN: %f
当打印回内核时......没有任何表现像我想要的那样!我的 0.9 被杀死为 0(这不是一个合法的 long long 值,如果是这样的话,那是有道理的)。最重要的是,我的双打没有被转换——它只是打印字符 %f。我可以做些什么来让它们在我打字时打印出来?
谢谢