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
*bufferis a user or a kernel address, and on how strict your needs on error handling / checking of the buffer contents are (things like, is123qxinvalid or should it return123?).