- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main(int argc,char** argv)
{
char *buf=malloc(atoi(argv[2]));
int fn,n,lk;
//printf("sizeof(buf)=%i\n",sizeof(buf));
memset(buf,0,atoi(argv[2]));
if(argc!=4){
printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
exit(1);
}
lk=atoi(argv[3]);
if((fn=open(argv[1],O_RDWR))==-1){
printf("openfilenew:open file %sfailure because %s\n",argv[1],strerror(errno));
exit(1);
}
switch(lk){
case 1:
{
int nm=0,k,count=atoi(argv[2]);
while(nm<count){
k=read(fn,buf,count-nm);
if(k==-1){
printf("error:%s",strerror(errno));exit(1);}
nm=nm+k;
};
/*if(n==-1){
printf("openfilenew:read file %s failure because %s \n",argv[1],strerror(errno));
free(buf);
close(fn);
exit(1);
}
if(n<atoi(argv[2]))
printf("openfilenwq:alread read %i bytes,< %i\n",atoi(argv[2]));
*/
buf[nm]=0;
printf("openfilenew:we read %i bytes[nm]==%x\n",nm,buf[nm]);
close(fn);
free(buf);
exit(0);
}
case 2:
{
printf("please input %i data of char for writing\n",atoi(argv[2])-1);
int kk=0;
//gets(buf);
fgets((char*)buf,(int)atoi(argv[2]),stdin);
printf("the act input %i bytes from stdin\n",atoi(argv[2])-1);
n=write(fn,buf,atoi(argv[2]));
if(n==-1)
printf("openfilenew:write to %s failure\n",argv[1]);
if(n<atoi(argv[2]))
printf("openfilenew:write bytes < the act input bytes%i\n",sizeof(buf));
close(fn);
exit(0);
break;
}
default:
printf("openfilenew:Usage: openfilenew filename bytes read/write(1/2)\n");
exit(1);
}
free(buf);
return 0;
}
|
1)上面是测试程序,名字叫test.c.
2)在LDD3的short.c的:
ssize_t do_short_read (struct inode *inode, struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
printk(KERN_ALERT "short: count of do_short_read function = %i\n",count);
....
}
函数中只是想看看count到底等于多少.
3)运行测试程序
test /dev/short0 10 2(表示写10个字符给/dev/short0,因为有个隐含的换行字符,所以提示输入9个字符)
(输入9个字符,比如1 2 3 4 5 6 7 8 9 )
test /dev/short0 6 1(表示从/dev/short0读6个字符)-------这一句可以再执行一遍,看看两次有什么区别.
执行了读6个字符之后,如果 dmesg | grep short ,应该发现short: count of do_short_read function = 6才正确,可是我执行的时候却是3
这个问题困绕了我很长一段时间,各位,请了!!!
[ 本帖最后由 dreamice 于 2009-1-14 16:00 编辑 ] |
|