主设备号和次设备号获取调到

我的职业是看店铺的 2013-01-23 11:41:26
在运行程序的时候,主设备号和次设备号获取颠倒,查看文件,但是不知道哪里出错了
运行结果如下面
[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;
}

...全文
152 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
man 2 stat后 The st_dev field describes the device on which this file resides. The st_rdev field describes the device that this file (inode) represents. 我这2句没看清楚 3q
ForestDB 2013-01-24
  • 打赏
  • 举报
回复
#define MAJOR(a) (int)((unsigned short)a >> 8) #define MINOR(a) (int)((unsigned short)a & 0xFF) 是不是这里的宏没有考虑大小端? 直接用定义好的函数吧。
图灵狗 2013-01-24
  • 打赏
  • 举报
回复
应该是没有反,<sys/types.h>中本身已经包含int major(dev_t dev);int minor(dev_t dev);函数,最好直接这样写: printf("File number:major %d,minor %d\n", major(sbuf.st_dev), minor(sbuf.st_dev));

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧