简体   繁体   中英

Passing int to sysfs

I want to pass int to sysfs_store

What I tried

userspace

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM