简体   繁体   English

我可以在Linux内核模块中使用strtok()吗?

[英]Can I use strtok() in a Linux Kernel Module?

I need to do a parse on the data written to my module, and the use of the strtok() function of string.h would be useful. 我需要对写入模块的数据进行解析,并且使用string.h的strtok()函数会很有用。 However I've tried 但是我试过了

#include <string.h>

and

#include <linux/string.h>

with no success. 没有成功。 Is this possible? 这可能吗? Or will I have to write my own strtok function? 或者我是否必须编写自己的strtok函数?

Thanks 谢谢

The latest kernel library has this, which may do what you need: 最新的内核库有这个,它可以做你需要的:

/**
 * strsep - Split a string into tokens
 * @s: The string to be searched
 * @ct: The characters to search for
 *
 * strsep() updates @s to point after the token, ready for the next call.
 *
 * It returns empty tokens, too, behaving exactly like the libc function
 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
 * Same semantics, slimmer shape. ;)
 */

There is no strtok in the valid Linux Kernel API. 有效的Linux内核API中没有strtok You will have to write your own. 你必须自己写。 See the section String Manipulation in the Linux Kernel API. 请参阅Linux Kernel API中的String Manipulation部分。

BTW, I would suggest staying away from strtok (or anything strtok -like). 顺便说一句,我建议远离strtok (或任何类似strtok )。 It's not reentrant and is unsafe in kernel code (which is inherently multithreaded). 它不是可重入的,并且在内核代码(本质上是多线程的)中是不安全的。

If you're going to duplicate the function, consider duplicating strtok_r . 如果您要复制该函数,请考虑复制strtok_r

char *strsep(char **s, const char *ct) char * strsep(char ** s,const char * ct)

would be the function that you are looking for. 将是你正在寻找的功能。
You can look it up in lxr, source/lib/string.c, line 589 (for version/release 4.6) 你可以在lxr,source / lib / string.c,第589行查找(对于版本/版本4.6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM