2010-02-11 148 views
6

我需要对写入我的模块的数据进行解析,并且使用string.h的strtok()函数会很有用。不过,我已经试过我可以在Linux内核模块中使用strtok()吗?

#include <string.h> 

#include <linux/string.h> 

没有成功。这可能吗?或者我将不得不写我自己的strtok功能?

感谢

回答

7

有有效的Linux内核API中没有strtok。你将不得不写你自己的。请参阅Linux Kernel API中的String Manipulation部分。

顺便说一句,我建议远离strtok(或任何strtok样)。它不是可重入的,并且在内核代码中是不安全的(这本质上是多线程的)。

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

+0

好的真棒,至少我知道我必须现在写我自己,谢谢! – cheesysam 2010-02-11 18:25:17

14

最新的内核库有这一点,这可能就是你所需要的东西:

/** 
* 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. ;) 
*/ 
相关问题