?? proc_text.c
字號:
#define __NO_VERSION#define __KERNEL__#define MODULE#include <linux/kernel.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/proc_fs.h>#include <asm/uaccess.h>#define STRINGLEN 10#define NAMELEN 10#define IDLEN 10#define KBUFLEN 50 struct person_information{ char name[NAMELEN]; char id[IDLEN]; };struct person_information global_buffer[STRINGLEN];struct proc_dir_entry *student_dir,*student_file;int rec_count=0; int proc_read_student(char *page,char **start,off_t off,int count,int *eof, void *data){ int len; MOD_INC_USE_COUNT; if(rec_count==0)//若student文件中沒有內(nèi)容 { len=sprintf(page,"there is no record!\n"); } int v; for(v=0;v<rec_count;v++)//有內(nèi)容則打印 { len=len+sprintf(page,"name is %s, id is %s \n",global_buffer[v].name, global_buffer[v].id); } MOD_DEC_USE_COUNT; return len;} int proc_write_student(struct file *file,const char *buffer,unsigned long count, void *data){ int len=count; char kbuffer[KBUFLEN]; MOD_INC_USE_COUNT; char op[5]; char name[NAMELEN]; char id[IDLEN]; copy_from_user(kbuffer,buffer,len);//將用戶區(qū)命令copy到核心緩沖區(qū) kbuffer[len]='\0'; int index=0; while(kbuffer[index]!='-') { op[index]=kbuffer[index]; index++; } op[index]='\0';//將指令類型存入op index++; int i=0; while(kbuffer[index]!='-') { name[i]=kbuffer[index]; i++; index++; } name[i]='\0';//學(xué)生姓名存入name index++; i=0; while(index<strlen(kbuffer)) { id[i]=kbuffer[index]; i++; index++; } id[i]='\0';//學(xué)生號存入id if(strcmp(op,"insert")==0)//若用戶指令為插入 { strcpy(global_buffer[rec_count].name,name); strcpy(global_buffer[rec_count].id,id); rec_count++; } else//若用戶指令為刪除 { int j; for(j=0;j<rec_count;j++) { if((strcmp(global_buffer[j].name,name)==0)&& (strcmp(global_buffer[j].id,id)==0)) break;//找到要刪除的 } while(j<rec_count)//數(shù)組內(nèi)容依次前移 { strcpy(global_buffer[j].name,global_buffer[j+1].name); strcpy(global_buffer[j].id,global_buffer[j+1].id); j++; } rec_count--; } MOD_DEC_USE_COUNT; return len;} int init_module(){ student_dir=proc_mkdir("proc_text",NULL);//建立新文件夾proc_text student_dir->owner=THIS_MODULE; student_file=create_proc_entry("student",0644,student_dir);//并生成文件student student_file->read_proc=proc_read_student; student_file->write_proc=proc_write_student; student_file->owner=THIS_MODULE; int w; for(w=0;w<STRINGLEN;w++)//初始化 { strcpy(global_buffer[w].name,"$$$$$$$$$"); strcpy(global_buffer[w].id,"$$$$$$$$$"); } return 0;}void cleanup_module()//刪除文件以及文件夾{ remove_proc_entry("student",student_dir); remove_proc_entry("proc_text",NULL);}MODULE_LICENSE("GPL");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -