簡體   English   中英

我可以在Linux內核模塊中使用strtok()嗎?

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

我需要對寫入模塊的數據進行解析,並且使用string.h的strtok()函數會很有用。 但是我試過了

#include <string.h>

#include <linux/string.h>

沒有成功。 這可能嗎? 或者我是否必須編寫自己的strtok函數?

謝謝

最新的內核庫有這個,它可以做你需要的:

/**
 * 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. ;)
 */

有效的Linux內核API中沒有strtok 你必須自己寫。 請參閱Linux Kernel API中的String Manipulation部分。

順便說一句,我建議遠離strtok (或任何類似strtok )。 它不是可重入的,並且在內核代碼(本質上是多線程的)中是不安全的。

如果您要復制該函數,請考慮復制strtok_r

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

將是你正在尋找的功能。
你可以在lxr,source / lib / string.c,第589行查找(對於版本/版本4.6)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM