Chinaunix首页 | 论坛 | 博客
  • 博客访问: 271427
  • 博文数量: 40
  • 博客积分: 589
  • 博客等级: 中士
  • 技术积分: 549
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-10 16:03
文章分类

全部博文(40)

文章存档

2013年(6)

2012年(34)

我的朋友

分类: LINUX

2012-10-25 15:07:54

  1 #include
  2 #include
  3 #include
  4 #include
  5 
  6 int main(void)
  7 {
  8     int fd,len;
  9     fd = open("/proc/cpuinfo",O_RDONLY);
 10     if(fd<0)
 11     {
 12         printf("error");
 13         return(-1);
 14     }
 15     len = lseek(fd,0,SEEK_SET);
 16     printf("current pos1 is:%d\n",len);
 17     len = lseek(fd,0,SEEK_END); //这里出错!!!
 18     printf("current pos2 is:%d\n",len);//居然返回-1
 19 }

原来是procfs文件的系统的seq_lseek不支持SEEK_END这个选项,无语了!
#define SEEK_SET 0 /* seek relative to beginning of file */
#define SEEK_CUR 1 /* seek relative to current file position */
#define SEEK_END 2 /* seek relative to end of file */
#define SEEK_MAX SEEK_END

loff_t seq_lseek(struct file *file, loff_t offset, int origin)
{
struct seq_file *m = (struct seq_file *)file->private_data;
loff_t retval = -EINVAL;

mutex_lock(&m->lock);
m->version = file->f_version;
switch (origin) {
case 1: //--->SEEK_SET
offset += file->f_pos;
case 0: //--->SEEK_CUR
if (offset < 0)
break;
retval = offset;
if (offset != m->read_pos) {
while ((retval=traverse(m, offset)) == -EAGAIN)
;
if (retval) {
/* with extreme prejudice... */
file->f_pos = 0;
m->read_pos = 0;
m->version = 0;
m->index = 0;
m->count = 0;
} else {
m->read_pos = offset;
retval = file->f_pos = offset;
}
}
}
file->f_version = m->version;
mutex_unlock(&m->lock);
return retval;
}
阅读(2650) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~