Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1072062
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2008-03-18 10:28:11

#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/fs.h>

static int array[5] = {0, 1, 2, 3, 4};

#define PROC_NAME "myproc"

static int myproc_open(struct inode *inodp, struct file *filp);

static struct file_operations myproc_fops = {
        .owner = THIS_MODULE,
        .open = myproc_open,
        .read = seq_read,
        .llseek = seq_lseek,
        .release = seq_release,
};

static void * myproc_seq_start(struct seq_file *m, loff_t *pos)
{
        if (*pos >= 5)
                return NULL;

        return array + *pos;
}

static void * myproc_seq_next(struct seq_file *m, void *v, loff_t *pos)
{
        int *p;

        p = (int *) v;
        (*pos)++;
        if (*pos >= 5)
                return NULL;

        return array + *pos;
}

static void myproc_seq_stop(struct seq_file *m, void *v)
{
        return;
}

static int myproc_seq_show(struct seq_file *m, void *v)
{
        int *p;

        p = (int *) v;
        seq_printf(m, "%d\n", *p);

        return 0;
}

static struct seq_operations myproc_seq_ops = {
        .start = myproc_seq_start,
        .next = myproc_seq_next,
        .stop = myproc_seq_stop,
        .show = myproc_seq_show,
};

static int myproc_open(struct inode *inodp, struct file *filp)
{
        return seq_open(filp, &myproc_seq_ops);
}

static int __init myproc_init(void)
{
        struct proc_dir_entry *entry;

        entry = create_proc_entry(PROC_NAME, 0, NULL);
        if (entry)
                entry->proc_fops = &myproc_fops;

        return 0;
}

static void __exit myproc_exit(void)
{
        remove_proc_entry(PROC_NAME, NULL);
}

MODULE_LICENSE("GPL");

module_init(myproc_init);
module_exit(myproc_exit);

阅读(641) | 评论(0) | 转发(0) |
0

上一篇:seq_file用法

下一篇:环形缓冲区

给主人留下些什么吧!~~