home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / diskutil / mtools / dir_read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-05  |  3.2 KB  |  155 lines

  1. #include <stdio.h>
  2. #include "msdos.h"
  3.  
  4. long dir_chain[MAX_DIR_SECS];        /* chain of sectors in directory */
  5. unsigned char *dir_buf;            /* the directory buffer */
  6. int dir_dirty;                /* is the buffer dirty? */
  7.  
  8. extern int dir_len, dir_start, clus_size, dir_entries, fat_error;
  9. extern unsigned int last_fat;
  10.  
  11. /*
  12.  * Read a directory entry, return a pointer a static structure.
  13.  */
  14.  
  15. struct directory *
  16. dir_read(num)
  17. int num;
  18. {
  19.     char *memcpy();
  20.     unsigned char *offset;
  21.     static struct directory dir;
  22.  
  23.     offset = dir_buf + (num * MDIR_SIZE);
  24.     memcpy((char *) &dir, (char *) offset, MDIR_SIZE);
  25.     return(&dir);
  26. }
  27.  
  28. /*
  29.  * Fill in the global variable dir_chain[].  Argument is the starting
  30.  * cluster number.  Returns -1 on error.
  31.  */
  32.  
  33. int
  34. fill_chain(num)
  35. unsigned int num;
  36. {
  37.     register int i, length;
  38.     unsigned int next, fat_decode();
  39.     unsigned char *offset;
  40.     char *malloc();
  41.     void free(), perror(), exit(), disk_read(), dir_flush();
  42.  
  43.     length = 0;
  44.     /* CONSTCOND */
  45.     while (1) {
  46.         dir_chain[length] = (long) (num - 2) * clus_size + dir_start + dir_len;
  47.         length++;
  48.                     /* sectors, not clusters! */
  49.         for (i = 1; i < clus_size; i++) {
  50.             dir_chain[length] = dir_chain[length - 1] + 1L;
  51.             length++;
  52.         }
  53.  
  54.         if (length >= MAX_DIR_SECS) {
  55.             fprintf(stderr, "fill_chain: directory too large\n");
  56.             return(-1);
  57.         }
  58.                     /* get next cluster number */
  59.         next = fat_decode(num);
  60.         if (next == 1) {
  61.             fprintf(stderr, "fill_chain: FAT problem\n");
  62.             fat_error++;
  63.             return(-1);
  64.         }
  65.                     /* end of cluster chain */
  66.         if (next >= last_fat)
  67.             break;
  68.         num = next;
  69.     }
  70.     if (dir_dirty)
  71.         dir_flush();
  72.                     /* fill the dir_buf */
  73.     free((char *) dir_buf);
  74.     dir_buf = (unsigned char *) malloc((unsigned int) length * MSECTOR_SIZE);
  75.     if (dir_buf == NULL) {
  76.         perror("fill_chain: malloc");
  77.         exit(1);
  78.     }
  79.  
  80.     for (i = 0; i < length; i++) {
  81.         offset = dir_buf + (i * MSECTOR_SIZE);
  82.         disk_read(dir_chain[i], offset, MSECTOR_SIZE);
  83.     }
  84.  
  85.     dir_entries = length * 16;
  86.     return(0);
  87. }
  88.  
  89. /*
  90.  * Reset the global variable dir_chain[] to the root directory.
  91.  */
  92.  
  93. void
  94. reset_chain(code)
  95. int code;
  96. {
  97.     register int i;
  98.     char *malloc();
  99.     void free(), disk_read(), dir_flush(), exit(), perror();
  100.  
  101.     if (dir_dirty)
  102.         dir_flush();
  103.  
  104.     for (i = 0; i < dir_len; i++)
  105.         dir_chain[i] = (long) dir_start + i;
  106.  
  107.     if (code == OLD)
  108.         free((char *) dir_buf);
  109.  
  110.     dir_buf = (unsigned char *) malloc((unsigned int) dir_len * MSECTOR_SIZE);
  111.     if (dir_buf == NULL) {
  112.         perror("reset_chain: malloc");
  113.         exit(1);
  114.     }
  115.     disk_read((long) dir_start, dir_buf, dir_len * MSECTOR_SIZE);
  116.  
  117.     dir_entries = dir_len * 16;
  118.     return;
  119. }
  120.  
  121. /*
  122.  * Get rid of spaces in an MSDOS 'raw' name (one that has come from the
  123.  * directory structure) so that it can be used for regular expression
  124.  * matching with a unix filename.  Also used to 'unfix' a name that has
  125.  * been altered by dos_name().  Returns a pointer a static buffer.
  126.  */
  127.  
  128. char *
  129. unix_name(name, ext)
  130. unsigned char *name, *ext;
  131. {
  132.     char *s, tname[9], text[4], *strcpy(), *strcat(), *strchr();
  133.     char *strncpy();
  134.     static char ans[13];
  135.  
  136.     strncpy(tname, (char *) name, 8);
  137.     tname[8] = '\0';
  138.     if (s = strchr(tname, ' '))
  139.         *s = '\0';
  140.  
  141.     strncpy(text, (char *) ext, 3);
  142.     text[3] = '\0';
  143.     if (s = strchr(text, ' '))
  144.         *s = '\0';
  145.  
  146.     if (*text) {
  147.         strcpy(ans, tname);
  148.         strcat(ans, ".");
  149.         strcat(ans, text);
  150.     }
  151.     else
  152.         strcpy(ans, tname);
  153.     return(ans);
  154. }
  155.