Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing int to sysfs

I want to pass int to sysfs_store

What I tried

userspace

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int tid = 5234; // can be negative as well
char buf[5];
sprintf(buf, "%d\n", tid);
write (fd, buf, 1);

driver sysfs_store

int v;
kstrtoint(buf,10,&v);
pr_info("%d\n",v); // printing only first digit 5. Should print 5234

>Solution :

Since sprintf() returns bytes copied count; use it

Also switch to snprintf() to avoid buffer overflows.

int data_len = snprintf(buf, sizeof(buf), "%d\n", tid);
write (fd, buf, data_len);

It’s always better to have bigger buffers to cover all your scenarios.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading