zoukankan      html  css  js  c++  java
  • 获取设备的设备号

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/sysmacros.h>
    #include <sys/stat.h>
    int main(int argc,char *argv[])
    {
        int i;
        struct stat buf;
        for(i = 1; i < argc; i++)
        {
            printf("%s:",argv[i]);
            if(stat(argv[i],&buf) < 0)
            {
                printf("stat error ");
                continue;        
            }
            //st_dev为文件系统的设备号 st_rdev为实际设备的设备号(字符特殊文件和块特殊文件才有)
            //设备号分为主、次设备号 分别使用宏major minor获取
            printf("dev = %d/%d", major(buf.st_dev),minor(buf.st_dev));
            if(S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode))
            {
                printf(" (%s) rdev = %d/%d",(S_ISCHR(buf.st_mode)) ? "character" : "block"
                    ,major(buf.st_rdev),minor(buf.st_rdev));
            }
            printf(" ");    
        }
        
        return 0;
    }

    测试

    ./devtype /home/xww /dev/tty[01]
    获取三个设备的设备号 之所以说三个是因为shell程序将扩展字符串/dev/tty[01]为/dev/tty0  /dev/tty1

  • 相关阅读:
    164-268. 丢失的数字
    163-20. 有效的括号
    Sword 30
    Sword 29
    Sword 27
    Sword 25
    Sword 24
    Sword 22
    Sword 21
    Sword 18
  • 原文地址:https://www.cnblogs.com/xieweiwei/p/3246354.html
Copyright © 2011-2022 走看看