主设备号和次设备号获取调到
在运行程序的时候,主设备号和次设备号获取颠倒,查看文件,但是不知道哪里出错了
运行结果如下面
[mlstly885@localhost IO]$ ./01stat /dev/tty
File name: /dev/tty
File number:major 0,minor 5
[mlstly885@localhost IO]$ ls -l /dev/tty
crw-rw-rw-. 1 root tty 5, 0 1月 24 05:46 /dev/tty
希望大家帮我看下,谢谢
代码如下
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
}while(0)
#define MAJOR(a) (int)((unsigned short)a >> 8)
#define MINOR(a) (int)((unsigned short)a & 0xFF)
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage %s file\n", argv[0]);
exit(EXIT_FAILURE);
}
struct stat sbuf;
printf("File name: %s\n", argv[1]);
if (stat(argv[1], &sbuf) == -1)
ERR_EXIT("stat error");
printf("File number:major %d,minor %d\n", MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev));
return 0;
}