如何在Linux内核中将char []字符串转换为int? [英] How convert a char[] string to int in the Linux kernel?

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

问题描述

如何在Linux内核中将char []转换为int

How convert char[] to int in linux kernel

是否已验证输入的文本实际上是一个int?

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;
}

推荐答案

您需要哪个取决于*buffer是用户还是内核地址,以及您对错误处理/检查缓冲区内容的要求有多严格(例如123qx是无效的还是应该?返回123吗?).

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

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

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