Hello,
I'm trying to write a linux module that can be able to read a file line by line and stock each line in
a corresponding column of an array.
For example if my file contains those following lines:
1
10
20
,I would like to read this file with kernel functions like kernel_read(...) and insert
each line in one corresponding column of my array such as tab[0] = 1, tab[1] = 10 and tab[2] = 20;
When tab[3] is the array to receive the file's content.
I have already this code but I get the content of line character by character no all the line at same time.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/string.h>
MODULE_LICENSE("GPL");
char *tab; //the table to contain each line the file
char tmp[10];
static int i=0;
static void read_file(char *filename)
{
struct file *f = NULL;
int fd;
char buf[1];
loff_t f_pos = 0;
mm_segment_t old_fs = get_fs();
tab = kmalloc(15, GFP_KERNEL);
set_fs(KERNEL_DS);
f = filp_open(filename, O_RDONLY, 0);
if (!IS_ERR(f)) {
printk(KERN_DEBUG);
while (kernel_read(f, buf, 1, &f_pos) == 1){
printk("%c", buf[0]);
if(buf[0]!='\n'){
tab[i]=buf[0];
printk("tab[%d] = %c", i, tab[i]);
i++;
}
}
printk("\n");
//sys_close(fd);
filp_close(f, NULL);
}
set_fs(old_fs);
}
static int __init init(void)
{
read_file("myfile.txt");
return 0;
}
static void __exit exit(void)
{ }
module_init(init);
module_exit(exit);
The above code give me character by character on each line. For example if the value of the line is 10, I can not get 10 but 1 and after 0;
I can do it with C language standard functions in user space.
As it is no able to use them in kernel space, How can I do to be able to insert the content of each lines in columns of the receiving array?
Can help me, please ?
I already know that reading file from kernel is not recommended but I need to do this for some tests.