How convert a char[] string to int in the Linux kernel?
How convert char[] to int in linux kernel
with validation 开发者_如何学Cthat 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
?).
Minimal runnable kstrtoull_from_user
debugfs
example
The kstrto*_from_user
family is very convenient when dealing with user data.
kstrto.c:
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *toplevel_file;
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
int ret;
unsigned long long res;
ret = kstrtoull_from_user(buf, len, 10, &res);
if (ret) {
/* Negative error code. */
pr_info("ko = %d\n", ret);
return ret;
} else {
pr_info("ok = %llu\n", res);
*off= len;
return len;
}
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.write = write,
};
static int myinit(void)
{
toplevel_file = debugfs_create_file("lkmc_kstrto", S_IWUSR, NULL, NULL, &fops);
if (!toplevel_file) {
return -1;
}
return 0;
}
static void myexit(void)
{
debugfs_remove(toplevel_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");
Usage:
insmod kstrto.ko
cd /sys/kernel/debug
echo 1234 > lkmc_kstrto
echo foobar > lkmc_kstrto
Dmesg outputs:
ok = 1234
ko = -22
Tested in Linux kernel 4.16 with this QEMU + Buildroot setup.
For this particular example, you might have wanted to use debugfs_create_u32
instead.
Because of the unavailability of a lot of common function/macros in linux kernel, you can not use any direct function to get integer value from a string buffer.
This is the code that I have been using for a long time for doing this and it can be used on all *NIX flavors (probably without any modification).
This is the modified form of code, which I used a long time back from an open source project (don't remember the name now).
#define ISSPACE(c) ((c) == ' ' || ((c) >= '\t' && (c) <= '\r'))
#define ISASCII(c) (((c) & ~0x7f) == 0)
#define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
#define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
#define ISALPHA(c) (ISUPPER(c) || ISLOWER(c))
#define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
unsigned long mystr_toul (
char* nstr,
char** endptr,
int base)
{
#if !(defined(__KERNEL__))
return strtoul (nstr, endptr, base); /* user mode */
#else
char* s = nstr;
unsigned long acc;
unsigned char c;
unsigned long cutoff;
int neg = 0, any, cutlim;
do
{
c = *s++;
} while (ISSPACE(c));
if (c == '-')
{
neg = 1;
c = *s++;
}
else if (c == '+')
c = *s++;
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X'))
{
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0; ; c = *s++)
{
if (!ISASCII(c))
break;
if (ISDIGIT(c))
c -= '0';
else if (ISALPHA(c))
c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else
{
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0)
{
acc = INT_MAX;
}
else if (neg)
acc = -acc;
if (endptr != 0)
*((const char **)endptr) = any ? s - 1 : nstr;
return (acc);
#endif
}
You could use strtoul
or strtol
. Here's a link to the man pages:
http://www.kernel.org/doc/man-pages/online/pages/man3/strtoul.3.html
http://www.kernel.org/doc/man-pages/online/pages/man3/strtol.3.html
I use sscanf() (the kernel version) to scan from a string stream, and it works on 2.6.39-gentoo-r3. Frankly, I could never get simple_strtol() to work in the kernel--I am currently figuring out why this doesn't work on my box.
...
memcpy(bufActual, calc_buffer, calc_buffer_size);
/* a = simple_strtol(bufActual, NULL, 10); */ // Could not get this to work
sscanf(bufActual, "%c%ld", &opr, &a); // places '+' in opr and a=20 for bufActual = "20+\0"
...
Use atoi and isdigit (note isdigit just takes a char). http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
精彩评论