打印分区表-C程序

Posted

技术标签:

【中文标题】打印分区表-C程序【英文标题】:Printing Partition Table - C program 【发布时间】:2012-08-27 09:01:08 【问题描述】:

我正在尝试使用 C 编程语言打印分区表,一切似乎都工作正常:打开和读取,但我不明白它为什么打印垃圾值。

代码如下:

struct partition

    unsigned char drive;
    unsigned char chs_begin[3];
    unsigned char sys_type;
    unsigned char chs_end[3];
    unsigned char start_sector[4];
    unsigned char nr_sector[4];
;

int main()

    int gc = 0, i = 1, nr = 0, pos = -1, nw = 0;
    int fd =0;
    char  buf[512] ;
    struct partition *sp;
    printf("Ok ");

    if ( (fd = open("/dev/sda", O_RDONLY | O_SYNC )) == -1)
    
         perror("Open");
         exit(1);
    
    printf("fd is %d \n", fd);

    pos = lseek (fd, 0, SEEK_CUR);

    printf("Position of pointer is :%d\n", pos);
    if ((nr = read(fd, buf, sizeof(buf))) == -1)
    
        perror("Read");
        exit(1);
    

    close(fd);

    printf("Size of buf = %d\n and number of bytes read are %d ", sizeof(buf), nr);


    if ((nw = write(1, buf, 64)) == -1)
    
        printf("Write: Error");
        exit(1);
    

    printf("\n\n %d bytes are just been written on stdout\n", nw,"this can also be printed\n");

    printf("\n\t\t*************Partition Table****************\n\n");

    for (i=0 ; i<4 ; i++)
    
        sp = (struct partition *)(buf + 446 + (16 * i));
        putchar(sp -> drive);
    


    return 0;

打印的是垃圾而不是分区表。

我可能有一些基本的理解问题,但我用谷歌搜索了很长时间,但它并没有真正帮助。我还看到了 fdisk 的源代码,但目前超出了我的理解范围。有人可以指导我吗?我不指望有人能清除我的错误并给我工作代码。只需一两句话 - 或任何链接。

【问题讨论】:

您是否考虑查看fdisk 或其他分区实用程序的源代码? @BasileStarynkevitch Starynkevitch 是的,他按照他的帖子做了。 @Adorn您的问题出现在这一行? printf("\n\t\t*************Partition Table****************\n\n"); ?还是你的意思是之后的输出? drivevariable 类型是什么? 我认为partition entry 的第一个字节是引导指示器(可引导)标志。它可能有00x80 值。您似乎正试图将其用作驱动器号。 您的write() 调用会打印存储在 MBR 中的引导代码。如果要打印分区表,应从buf + 446 开始打印。然后,正如@fasked 所说,分区条目不包含驱动器号(无论如何,仅在类似 DOS 的操作系统中使用),而是一个活动分区指示器。 【参考方案1】:

试试这个:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

struct partition

        unsigned char boot_flag;        /* 0 = Not active, 0x80 = Active */
        unsigned char chs_begin[3];
        unsigned char sys_type;         /* For example : 82 --> Linux swap, 83 --> Linux native partition, ... */
        unsigned char chs_end[3];
        unsigned char start_sector[4];
        unsigned char nr_sector[4];
;

void string_in_hex(void *in_string, int in_string_size);
void dump_partition(struct partition *part, int partition_number);

void dump_partition(struct partition *part, int partition_number)

        printf("Partition /dev/sda%d\n", partition_number + 1);
        printf("boot_flag = %02X\n", part->boot_flag);
        printf("chs_begin = ");
        string_in_hex(part->chs_begin, 3);
        printf("sys_type = %02X\n", part->sys_type);
        printf("chs_end = ");
        string_in_hex(part->chs_end, 3);
        printf("start_sector = ");
        string_in_hex(part->start_sector, 4);
        printf("nr_sector = ");
        string_in_hex(part->nr_sector, 4);


void string_in_hex(void *in_string, int in_string_size)

        int i;
        int k = 0;

        for (i = 0; i < in_string_size; i++)
        
                printf("%02x ", ((char *)in_string)[i]& 0xFF);
                k = k + 1;
                if (k == 16)
                
                        printf("\n");
                        k = 0;
                
        
        printf("\n");


int main(int argc, char **argv)

        int /*gc = 0,*/ i = 1, nr = 0, pos = -1/*, nw = 0*/;
        int fd = 0;
        char  buf[512] ;
        struct partition *sp;
        int ret = 0;

        printf("Ok ");

        if ((fd = open("/dev/sda", O_RDONLY | O_SYNC)) == -1)
        
                perror("Open");
                exit(1);
        
        printf("fd is %d\n", fd);

        pos = lseek (fd, 0, SEEK_CUR);

        printf("Position of pointer is :%d\n", pos);
        if ((nr = read(fd, buf, sizeof(buf))) == -1)
        
                perror("Read");
                exit(1);
        

        ret = close(fd);
        if (ret == -1)
        
                perror("close");
                exit(1);
        

        /* Dump the MBR buffer, you can compare it on your system with the output of the command:
         * hexdump -n 512 -C /dev/sda
         */
        string_in_hex(buf, 512);

        printf("Size of buf = %d - and number of bytes read are %d\n", sizeof(buf), nr);
        /*if ((nw = write(1, buf, 64)) == -1)
        
                printf("Write: Error");
                exit(1);
        

        printf("\n\n%d bytes are just been written on stdout\nthis can also be printed\n", nw);
        */
        //printf("\n\t\t*************Partition Table****************\n\n");
        printf("\n\t\t*************THE 4 MAIN PARTITIONS****************\n\n");

        /* Dump main partitions (4 partitions) */
        /* Note : the 4 partitions you are trying to dump are not necessarily existing! */
        for (i = 0 ; i < 4 ; i++)
        
                sp = (struct partition *)(buf + 446 + (16 * i));
                //putchar(sp->boot_flag);
                dump_partition(sp, i);
        

        return 0;

【讨论】:

非常感谢大家的帮助。 "注意:您尝试转储的 4 个分区不一定存在!"您能否详细说明此评论?如果有超过 4 个分区怎么办?您还可以建议一些资源来研究 HDD 和 SSD 的逻辑结构以及分区表。谢谢

以上是关于打印分区表-C程序的主要内容,如果未能解决你的问题,请参考以下文章

Arduino ESP32 flash分区表配置信息查询示例程序

C零基础视频-45-内存的分区:全局区栈区堆

scala-spark实现重分区和打印各个分区的data

MBR的位置是不是位于C盘?

磁盘做成LVM挂载

打印给定整数作为输入的所有唯一整数分区