Chinaunix首页 | 论坛 | 博客
  • 博客访问: 569046
  • 博文数量: 137
  • 博客积分: 4040
  • 博客等级: 上校
  • 技术积分: 1584
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-08 13:05
文章分类

全部博文(137)

文章存档

2011年(10)

2010年(23)

2009年(104)

分类: LINUX

2009-08-19 15:32:41

/*
 * linux/fs/ioctl.c
 *
 * (C) 1991 Linus Torvalds
 */


#include <string.h>
#include <errno.h>
#include <sys/stat.h>

#include <linux/sched.h>

extern int tty_ioctl(int dev, int cmd, int arg);// 终端ioctl(chr_drv/tty_ioctl.c, 115)


typedef int (*ioctl_ptr)(int dev,int cmd,int arg);// 定义输入输出控制(ioctl)函数指针


#define NRDEVS ((sizeof (ioctl_table))/(sizeof (ioctl_ptr)))// 定义系统中设备种数


static ioctl_ptr ioctl_table[]={// ioctl 操作函数指针表

    NULL,        /* nodev */
    NULL,        /* /dev/mem */
    NULL,        /* /dev/fd */
    NULL,        /* /dev/hd */
    tty_ioctl,    /* /dev/ttyx */
    tty_ioctl,    /* /dev/tty */
    NULL,        /* /dev/lp */
    NULL};        /* named pipes */
    

int sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)// 系统调用函数 - 输入输出控制函数

{    //参数:fd - 文件描述符;cmd - 命令码;arg - 参数,返回:成功则返回0,否则返回出错码

    struct file * filp;
    int dev,mode;

    if (fd >= NR_OPEN || !(filp = current->filp[fd]))// 如果文件描述符超出可打开的文件数,或者对应描述符的文件结构指针为空,则返回出错码

        return -EBADF;
    mode=filp->f_inode->i_mode;// 取对应文件的属性

    if (!S_ISCHR(mode) && !S_ISBLK(mode))//如果该文件不是字符文件,也不是块设备文件,则返回出错码

        return -EINVAL;
    dev = filp->f_inode->i_zone[0];// 从字符或块设备文件的i 节点中取设备号

    if (MAJOR(dev) >= NRDEVS)//如果设备号大于系统现有的设备数,则返回出错号

        return -ENODEV;
    if (!ioctl_table[MAJOR(dev)])//如果该设备在ioctl 函数指针表中没有对应函数,则返回出错码

        return -ENOTTY;
    return ioctl_table[MAJOR(dev)](dev,cmd,arg);// 返回实际ioctl 函数返回码,成功则返回0,否则返回出错码

}

阅读(811) | 评论(0) | 转发(0) |
0

上一篇:fcntl.c

下一篇:exec.c

给主人留下些什么吧!~~