今天用ubuntu的"Terminal"上ptt,卻出現亂碼,原來是"Character Encoding"預設是"UTF-8",將其改成"BIG5"就OK啦,post文章中文也不成問題了。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
#define MAX_LINE 1000
static uint32_t *lines;
/**
* seq_start() takes a position as an argument and returns an iterator which
* will start reading at that position.
*/
static void* seq_start(struct seq_file *s, loff_t *pos)
{
uint32_t *lines;
if (*pos >= MAX_LINE) {
return NULL; // no more data to read
}
lines = kzalloc(sizeof(uint32_t), GFP_KERNEL);
if (!lines) {
return NULL;
}
*lines = *pos + 1;
return lines;
}
/**
* move the iterator forward to the next position in the sequence
*/
static void* seq_next(struct seq_file *s, void *v, loff_t *pos)
{
uint32_t *lines = v;
*pos = ++(*lines);
if (*pos >= MAX_LINE) {
return NULL; // no more data to read
}
return lines;
}
/**
* stop() is called when iteration is complete (clean up)
*/
static void seq_stop(struct seq_file *s, void *v)
{
kfree(v);
}
/**
* success return 0, otherwise return error code
*/
static int seq_show(struct seq_file *s, void *v)
{
seq_printf(s, "Line #%d: This is Brook's demo\n", *((uint32_t*)v));
return 0;
}
static struct seq_operations seq_ops = {
.start = seq_start,
.next = seq_next,
.stop = seq_stop,
.show = seq_show
};
static int proc_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_ops);
}
static struct file_operations proc_ops = {
.owner = THIS_MODULE, // system
.open = proc_open,
.read = seq_read, // system
.llseek = seq_lseek, // system
.release = seq_release // system
};
static int __init init_modules(void)
{
struct proc_dir_entry *ent;
ent = create_proc_entry("brook", 0, NULL);
if (ent) {
ent->proc_fops = &proc_ops;
}
return 0;
}
static void __exit exit_modules(void)
{
if (lines) {
kfree(lines);
}
remove_proc_entry("brook", NULL);
}
module_init(init_modules);
module_exit(exit_modules);
static struct seq_operations seq_ops = {
.start = seq_start,
.next = seq_next,
.stop = seq_stop,
.show = seq_show
};
static int proc_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_ops);
}
static struct file_operations proc_ops = {
.owner = THIS_MODULE, // system
.open = proc_open,
.read = seq_read, // system
.llseek = seq_lseek, // system
.release = seq_release // system
};
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
MODULE_LICENSE("GPL");
#define MAX_LINE 1000
/* read file operations */
static int read_proc(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
uint32_t curline;
char *p, *np;
*start = p = np = page;
curline = *(uint32_t*) data;
printk("count=%d, off=%ld\n", count, off);
for (;curline < MAX_LINE && (p - page) < count; curline++) {
np += sprintf(p, "Line #%d: This is Brook's demo\n", curline);
if ((count - (np - page)) < (np - p)) {
break;
}
p = np;
}
if (curline < MAX_LINE) {
*eof = 1;
}
*(uint32_t*)data = curline;
return (p - page);
}
/* private data */
static uint32_t *lines;
static int __init init_modules(void)
{
struct proc_dir_entry *ent;
lines = kzalloc(sizeof(uint32_t), GFP_KERNEL);
if (!lines) {
printk("no mem\n");
return -ENOMEM;
}
*lines = 0;
/* create a procfs entry for read-only */
ent = create_proc_read_entry ("brook", S_IRUGO, NULL, read_proc, lines);
if (!ent) {
printk("create proc failed\n");
kfree(lines);
}
return 0;
}
static void __exit exit_modules(void)
{
if (lines) {
kfree(lines);
}
/* remove procfs entry */
remove_proc_entry("brook", NULL);
}
module_init(init_modules);
module_exit(exit_modules);
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
#define MAX_LINE 1000
/* write file operations */
static int write_proc(struct file *file, const char __user *buf,
unsigned long count, void *data)
{
char num[10], *p;
int nr, len;
/* no data be written */
if (!count) {
printk("count is 0\n");
return 0;
}
/* Input size is too large to write our buffer(num) */
if (count > (sizeof(num) - 1)) {
printk("input is too large\n");
return -EINVAL;
}
if (copy_from_user(num, buf, count)) {
printk("copy from user failed\n");
return -EFAULT;
}
/* atoi() */
p = num;
len = count;
nr = 0;
do {
unsigned int c = *p - '0';
if (*p == '\n') {
break;
}
if (c > 9) {
printk("%c is not digital\n", *p);
return -EINVAL;
}
nr = nr * 10 + c;
p++;
} while (--len);
*(uint32_t*) data = nr;
return count;
}
static int read_proc(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
uint32_t curline;
char *p, *np;
*start = p = np = page;
curline = *(uint32_t*) data;
printk("count=%d, off=%ld\n", count, off);
for (;curline < MAX_LINE && (p - page) < count; curline++) {
np += sprintf(p, "Line #%d: This is Brook's demo\n", curline);
if ((count - (np - page)) < (np - p)) {
break;
}
p = np;
}
if (curline < MAX_LINE) {
*eof = 1;
}
*(uint32_t*)data = curline;
return (p - page);
}
static uint32_t *lines;
static int __init init_modules(void)
{
struct proc_dir_entry *ent;
lines = kzalloc(sizeof(uint32_t), GFP_KERNEL);
if (!lines) {
printk("no mem\n");
return -ENOMEM;
}
*lines = 0;
ent = create_proc_entry("brook", S_IFREG | S_IRWXU, NULL);
if (!ent) {
printk("create proc failed\n");
kfree(lines);
} else {
ent->write_proc = write_proc;
ent->read_proc = read_proc;
ent->data = lines;
}
return 0;
}
static void __exit exit_modules(void)
{
if (lines) {
kfree(lines);
}
remove_proc_entry("brook", NULL);
}
module_init(init_modules);
module_exit(exit_modules);