In the post , we saw how to use read integers using the get_option, if we want to read a range of integers we can use get_options.
Ref: Kernel API
To read the numbers that are passed,as it is a range we will use an array into which the numbers can be filled. The array will have to be declared with the appropriate size.
The array will be passed to get_options which will fill the array with the elements that belong the range that was passed as shown in the function below.
We can pass a range of integers seperated by a "-" , which will be used to fill the array with the elements.
The following is the full code that passes range of numbers to the getargs function shown above,which in turn prints out all the numbers in the range.
Save the file as readOptions.c and compile it using the following makefile.
Compile and insert the code using
To view the output, run the command dmesg.
Ref: Kernel API
To read the numbers that are passed,as it is a range we will use an array into which the numbers can be filled. The array will have to be declared with the appropriate size.
The array will be passed to get_options which will fill the array with the elements that belong the range that was passed as shown in the function below.
We can pass a range of integers seperated by a "-" , which will be used to fill the array with the elements.
The following is the full code that passes range of numbers to the getargs function shown above,which in turn prints out all the numbers in the range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include<linux/sched.h> | |
int pint=-1; | |
static __init int getargs(char *str){ | |
char *ret; | |
int i=0,num; | |
int pint[5]; | |
ret=get_options(str,ARRAY_SIZE(pint),pint); | |
for(i=0;i<ARRAY_SIZE(pint);i++) | |
printk(KERN_INFO "%d\n",pint[i]); | |
return 0; | |
} | |
static __init int proc_init (void) { | |
getargs("1-6"); | |
return 0; | |
} | |
void proc_cleanup(void) { | |
} | |
MODULE_LICENSE("GPL"); | |
module_init(proc_init); | |
module_exit(proc_cleanup); |
Save the file as readOptions.c and compile it using the following makefile.
Compile and insert the code using
To view the output, run the command dmesg.
No comments:
Post a Comment