/*
file i/o in kernel module
by flyduck 2001/03/21
*/#define __kernel__
#define module
#include
#include
#include
#include
#include
#include
#include
#define eof (-1)
#define seek_set 0
#define seek_cur 1
#define seek_end 2
//// function prototypes
//struct file *klib_fopen(const char *filename, int flags, int mode);
void klib_fclose(struct file *filp);
int klib_fseek(struct file *filp, int offset, int whence);
int klib_fread(char *buf, int len, struct file *filp);
int klib_fgetc(struct file *filp);
char *klib_fgets(char *str, int size, struct file *filp);
int klib_fwrite(char *buf, int len, struct file *filp);
int klib_fputc(int ch, struct file *filp);
int klib_fputs(char *str, struct file *filp);
int klib_fprintf(struct file *filp, const char *fmt, ...);
//// library functions
//// context : user
// parameter :
// filename : filename to open
// flags :
// o_rdonly, o_wronly, o_rdwr
// mode : file creation permission.
// s_ir*** s_iw*** s_i***x (*** = usr, grp, oth), s_irwxx (x = u, g, o)
// return :
// file pointer. if error, return null
struct file *klib_fopen(const char *filename, int flags, int mode)
// context : user
// parameter :
// filp : file pointer
// return :
void klib_fclose(struct file *filp)
// context : user
// parameter :
// filp : file pointer
// offset :
// whence : seek_set, seek_cur
// comment :
// do not support seek_end
// no boundary check (file position may exceed file size)
int klib_fseek(struct file *filp, int offset, int whence)
else
return -enoent;
}// context : user
// parameter :
// buf : buffer to read into
// len : number of bytes to read
// filp : file pointer
// return :
// actually read number. 0 = eof, negative = error
int klib_fread(char *buf, int len, struct file *filp)
// context : user
// parameter :
// filp : file pointer
// return :
// read character, eof if end of file
int klib_fgetc(struct file *filp)
// context : user
// parameter :
// str : string
// size : size of str buffer
// filp : file pointer
// return :
// read string. null if end of file
// comment :
char *klib_fgets(char *str, int size, struct file *filp)
}*cp = 0;
set_fs(oldfs);
return (len < 0 || readlen == 0) ? null : str;
} else
return null;
}// context : user
// parameter :
// buf : buffer containing data to write
// len : number of bytes to write
// filp : file pointer
// return :
// actually written number. 0 = retry, negative = error
int klib_fwrite(char *buf, int len, struct file *filp)
// context : user
// parameter :
// filp : file pointer
// return :
// written character, eof if error
int klib_fputc(int ch, struct file *filp)
// context : user
// parameter :
// str : string
// filp : file pointer
// return :
// count of written characters. 0 = retry, negative = error
int klib_fputs(char *str, struct file *filp)
// context : user
// parameter :
// filp : file pointer
// fmt : printf() style formatting string
// return :
// same as klib_fputs()
int klib_fprintf(struct file *filp, const char *fmt, ...)
//// test functions
//#define maxlinelen 1024
int init_module(void)
printk("# klib_file : fgetc/n");
for (i = 0; ; ++i)
printk("# klib_file : fgets/n");
klib_fseek(filp, 4450, seek_set);
klib_fgets(str, maxlinelen, filp);
printk("%s", str);
printk("# klib_file : fgets #2/n");
klib_fseek(filp, 0, seek_set);
for (line = 0; klib_fgets(str, maxlinelen, filp) != null; ++line)
printk("%04d : %s", line, str);
klib_fclose(filp);
printk("# klib_file : open for write/n");
if ((filp = klib_fopen("./tmpfile", o_creat | o_wronly | o_trunc, s_irusr |
s_iwusr)) == null)
printk("# klib_file : fputc/n");
klib_fputc('*', filp);
klib_fputc('/n', filp);
printk("# klib_file : fputs/n");
klib_fputs("hello, world/n", filp);
printk("# klib_file : fprintf/n");
klib_fprintf(filp, "write %d, %d, %d/n", 1, 2, 3);
klib_fclose(filp);
printk("unloading klib_file/n");
return 1;
}void cleanup_module(void)
linux核心檔案讀取
平時 網路部分的東西碰的多些,這塊一開始還真不知道怎麼寫,因為肯定和在 使用者空間下是不同的。google過後,得到以下答案。一般可以用兩種方法 第一種是用 系統呼叫。第二種方法是filp open 等函式。下面分別來說下這兩種方法。1 利用系統呼叫 sys open,sys write,sys r...
linux 讀取檔案
linux讀取檔案是經常要用到的操作,以下示例 說明看注釋 讀取檔案snlist.txt中的每一行內容賦給sn變數 while read sn doecho sn is sn 判斷是否是檔案 if d sn then echo sn not existed else 對sn進行 拆分並獲取最後乙個結...
Linux上讀取檔案
linux上讀取檔案的方法 bin bash this is a script fortest exec config file 1 該指令碼傳乙個檔名為引數 file no 1 echo config file exec config file 將 config file中的內容作為exec的標準...