2010-02-11 64 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. ;) 
*/