Question

I'd love to use the outdated "atoi" kind of stuff, but it turns out I don't have it in kernel space.

I have this code in the write method of my module. Writes are being controlled by me in user space, and it so happens that the user will always write something in this format:

"0.9 9.5 7.6 "

Hence - I have this code to parse it:

 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);
...

I then trigger my module by echo'ing on the procfile it creates (calling my write method) like this:

echo "0.9 9.8 3.4 " > name_of_my_proc

With 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

When printing back to the kernel...nothing performs like I'd like! My 0.9 gets killed off to 0 (is that not a legit long long value, if so that would make sense). Most importantly, my doubles don't get converted - it just prints the character %f. What can I do to make them print as I typed them?

Thanks

Was it helpful?

Solution

You are using wrong scan codes.

Scan code:  Data type:
%d          int
%ld         long
%f          float
%lf         double

Also note that long long is an integer data type and cannot store fractional numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top