1: #include <stdio.h> 2: #include "msdos.h" 3: 4: extern int clus_size, dir_start, dir_len, fat_error; 5: extern unsigned int last_fat; 6: 7: /* 8: * Read the clusters given the beginning FAT entry. Returns 0 on success. 9: */ 10: 11: int 12: file_read(fp, fat, textmode, stripmode, size) 13: FILE *fp; 14: unsigned int fat; 15: int textmode, stripmode; 16: long size; 17: { 18: register int i; 19: int in_len, out_len; 20: unsigned int fat_decode(); 21: long current, start; 22: unsigned char in_buf[MAX_CLUSTER], out_buf[MAX_CLUSTER]; 23: void disk_read(), perror(); 24: 25: current = 0L; 26: in_len = clus_size * MSECTOR_SIZE; 27: /* a zero length file? */ 28: if (fat == 0) 29: return(0); 30: 31: /* CONSTCOND */ 32: while (1) { 33: start = (long) (fat - 2) * clus_size + dir_start + dir_len; 34: disk_read(start, in_buf, in_len); 35: 36: /* do the translations */ 37: if (textmode || stripmode) { 38: out_len = 0; 39: for (i = 0; i < in_len; i++) { 40: current++; 41: if (current > size) 42: break; 43: if (textmode && in_buf[i] == '\r') 44: continue; 45: if (textmode && current == size && in_buf[i] == 0x1a) 46: continue; 47: if (stripmode) 48: out_buf[out_len++] = in_buf[i] & 0x7f; 49: else 50: out_buf[out_len++] = in_buf[i]; 51: } 52: /* write it */ 53: if (fwrite((char *) out_buf, sizeof(*out_buf), out_len, fp) == 0) { 54: perror("file_write: fwrite"); 55: return(-1); 56: } 57: } 58: /* much easier... */ 59: else { 60: out_len = (size - current > in_len) ? in_len : size - current; 61: if (fwrite((char *) in_buf, sizeof(*in_buf), out_len, fp) == 0) { 62: perror("file_write: fwrite"); 63: return(-1); 64: } 65: 66: current += out_len; 67: if (current >= size) 68: break; 69: } 70: 71: /* get next cluster number */ 72: fat = fat_decode(fat); 73: if (fat == 1) { 74: fprintf(stderr, "file_read: FAT problem\n"); 75: fat_error++; 76: return(-1); 77: } 78: /* end of cluster chain */ 79: if (fat >= last_fat) 80: break; 81: } 82: return(0); 83: }