Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How convert a char[] string to int in the Linux kernel?

Tags:

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; } 
like image 809
caeycae Avatar asked May 26 '11 13:05

caeycae


1 Answers

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

like image 164
FrankH. Avatar answered Oct 25 '22 17:10

FrankH.