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 / mkentry.c < prev    next >
Text File  |  1990-05-10  |  3KB  |  110 lines

  1. /*
  2.  * mk_entry(), grow()
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include "msdos.h"
  8.  
  9. extern int fd, dir_start, dir_len, clus_size, dir_entries;
  10. extern int dir_chain[25];
  11.  
  12. /*
  13.  * Make a directory entry.  Builds a directory entry based on the
  14.  * name, attribute, starting cluster number, and size.  Returns a pointer
  15.  * to the static directory structure.
  16.  */
  17.  
  18. struct directory *
  19. mk_entry(filename, attr, fat, size, date)
  20. char *filename;
  21. unsigned char attr;
  22. int fat;
  23. long size, date;
  24. {
  25.     int i;
  26.     char *strncpy();
  27.     static struct directory ndir;
  28.     struct tm *now, *localtime();
  29.     unsigned char hour, min_hi, min_low, sec;
  30.     unsigned char year, month_hi, month_low, day;
  31.  
  32.     now = localtime(&date);
  33.     strncpy((char *) ndir.name, filename, 8);
  34.     strncpy((char *) ndir.ext, filename+8, 3);
  35.     ndir.attr = attr;
  36.     for (i=0; i<10; i++)
  37.         ndir.reserved[i] = '\0';
  38.     hour = now->tm_hour << 3;
  39.     min_hi = now->tm_min >> 3;
  40.     min_low = now->tm_min << 5;
  41.     sec = now->tm_sec / 2;
  42.     ndir.time[1] = hour + min_hi;
  43.     ndir.time[0] = min_low + sec;
  44.     year = (now->tm_year - 80) << 1;
  45.     month_hi = (now->tm_mon+1) >> 3;
  46.     month_low = (now->tm_mon+1) << 5;
  47.     day = now->tm_mday;
  48.     ndir.date[1] = year + month_hi;
  49.     ndir.date[0] = month_low + day;
  50.     ndir.start[1] = fat / 0x100;
  51.     ndir.start[0] = fat % 0x100;
  52.     ndir.size[3] = 0;        /* can't be THAT large */
  53.     ndir.size[2] = size / 0x10000L;
  54.     ndir.size[1] = (size % 0x10000L) / 0x100;
  55.     ndir.size[0] = (size % 0x10000L) % 0x100;
  56.     return(&ndir);
  57. }
  58.  
  59. /*
  60.  * Make a subdirectory grow in length.  Only subdirectories (not root) 
  61.  * may grow.  Returns a 0 on success or 1 on failure (disk full).
  62.  */
  63.  
  64. int
  65. grow(fat)
  66. int fat;
  67. {
  68.     int i, next, last, num, sector, buflen;
  69.     unsigned char tbuf[CLSTRBUF];
  70.     void perror(), exit(), move();
  71.  
  72.     last = nextfat(0);
  73.     if (last == -1)
  74.         return(1);
  75.  
  76.     while (1) {
  77.         next = getfat(fat);
  78.         if (next == -1) {
  79.             fprintf(stderr, "grow: FAT problem\n");
  80.             exit(1);
  81.         }
  82.                     /* end of cluster chain */
  83.         if (next >= 0xff8)
  84.             break;
  85.         fat = next;
  86.     }
  87.                     /* mark the end of the chain */
  88.     putfat(fat, (unsigned int) last);
  89.     putfat(last, 0xfff);
  90.                     /* zero the buffer */
  91.     buflen = clus_size * MSECSIZ;
  92.     for (i=0; i<buflen; i++)
  93.         tbuf[i] = '\0';
  94.  
  95.                     /* write the cluster */
  96.     sector = (last - 2) * clus_size + dir_start + dir_len;
  97.     move(sector);
  98.     if (write(fd, (char *) tbuf, buflen) != buflen) {
  99.         perror("grow: write");
  100.         exit(1);
  101.     }
  102.                     /* fix up the globals.... */
  103.     num = dir_entries / 16;
  104.     dir_entries += clus_size * 16;
  105.     dir_chain[num] = sector;
  106.     if (clus_size == 2)
  107.         dir_chain[num+1] = sector +1;
  108.     return(0);
  109. }
  110.