如何转换的char [],在linux内核诠释? [英] How convert char[] to int in linux kernel?

查看:167
本文介绍了如何转换的char [],在linux内核诠释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换的char []在linux内核为int

与验证所输入的文本实际上是一个int?

  INT procfile_write(结构文件*文件,为const char *缓冲区,无符号长计数,
       void *的数据)
{   焦炭procfs_buffer [PROCFS_MAX_SIZE]    / *获取缓冲区大小* /
   无符号长procfs_buffer_size =计数;
   如果(procfs_buffer_size> PROCFS_MAX_SIZE){
       procfs_buffer_size = PROCFS_MAX_SIZE;
   }   / *写数据到缓冲器* /
   如果(调用copy_from_user(procfs_buffer,缓冲器,procfs_buffer_size)){
       返回-EFAULT;
   }   INT = buffer2int(procfs_buffer,procfs_buffer_size);   返回procfs_buffer_size;
}


解决方案

请参阅 kstrtol的各种各样的化身()在<一个href=\"http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=include/linux/kernel.h\"><$c$c>#include &LT;在include / linux / kernel.h方式&gt; 在友好的Linux源代码树

您需要哪一个取决于是否 *缓冲区是一个用户或内核地址,您的需要是如何严格的错误处理/检查的缓冲区内容(之类的东西,是 123qx 无效或应交回 123 ?)。

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

这篇关于如何转换的char [],在linux内核诠释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆