Linux内核中如何将char[]字符串转换为int?
如何在linux内核中将char[]转换为int
并验证输入的文本实际上是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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅
#include
在您友好的 Linux 源代码树中。您需要哪一个取决于 *buffer 是用户地址还是内核地址,以及您对错误处理/检查缓冲区内容的需求有多严格(例如,是 123qx 无效还是应该返回
123
?)。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, is123qx
invalid or should it return123
?).最小可运行
kstrtoull_from_user
debugfs
示例kstrto*_from_user
系列在处理用户数据时非常方便。kstrto.c:
用法:
Dmesg 输出:
在 Linux 内核 4.16 中测试 这个 QEMU + Buildroot设置。
对于这个特定的示例,您可能想要使用
debugfs_create_u32
代替。Minimal runnable
kstrtoull_from_user
debugfs
exampleThe
kstrto*_from_user
family is very convenient when dealing with user data.kstrto.c:
Usage:
Dmesg outputs:
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.由于Linux内核中没有许多常用函数/宏,因此您无法使用任何直接函数从字符串缓冲区中获取整数值。
这是我长期以来一直使用的代码,它可以用于所有 *NIX 风格(可能无需任何修改)。
这是代码的修改形式,我很久以前就在一个开源项目中使用过它(现在不记得名字了)。
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).
您可以使用
strtoul
或strtol
。以下是手册页的链接: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
You could use
strtoul
orstrtol
. 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
我使用 sscanf() (内核版本)从字符串流中进行扫描,它适用于 2.6.39-gentoo-r3。坦率地说,我永远无法让 simple_strtol() 在内核中工作——我目前正在弄清楚为什么这在我的机器上不起作用。
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.
使用atoi和isdigit(注意isdigit只接受一个字符)。
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
Use atoi and isdigit (note isdigit just takes a char).
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/