|
发表于 2003-7-13 01:38:47
|
显示全部楼层
- #include <stdio.h>
- #include <fcntl.h>
- void printPType(unsigned char *, char *);
- int
- main(int argc, char *argv[])
- {
- int fd, n, d;
- char hd[10], errstr[1024];
- unsigned char buf[512];
- if(argc == 1){
- fprintf(stderr, "usage: %s hdx\n", argv[0]);
- exit(-1);
- }
- for(d = 1; d < argc; ++d){
- if(strlen(argv[d]) > 3
- || argv[d][0] != 'h' || argv[d][1] != 'd'
- || argv[d][2] < 'a' || argv[d][2] > 'd'){
- fprintf(stderr, "%s is error name\n", argv[d]);
- continue;
- }
- strcpy(hd, "/dev/");
- strcat(hd, argv[d]);
-
- if((fd = open(hd, O_RDONLY)) < 0){
- snprintf(errstr, 1023, "cannot open %s", hd);
- perror(errstr);
- continue;
- }
- if((n = read(fd, buf, 512)) != 512){
- snprintf(errstr, 1023, "cannot read first sector for %s", hd);
- perror(errstr);
- close(fd);
- continue;
- }
- close(fd);
- printf("%s:\n----------\n", hd);
- printPType(buf + 446, hd);
- }
-
- exit(0);
- }
- void
- printPType(unsigned char *dpt, char *hd)
- {
- int i = 0;
- unsigned char type;
- for(i = 0; i < 4; ++i, dpt += 16){
- type = *(dpt + 4);
- printf("%s%d: ", hd, i + 1);
- switch(type){
- case 0:
- printf("none\n");
- break;
- case 1:
- printf("FAT12\n");
- break;
- case 4:
- printf("FAT16 (<32M)\n");
- break;
- case 5:
- printf("EXTEND (<2G)\n");
- break;
- case 6:
- case 0xe:
- printf("FAT16 (>32M)\n");
- break;
- case 7:
- printf("NTFS\n");
- break;
- case 0xb:
- printf("FAT32\n");
- break;
- case 0xc:
- case 0xf:
- printf("EXTEND (>2G)\n");
- break;
- case 0x82:
- printf("Linux SWAP\n");
- break;
- case 0x83:
- printf("Linux Navite\n");
- break;
- default:
- printf("unknown type\n");
- break;
- }
- }
- }
复制代码
用xxx hda hdb之类的命令,必须是root用户,
程序只能显示主分区,不能显示逻辑盘,不过原理一样。
我今天在网上大概查了一下分区类型,好多都没有查到(也没好好查),比如FreeBSD的类型就没有加进去。
当然,程序也有一些问题,不过基本功能还行。 |
|