projects
/
cascardo
/
kernel
/
samples
/
02.char
/
.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Control size of buffer.
[cascardo/kernel/samples/02.char/.git]
/
helloc.c
diff --git
a/helloc.c
b/helloc.c
index
f43a767
..
9f983a2
100644
(file)
--- a/
helloc.c
+++ b/
helloc.c
@@
-31,6
+31,14
@@
static int helloc_open(struct inode *ino, struct file *filp)
return -ENODEV;
msg = hello_message[iminor(ino)];
filp->private_data = msg;
return -ENODEV;
msg = hello_message[iminor(ino)];
filp->private_data = msg;
+ if (filp->f_flags & O_TRUNC) {
+ memset(msg->text, 0, msg->len);
+ msg->len = 0;
+ printk(KERN_INFO "File opened with truncate flags\n");
+ }
+ if (filp->f_flags & O_APPEND) {
+ filp->f_pos = msg->len;
+ }
return 0;
}
return 0;
}
@@
-67,18
+75,27
@@
static ssize_t helloc_write(struct file *filp, const char __user *buf,
int r;
struct message *msg = filp->private_data;
/* do not read pass through the size of the message */
int r;
struct message *msg = filp->private_data;
/* do not read pass through the size of the message */
- if (*pos >=
msg->len
)
+ if (*pos >=
BUFFER_SIZE
)
/* return end of file */
return 0;
/* if len is bigger than the rest of the message, clamp it */
/* return end of file */
return 0;
/* if len is bigger than the rest of the message, clamp it */
- if (len > msg->len - *pos)
- len = msg->len - *pos;
+ if (len > BUFFER_SIZE - *pos)
+ len = BUFFER_SIZE - *pos;
+
/* copy message to user space and return error if it fails */
r = copy_from_user(msg->text + *pos, buf, len);
/* copy message to user space and return error if it fails */
r = copy_from_user(msg->text + *pos, buf, len);
- if (r)
+ if (r) {
+ if (*pos + len > msg->len)
+ memset(msg->text + msg->len, 0, *pos + len - msg->len);
return -EFAULT;
return -EFAULT;
+ }
/* update the file position */
*pos += len;
/* update the file position */
*pos += len;
+ if (msg->len < *pos) {
+ printk(KERN_DEBUG "Updating size from %d to %lld\n", msg->len, *pos);
+ msg->len = *pos;
+ }
+
return len;
}
return len;
}