LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 12-01-2019, 12:53 PM   #1
ajeet_singh
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Rep: Reputation: Disabled
Angry Linux kernel module to comapre strings of 2 files


I want to write kernel module in C language using which I can compare strings available inside two different files.
 
Old 12-01-2019, 01:01 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Hi, and Welcome here!

ok. And what is your problem? Why did you post it here?
 
1 members found this post helpful.
Old 12-02-2019, 12:18 AM   #3
ajeet_singh
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Lightbulb

I need C code for this. Since I am doing C code for kernel linux module, it gets difficult because syntax are changed. I am facing problem at every step so I want a fresh new code for this.
 
Old 12-02-2019, 12:49 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
I would rather suggest you to post your code and we can try to understand it and fix the problems. Also you can explain the difficulties you have. What syntax has been changed?
 
Old 12-02-2019, 01:27 AM   #5
ajeet_singh
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Exclamation Code with errors

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
//#include <linux/stdio.h>
//#include <stdlib.h>
//#include<linux/cdev.h>
//#include <linux/uaccess.h>
//#include <stdio.h>
//#include <linux/proc_fs.h>

//Maximum size of the array
#define MAX_SIZE 200
typedef struct
{
int pos;
int line;
} sMismatchingPos;
ssize_t proc_read(const FILE *fp1, const FILE *fp2,sMismatchingPos *psMismatchPos)
{
// pos and line to track of position of mismatch
int pos = 0, line = 1;
int ch1 =0, ch2 = 0;
int isContentMatch = 0;
// iterate loop until EOF
do
{
//fetch character of both file
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
++pos;
// if both variable encounters new
// line then line variable is incremented
// and pos variable is set to 0
if ((ch1 == '\n') && (ch2 == '\n'))
{
++line;
pos = 0;
}
//update structure variable
psMismatchPos->pos = pos;
psMismatchPos->line = line;
// if fetched data is not equal then
// set the mismatched flag
if(ch1!= ch2)
{
isContentMatch =1;
break;
}
}
while (ch1 != eof && ch2 != eof);
//return flag status
return isContentMatch;
}

int init_close_module()
{
printk(" Kernel Panic \n");
return 0;
}



int init_module()
{
//file pointers
FILE *fp1 = NULL;
FILE *fp2 = NULL;
//structure variable
sMismatchingPos misMatchPos = {0};
int isContentMatch = 0;
// opening both file in read only mode
fp1 = fopen("aticleworld1.txt", "r");
fp2 = fopen("aticleworld2.txt", "r");
//checking file open or not
if (fp1 == NULL || fp2 == NULL)
{
printk("Error : Files not open");
exit(1);
}
//if 1, then file mismatch
isContentMatch = proc_read(fp1, fp2,&misMatchPos);
if(isContentMatch)
{


{
init_close_module();
}

printk("Both files are different\n");

//print line and pos where both file mismatch
// printf("Line Number : %d \n",misMatchPos.line);
// printf("Position : %d \n",misMatchPos.pos);
}
else
{
printk("Both files are same\n");
}
// closing both file
fclose(fp1);
fclose(fp2);
return 0;
}


Above is my code.


Here I am getting problems in header files.

Errors which I am getting is :
1- Unknown type name FILE
2- implicit declaration of function ‘fgetc’ [-Werror=implicit-function-declaration]
ch1 = fgetc(fp1);
3- ‘eof’ undeclared (first use in this function)
while (ch1 != eof && ch2 != eof);

4- implicit declaration of function ‘fopen’ [-Werror=implicit-function-declaration]
fp1 = fopen("aticleworld1.txt", "r");

5- assignment makes pointer from integer without a cast [enabled by default]
fp1 = fopen("aticleworld1.txt", "r");

6- implicit declaration of function ‘exit’ [-Werror=implicit-function-declaration]
exit(1);
 
Old 12-02-2019, 01:29 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Thanks. Please use code tags if you wish to post code. That will keep the original formatting.
 
Old 12-02-2019, 01:31 AM   #7
ajeet_singh
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Exclamation

I am unable to do so thats why I posted like this.
 
Old 12-02-2019, 02:34 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
[code]here comes your code[/code]
that's all.
 
Old 12-02-2019, 02:39 AM   #9
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by ajeet_singh View Post
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
//#include <linux/stdio.h>
//#include <stdlib.h>
//#include<linux/cdev.h>
//#include <linux/uaccess.h>
//#include <stdio.h>
//#include <linux/proc_fs.h>
...

2- implicit declaration of function ‘fgetc’ [-Werror=implicit-function-declaration]
ch1 = fgetc(fp1);
3- ‘eof’ undeclared (first use in this function)
while (ch1 != eof && ch2 != eof);

4- implicit declaration of function ‘fopen’ [-Werror=implicit-function-declaration]
fp1 = fopen("aticleworld1.txt", "r");

5- assignment makes pointer from integer without a cast [enabled by default]
fp1 = fopen("aticleworld1.txt", "r");

6- implicit declaration of function ‘exit’ [-Werror=implicit-function-declaration]
exit(1);
At least a couple of the "implicit declaration" errors are because you have the <stdio.h> header commented out above. And "eof" should be in upper case letters, like "EOF".
 
Old 12-02-2019, 02:44 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
why did you comment out those include lines?
 
Old 12-02-2019, 03:16 AM   #11
ajeet_singh
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Exclamation

stdio.h is not working....kernel module doesn't support stdio.h .

I commented other includes because they are also not working, showing that it doesn't exists.
 
Old 12-02-2019, 06:39 AM   #12
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I don't think you can use system calls in the kernel, and fopen, fgetc etc. eventually make system calls like open(2) or read(2).

While I don't know how to write kernel code, it's obvious to me that you need to start learning at the beginning. I suggest you find documentation on Linux driver writing or kernel hacking.

By the way, it puzzles me why you need a kernel module to compare file contents. This is normally done in user mode, and programs like diff do that for you already.
 
1 members found this post helpful.
Old 12-02-2019, 07:02 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
It's not so much the syscalls, but the unavailability of [g]libc.
Crazy idea.
 
Old 12-02-2019, 07:47 AM   #14
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
This should get you started: https://kernelnewbies.org/KernelHacking
 
Old 12-03-2019, 10:26 PM   #15
ajeet_singh
LQ Newbie
 
Registered: Dec 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thanks alot everyone.
Mainly the problem lies with the header file.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replace strings in a file using strings from another file xpto09 Linux - Newbie 3 01-28-2016 06:11 PM
BASH: replace strings in on file by the strings in another one cristalp Programming 5 10-28-2011 09:47 AM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
How do I create the netfilter ip-strings module for a linux linux-2.6.18.2-34 kernel? jeffn Linux - Security 1 01-30-2008 06:39 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration