home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / mtype.c < prev    next >
C/C++ Source or Header  |  1990-05-10  |  4KB  |  175 lines

  1. /*
  2.  * Display contents of a MSDOS file
  3.  *
  4.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  5.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  6.  *                     Directorate of Engineering & Housing
  7.  *                     Environmental Management Office
  8.  *                     Fort Hood, TX 76544-5057
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include "msdos.h"
  13.  
  14. int fd;                /* the file descriptor for the floppy */
  15. int dir_start;            /* starting sector for directory */
  16. int dir_len;            /* length of directory (in sectors) */
  17. int dir_entries;        /* number of directory entries */
  18. int dir_chain[25];        /* chain of sectors in directory */
  19. int clus_size;            /* cluster size (in sectors) */
  20. int fat_len;            /* length of FAT table (in sectors) */
  21. int num_clus;            /* number of available clusters */
  22. unsigned char *fatbuf;        /* the File Allocation Table */
  23. char *mcwd;            /* the Current Working Directory */
  24.  
  25. long size;
  26. long current;
  27. int stripmode = 0;
  28. int textmode = 0;
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     extern int optind;
  35.     extern char *optarg;
  36.     int fat, i, ismatch, entry, c, oops;
  37.     char *filename, *newfile, text[4], tname[9], *getname(), *unixname();
  38.     char *strncpy(), *pathname, *getpath();
  39.     void exit(), readit(), free();
  40.     struct directory *dir, *search();
  41.  
  42.     if (init(0)) {
  43.         fprintf(stderr, "mtype: Cannot initialize diskette\n");
  44.         exit(1);
  45.     }
  46.                     /* get command line options */
  47.     oops = 0;
  48.     while ((c = getopt(argc, argv, "st")) != EOF) {
  49.         switch(c) {
  50.             case 's':
  51.                 stripmode = 1;
  52.                 break;
  53.             case 't':
  54.                 textmode = 1;
  55.                 break;
  56.             default:
  57.                 oops = 1;
  58.                 break;
  59.         }
  60.     }
  61.  
  62.     if (oops || (argc - optind) < 1) {
  63.         fprintf(stderr, "Usage: mtype [-st] msdosfile [msdosfiles...]\n");
  64.         exit(1);
  65.     }
  66.  
  67.     for (i=optind; i<argc; i++) {
  68.         filename = getname(argv[i]);
  69.         pathname = getpath(argv[i]);
  70.         if (subdir(pathname)) {
  71.             free(filename);
  72.             free(pathname);
  73.             continue;
  74.         }
  75.         ismatch = 0;
  76.         for (entry=0; entry<dir_entries; entry++) {
  77.             dir = search(entry);
  78.                     /* if empty */
  79.             if (dir->name[0] == 0x0)
  80.                 break;
  81.                     /* if erased */
  82.             if (dir->name[0] == 0xe5)
  83.                 continue;
  84.                     /* if dir or volume label */
  85.             if ((dir->attr & 0x10) || (dir->attr & 0x08))
  86.                 continue;
  87.  
  88.             strncpy(tname, (char *) dir->name, 8);
  89.             strncpy(text, (char *) dir->ext, 3);
  90.             tname[8] = '\0';
  91.             text[3] = '\0';
  92.  
  93.             newfile = unixname(tname, text);
  94.                     /* see it if matches the pattern */
  95.             if (match(newfile, filename)) {
  96.                 fat = dir->start[1]*0x100 + dir->start[0];
  97.                 size = dir->size[2]*0x10000L + dir->size[1]*0x100 + dir->size[0];
  98.                 readit(fat);
  99.                 ismatch = 1;
  100.             }
  101.             free(newfile);
  102.         }
  103.         if (!ismatch)
  104.             fprintf(stderr, "mtype: File \"%s\" not found\n", filename);
  105.         free(filename);
  106.         free(pathname);
  107.     }
  108.     close(fd);
  109.     exit(0);
  110. }
  111.  
  112. /*
  113.  * Decode the FAT chain given the begining FAT entry.
  114.  */
  115.  
  116. void
  117. readit(fat)
  118. int fat;
  119. {
  120.     void getcluster();
  121.  
  122.     current = 0L;
  123.     while (1) {
  124.         getcluster(fat);
  125.                     /* get next cluster number */
  126.         fat = getfat(fat);
  127.         if (fat == -1) {
  128.             fprintf(stderr, "mtype: FAT problem\n");
  129.             exit(1);
  130.         }
  131.                     /* end of cluster chain */
  132.         if (fat >= 0xff8)
  133.             break;
  134.     }
  135.     return;
  136. }
  137.  
  138. /*
  139.  * Read the named cluster, output to the stdout.
  140.  */
  141.  
  142. void
  143. getcluster(num)
  144. int num;
  145. {
  146.     register int i;
  147.     int buflen, start;
  148.     unsigned char buf[CLSTRBUF];
  149.     void exit(), perror(), move();
  150.  
  151.     start = (num - 2)*clus_size + dir_start + dir_len;
  152.     move(start);
  153.  
  154.     buflen = clus_size * MSECSIZ;
  155.     if (read(fd, (char *) buf, buflen) != buflen) {
  156.         perror("getcluster: read");
  157.         exit(1);
  158.     }
  159.                     /* stop at size not EOF marker */
  160.     for (i=0; i<buflen; i++) {
  161.         current++;
  162.         if (current > size) 
  163.             break;
  164.         if (textmode && buf[i] == '\r')
  165.             continue;
  166.         if (textmode && current == size && buf[i] == 0x1a)
  167.             continue;
  168.         if (stripmode)
  169.             putchar(buf[i] & 0x7f);
  170.         else
  171.             putchar(buf[i]);
  172.     }
  173.     return;
  174. }
  175.