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
", a, b);
	printf("st_rdev MAJOR:%d, MINOR:%d
", c, d);
	printf("st_dev major:%d, minor:%d
", g, h);
	printf("st_rdev major:%d, minor:%d
", 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


原文地址:https://www.cnblogs.com/muahao/p/8258026.html