/* * fib-module.c * */ #include #include #include #include static unsigned long long fibonacci(unsigned int n) { unsigned long long h,i,j,k,t; h = i = 1; j = k = 0; while (n > 0) { if (n&1) { t = j*h; j = i*h + j*k + t; i = i*k + t; } t = h*h; h = 2*k*h + t; k = k*k + t; n /= 2; } return j; } void *fib_start(struct seq_file *m, loff_t *pos) { return pos; } void fib_stop(struct seq_file *m, void *v) { } void *fib_next(struct seq_file *m, void *v, loff_t *pos) { if (++(*pos) >= 94) return NULL; return pos; } int fib_show(struct seq_file *m, void *v) { loff_t pos = * (loff_t *) v; if (pos >= 0 && pos < 94) seq_printf(m, "%llu\n", fibonacci(pos)); return 0; } struct seq_operations fib_op = { .start = fib_start, .stop = fib_stop, .next = fib_next, .show = fib_show }; static int fib_open(struct inode *inode, struct file *file) { return seq_open(file, &fib_op); } static struct file_operations proc_fib_operations = { .open = fib_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release, }; static int __init fib_init(void) { struct proc_dir_entry *proc_fib; proc_fib = create_proc_entry("fibonacci", 0666, NULL); if (!proc_fib) return 1; proc_fib->proc_fops = &proc_fib_operations; return 0; } static void __exit fib_exit(void) { remove_proc_entry("fibonacci", 0); } module_init(fib_init); module_exit(fib_exit); MODULE_LICENSE("GPL");