How convert char[] to int in linux kernel
with validation that the text entered is actually an int?
int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) { char procfs_buffer[PROCFS_MAX_SIZE]; /* get buffer size */ unsigned long procfs_buffer_size = count; if (procfs_buffer_size > PROCFS_MAX_SIZE ) { procfs_buffer_size = PROCFS_MAX_SIZE; } /* write data to the buffer */ if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) { return -EFAULT; } int = buffer2int(procfs_buffer, procfs_buffer_size); return procfs_buffer_size; }
See the various incarnations of kstrtol()
in #include <include/linux/kernel.h>
in your friendly linux source tree.
Which one you need depends on whether the *buffer
is a user or a kernel address, and on how strict your needs on error handling / checking of the buffer contents are (things like, is 123qx
invalid or should it return 123
?).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With