How to extract integer from string in Linux kernel space?

integer to string linux kernel
kstrtoint
kstrtoull_from_user

I want to extract integers from string, e.g. I want to extract 0 as group identifier and 1 as mdev identifier from string "/dev/nkdev/group0/mdev1".

In the above string "/dev/nkdev/group" is a fixed pattern in input, group id may be any integer between 0-9 and "mdev" is also not fixed, it may be any character sequence followed by an integer.

I used following code

#define NO_INTEGER "%0[^0-9]"

res = sscanf(dev_name_p, NO_INTEGER "%u" NO_INTEGER "%u",
             &group_id, &mdev_id);

But I found skip sequence is not supported in kernel. Can any body tell, if there is any kernel library function to do the job?

A format string like I use in this example should work:

#include <stdio.h>

int main(void)
{
        unsigned a = 38, b = 38; // Default magic value
        int n = sscanf("/dev/nkdev/group0/abc1", "/dev/nkdev/group%u/%*[a-z]%u", &a, &b);

        printf("n: %i, a: %u, b: %u\n", n, a, b);

        return 0;
}

It yields this output:

n: 2, a: 0, b: 1

c: How to extract integer from string in Linux kernel space?, I want to extract integers from string, e.g. I want to extract 0 as group identifier and 1 as mdev identifier from string. Basic Kernel Library Functions Next: Command-line Parsing. get_option — Parse integer from an option string

kstrtoint() — convert a string to an int.

kstrtol() — convert a string to a long.

You check declaration in linux kernel soruce tree /include/linux/kernel.h.you will have to include this header file #include< linux/kernel.h> before using this fuction.

 Example :

    const char *buf = "123";
    long value;
    if(kstrtoint(buf, 10, &value) != 0)
         return -1;
     printk("%d",value);

String Manipulation, strncasecmp — Case insensitive, length-limited string comparison; strcpy in a length limited string; skip_spaces — Removes leading whitespace from str  It does't work in Linux kernel space as '%*[a-z]' sequence formatting is not supported in Kernel space. I used similar approach in my code which works fine with libc but doesn't work in kernel space. – iGRJ Jun 23 '16 at 5:00

This solution runs in userspace, but all functions used here are available in kernel space with the same result

Dirty, but it will work:

#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    char string[] = "/dev/nkdev/group4/mdev12";
    char *stringp = (char *)&string;
    const char delimiters[] = "/";
    char *token;
    int num1, num2;

    token = strsep (&stringp, delimiters);        /* token => "" */
    token = strsep (&stringp, delimiters);        /* token => "dev" */
    token = strsep (&stringp, delimiters);        /* token => "nkdev" */
    token = strsep (&stringp, delimiters);        /* token => "group0" */
    while (*token != 0 && (*token < '0' || *token > '9'))
        token++;
    num1 = strtol(token, NULL, 10);
    token = strsep (&stringp, delimiters);        /* token => "mdev1" */
    while (*token != 0 && (*token < '0' || *token > '9'))
        token++;
    num2 = strtol(token, NULL, 10);
    printf("num1: %d\nnum2: %d\n", num1, num2);
}

kstrtol, kstrtol — convert a string to a long. Synopsis. int kstrtol (, const char * s , The string must be null-terminated, and may also include a single newline before its  sysfs_streq — return true if strings are equal, modulo trailing newline. match_string — matches given string in an array. memset — Fill a region of memory with the given value. memzero_explicit — Fill a region of memory (e.g. sensitive keying data) with 0s.

atoi(), where whitespace consists of the same characters for which the isspace() function is true, such as spaces and tabs. The atoi() function ignores leading white-space  Itoa and atoi functions in kernel space (too old to reply) to extract tokenized substrings for a given instead, available in <linux/string.h>.

Experimental Algorithms: 10th International Symposium, SEA 2011, , at 3.16 GHz, with 8 GB of main memory and 6 MB of cache, running Linux kernel 2:6:24-28. For extract we queried 10,000 random numbers between 1 and n. HashBB performs better in space than HashB when the strings are short​,  Converting integer to String Hi everyone, I would like to know how to convert an integer to a string. for instance if i=1 i would like to creat a variable called constant1. i want to do this in a for loop so for each value of i, i create a new variable such as constant2, constant3, and so on. for i in 1 2 3 do

Major and Minor Numbers, Major and Minor Numbers Char devices are accessed through names in the filesystem. The kernel uses the major number at open time to dispatch execution to the (module) initialization by calling the following function, defined in <linux/fs.h> : int the function can extract the device number by looking at inode->i_rdev . Pointers printed without a specifier extension (i.e unadorned %p) are hashed to prevent leaking information about the kernel memory layout. This has the added benefit of providing a unique identifier. On 64-bit machines the first 32 bits are zeroed. The kernel will print (ptrval) until it gathers enough entropy. If you really want the address

Comments
  • Isn't it possible to use the format /dev/nkdev/group%u/mdev%u?
  • Thanks, It could have been possible if mdev was a fixed pattern, In my case mdev is also not a fixed. it may be any character sequence.
  • Then what about /dev/nkdev/group%u/%*s%u?
  • @bzeaman this should be the accepted answer, you should post as one!
  • @IshayPeled It didn't work, I later on discovered. The %*s also includes the digit following the mdev part. In his question he states 'skip sequences' are not supported, and I think he means formats like %*[a-z] with that. I posted it as an answer anyway, it may help others.
  • This should be the accepted answer, quick, simple and works on all cases
  • It does't work in Linux kernel space as '%*[a-z]' sequence formatting is not supported in Kernel space. I used similar approach in my code which works fine with libc but doesn't work in kernel space.
  • This will not work for strings that do not consist exclusively of digits, test your solution with "/dev/nkdev/group1/mdev2"
  • This indeed does not work, and does not answer the question.
  • Dear down-voter, I'd love your input on what is wrong with this solution?
  • @AndyShevchenko Very constructive, flagged
  • I don't know what can I do more to make you read the question before answering.