# 2.11BSD#431 - /usr/src/local/mtools/fat_read.c

##### Functions

    fat_decode        X
    fat_read          X

##### Variables

    end_fat           X
    fat_buf           X
    fat_len           X
    last_fat          X

       1: #include <stdio.h>
       2: #include "msdos.h"
       3:
       4: int fat_len;                /* length of FAT table (in sectors) */
       5: unsigned int end_fat;           /* the end-of-chain marker */
       6: unsigned int last_fat;          /* the last in a chain marker */
       7: unsigned char *fat_buf;         /* the File Allocation Table */
       8:
       9: extern int fat_bits;
      10:
      11: /*
      12:  * Get and decode a FAT (file allocation table) entry.  Returns the cluster
      13:  * number on success or 1 on failure.
      14:  */
      15:
      16: unsigned int
      17: fat_decode(num)
      18: unsigned int num;
      19: {
      20:     unsigned int fat, fat_hi, fat_low, byte_1, byte_2;
      21:     int start;
      22:
      23:     if (fat_bits == 12) {
      24:         /*
      25: 		 *	|    byte n     |   byte n+1    |   byte n+2    |
      26: 		 *	|7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
      27: 		 *	| | | | | | | | | | | | | | | | | | | | | | | | |
      28: 		 *	| n+0.0 | n+0.5 | n+1.0 | n+1.5 | n+2.0 | n+2.5 |
      29: 		 *	    \_____  \____   \______/________/_____   /
      30: 		 *	      ____\______\________/   _____/  ____\_/
      31: 		 *	     /     \      \          /       /     \
      32: 		 *	| n+1.5 | n+0.0 | n+0.5 | n+2.0 | n+2.5 | n+1.0 |
      33: 		 *	|      FAT entry k      |    FAT entry k+1      |
      34: 		 */
      35:                     /* which bytes contain the entry */
      36:         start = num * 3 / 2;
      37:         if (start <= 2 || start + 1 > (fat_len * MSECTOR_SIZE))
      38:             return(1);
      39:
      40:         byte_1 = *(fat_buf + start);
      41:         byte_2 = *(fat_buf + start + 1);
      42:                     /* (odd) not on byte boundary */
      43:         if (num % 2) {
      44:             fat_hi = (byte_2 & 0xff) << 4;
      45:             fat_low = (byte_1 & 0xf0) >> 4;
      46:         }
      47:                     /* (even) on byte boundary */
      48:         else {
      49:             fat_hi = (byte_2 & 0xf) << 8;
      50:             fat_low = byte_1 & 0xff;
      51:         }
      52:         fat = (fat_hi + fat_low) & 0xfff;
      53:     }
      54:     else {
      55:         /*
      56: 		 *	|    byte n     |   byte n+1    |
      57: 		 *	|7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
      58: 		 *	| | | | | | | | | | | | | | | | |
      59: 		 *	|         FAT entry k           |
      60: 		 */
      61:                     /* which bytes contain the entry */
      62:         start = num * 2;
      63:         if (start <= 3 || start + 1 > (fat_len * MSECTOR_SIZE))
      64:             return(1);
      65:
      66:         fat = (*(fat_buf + start + 1) * 0x100) + *(fat_buf + start);
      67:     }
      68:     return(fat);
      69: }
      70:
      71: /*
      72:  * Read the entire FAT table into memory.
      73:  */
      74:
      75: void
      76: fat_read(start)
      77: int start;
      78: {
      79:     int buflen;
      80:     char *malloc();
      81:     void perror(), exit(), disk_read();
      82:                     /* only the first copy of the FAT */
      83:     buflen = fat_len * MSECTOR_SIZE;
      84:     fat_buf = (unsigned char *) malloc((unsigned int) buflen);
      85:     if (fat_buf == NULL) {
      86:         perror("fat_read: malloc");
      87:         exit(1);
      88:     }
      89:                     /* read the FAT sectors */
      90:     disk_read((long) start, fat_buf, buflen);
      91:
      92:                     /* the encoding scheme */
      93:     if (fat_bits == 12) {
      94:         end_fat = 0xfff;
      95:         last_fat = 0xff8;
      96:     }
      97:     else {
      98:         end_fat = 0xffff;
      99:         last_fat = 0xfff8;
     100:     }
     101:     return;
     102: }

#### Defined functions

fat_decode defined in line 16; used 18 times

- in /usr/src/local/mtools/buf_write.c line 124, 136
- in /usr/src/local/mtools/dir_make.c line 24, 33, 82-85(2)
- in /usr/src/local/mtools/dir_read.c line 38, 59
- in /usr/src/local/mtools/fat_free.c line 17, 25
- in /usr/src/local/mtools/file_read.c line 20, 72
- in /usr/src/local/mtools/mdir.c line 216-221(2)
- in /usr/src/local/mtools/mrd.c line 136, 154
- in /usr/src/local/mtools/mwrite.c line 343-348(2)

fat_read defined in line 75; used 2 times

- in /usr/src/local/mtools/init.c line 37, 229

#### Defined variables

end_fat defined in line 5; used 6 times

- in line 94-98(2)
- in /usr/src/local/mtools/dir_make.c line 46
- in /usr/src/local/mtools/file_write.c line 34, 44, 51

fat_buf defined in line 7; used 18 times

- in line 40-41(2), 66(2), 84-90(3)
- in /usr/src/local/mtools/fat_write.c line 37-43(6), 58-59(2), 82, 91
- in /usr/src/local/mtools/init.c line 43

fat_len defined in line 4; used 14 times

- in line 37, 63, 83
- in /usr/src/local/mtools/fat_write.c line 33, 55, 79-80(2), 90
- in /usr/src/local/mtools/init.c line 104, 114, 124, 134, 180-181(2)

last_fat defined in line 6; used 6 times

- in line 95-99(2)
- in /usr/src/local/mtools/dir_make.c line 40
- in /usr/src/local/mtools/dir_read.c line 66
- in /usr/src/local/mtools/fat_free.c line 32
- in /usr/src/local/mtools/file_read.c line 79

Last modified: 1992-06-24 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 499 |