Advertisement
anhkiet2507

Program reading ROOT and print file and directory name and size

Oct 5th, 2022
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <memory.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct BOOT {  //for FAT16
  8.     char jmp[3];
  9.     char OEM[8];
  10.     int bytes_per_sector;
  11.     char sectors_per_cluster;
  12.     int reserved;
  13.     char FAT_cnt;
  14.     int ROOT_size;
  15.     int total_sectors;
  16.     char media;
  17.     int FAT_size;
  18.     int sectors_per_track;
  19.     int head_cnt;
  20.     long hidden_sectors;
  21.     long total_sectors_long;
  22.     char unknown[3];
  23.     long serial;
  24.     char volume[11];
  25.     char FAT_type[8];
  26.     char loader[448];
  27.     char mark[2];
  28. };
  29.  
  30. struct ROOT {
  31.     char name[8];
  32.     char ext[3];
  33.     char attr;
  34.     char reserved[10];
  35.     char time[2];
  36.     char date[2];
  37.     int first_cluster;
  38.     long size;
  39. };
  40.  
  41. void main()
  42. {
  43.  
  44.     int drive = 3; //A=0, B=1, C=2, D=3 ...
  45.  
  46.     //Reading boot sector from disk D
  47.     BOOT boot;
  48.  
  49.     int res = absread(drive, 1, 0, &boot);
  50.     if(res != 0){
  51.         printf("Cannot read boot sector\n");
  52.         return;
  53.     }
  54.     int i;
  55.     unsigned int *fat = (unsigned int *)malloc (boot.FAT_size * boot.bytes_per_sector);
  56.     if (fat == NULL) {
  57.         printf("Not enough memory\n");
  58.         return;
  59.     }
  60.     res = absread(drive, boot.FAT_size, boot.reserved, fat);
  61.     int free_count = 0;
  62.     unsigned int n = 5;
  63.     unsigned int cur = n;
  64.     //Reading ROOT from disk D
  65.     printf("\n\n");
  66.     printf("Reading ROOT information:\n");
  67.     printf("------------------------\n");
  68.  
  69.     int num_byte = boot.ROOT_size * 32;//sizeof(ROOT)
  70.  
  71.     ROOT *root = (ROOT *)malloc(num_byte);
  72.     if(root == NULL) return;
  73.  
  74.     int num_sector = num_byte / boot.bytes_per_sector;
  75.     int root_begin = boot.reserved + boot.FAT_size * boot.FAT_cnt;
  76.  
  77.     res = absread(drive, num_sector, root_begin, (void *)root);
  78.     if(res != 0){
  79.         printf("\n Cannot read ROOT\n");
  80.         return;
  81.     }
  82.     //Printing first 3 items of root
  83.     printf("3 first items of root:\n");
  84.     for(i = 0; i < 3; i++){
  85.         if(root[i].name[0] == ' ') continue;
  86.         for(int j = 0; j < 8 && root[i].name[j] != ' '; j++)
  87.             printf("%c", root[i].name[j]);
  88.         printf("\t");
  89.         printf("%d \t %ld\n", root[i].first_cluster, root[i].size);
  90.     }
  91.  
  92.     free(root);
  93.     free(fat);
  94.  
  95.     getchar();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement