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

che*_*sam 7 c string module linux-kernel

我需要对写入模块的数据进行解析,并且使用string.h的strtok()函数会很有用.但是我试过了

#include <string.h>
Run Code Online (Sandbox Code Playgroud)

#include <linux/string.h>
Run Code Online (Sandbox Code Playgroud)

没有成功.这可能吗?或者我是否必须编写自己的strtok函数?

谢谢

Tim*_*fer 15

最新的内核库有这个,它可以做你需要的:

/**
 * 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. ;)
 */
Run Code Online (Sandbox Code Playgroud)


0xf*_*xfe 7

没有strtok有效的Linux内核的API中.你必须自己写.请参阅Linux Kernel API中的String Manipulation部分.

顺便说一句,我建议远离strtok(或类似的东西strtok).它不是可重入的,并且在内核代码(本质上是多线程的)中是不安全的.

如果您要复制该功能,请考虑复制strtok_r.