Can I use strtok () in a Linux kernel module?

I need to parse the data written to my module, and using the strtok () function for string.h would be useful. However i tried

#include <string.h> 

and

 #include <linux/string.h> 

without success. Is it possible? Or will I have to write my own strtok function?

thanks

+6
c string module linux-kernel
source share
3 answers

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

By the way, I would suggest staying away from strtok (or something strtok -like). It is not reentrant and unsafe in kernel code (which is inherently multithreaded).

If you are going to duplicate a function, consider duplicating strtok_r .

+7
source share

The latest kernel library has this, which can 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. ;) */ 
+14
source share

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

will be the function you are looking for.
You can see it in lxr, source / lib / string.c, line 589 (for version / version 4.6)

0
source share

All Articles