Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
//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);
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".
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.