C lstat major MAJOR 获得设备号

#cat lstat.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>


#define MINORBITS   20
#define MINORMASK   ((1U << MINORBITS) - 1)
#define MAJOR(dev)  ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)  ((unsigned int) ((dev) & MINORMASK))

int main(void)
{

	struct stat buf;

	lstat("/dev/sdc", &buf);

	int a = MAJOR(buf.st_dev);
	int b = MINOR(buf.st_dev);

	int c = MAJOR(buf.st_rdev);
	int d = MINOR(buf.st_rdev);

	int e = major(buf.st_rdev);
	int f = minor(buf.st_rdev);

	int g = major(buf.st_dev);
	int h = minor(buf.st_dev);
	printf("st_dev MAJOR:%d, MINOR:%d\n", a, b);
	printf("st_rdev MAJOR:%d, MINOR:%d\n", c, d);
	printf("st_dev major:%d, minor:%d\n", g, h);
	printf("st_rdev major:%d, minor:%d\n", e, f);


	return 0;

}

#./lstat
st_dev MAJOR:0, MINOR:5
st_rdev MAJOR:0, MINOR:2080
st_dev major:0, minor:5
st_rdev major:8, minor:32


posted @ 2018-01-10 13:20  苏小北1024  阅读(214)  评论(0编辑  收藏  举报