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

前端 未结 3 425
暖寄归人
暖寄归人 2020-12-18 02:02

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. However I\'ve tried

#include 

        
相关标签:
3条回答
  • 2020-12-18 02:28

    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)

    0 讨论(0)
  • 2020-12-18 02:34

    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. ;)
     */
    
    0 讨论(0)
  • 2020-12-18 02:40

    There is no strtok in the valid Linux Kernel API. You will have to write your own. See the section String Manipulation in the Linux Kernel API.

    BTW, I would suggest staying away from strtok (or anything strtok-like). 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.

    0 讨论(0)
提交回复
热议问题