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 / mmd.c < prev    next >
C/C++ Source or Header  |  1990-05-10  |  4KB  |  180 lines

  1. /*
  2.  * Make a MSDOS sub directory
  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. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.     int entry, slot, fat, dot, fargn, verbose;
  30.     char *filename, *newfile, *fixname(), *strncpy();
  31.     char *getpath(), *pathname, tname[9], text[4], *fixed, *unixname();
  32.     void exit(), putcluster(), writefat(), writedir(), free();
  33.     struct directory *dir, *search(), *mk_entry();
  34.     long time(), now;
  35.  
  36.     if (init(2)) {
  37.         fprintf(stderr, "mmd: Cannot initialize diskette\n");
  38.         exit(1);
  39.     }
  40.  
  41.     fargn = 1;
  42.     verbose = 0;
  43.     if (argc > 1) {
  44.         if (!strcmp(argv[1], "-v")) {
  45.             fargn = 2;
  46.             verbose = 1;
  47.         }
  48.     }
  49.                     /* only 1 directory ! */
  50.     if (argc != fargn+1) {
  51.         fprintf(stderr, "Usage: mmd [-v] msdosdirectory\n");
  52.         exit(1);
  53.     }
  54.     fixed = fixname(argv[fargn], verbose);
  55.  
  56.     strncpy(tname, fixed, 8);
  57.     strncpy(text, fixed+8, 3);
  58.     tname[8] = '\0';
  59.     text[3] = '\0';
  60.  
  61.     filename = unixname(tname, text);
  62.     pathname = getpath(argv[fargn]);
  63.  
  64.     if (subdir(pathname))
  65.         exit(1);
  66.                     /* see if exists and get slot */
  67.     slot = -1;
  68.     dot = 0;
  69.     for (entry=0; entry<dir_entries; entry++) {
  70.         dir = search(entry);
  71.                     /* if empty */
  72.         if (dir->name[0] == 0x0) {
  73.             if (slot < 0)
  74.                 slot = entry;
  75.             break;
  76.         }
  77.                     /* if erased */
  78.         if (dir->name[0] == 0xe5) {
  79.             if (slot < 0)
  80.                 slot = entry;
  81.             continue;
  82.         }
  83.                     /* if not a directory */
  84.         if (!(dir->attr & 0x10))
  85.             continue;
  86.  
  87.         strncpy(tname, (char *) dir->name, 8);
  88.         strncpy(text, (char *) dir->ext, 3);
  89.         tname[8] = '\0';
  90.         text[3] = '\0';
  91.  
  92.         newfile = unixname(tname, text);
  93.                     /* save the 'dot' directory info */
  94.         if (!strcmp(".", newfile))
  95.             dot = dir->start[1]*0x100 + dir->start[0];
  96.  
  97.         if (!strcmp(filename, newfile)) {
  98.             fprintf(stderr, "mmd: Directory \"%s\" already exists\n", filename);
  99.             exit(1);
  100.         }
  101.         free(newfile);
  102.     }
  103.                     /* no '.' entry means root directory */
  104.     if (dot == 0 && slot < 0) {
  105.         fprintf(stderr, "mmd: No directory slots\n");
  106.         exit(1);
  107.     }
  108.                     /* make the directory grow */
  109.     if (dot && slot < 0) {
  110.         if (grow(dot)) {
  111.             fprintf(stderr, "mmd: Disk full\n");
  112.             exit(1);
  113.         }
  114.                     /* first slot in 'new' directory */
  115.         slot = entry;
  116.     }
  117.     if ((fat = nextfat(0)) == -1) {
  118.         fprintf(stderr, "mmd: Disk full\n");
  119.         exit(1);
  120.     }
  121.                     /* make directory entry */
  122.     time(&now);
  123.     dir = mk_entry(fixed, 0x10, fat, 0L, now);
  124.     writedir(slot, dir);
  125.                     /* write the cluster */
  126.     putfat(fat, 0xfff);
  127.     putcluster(fat, dot);
  128.                     /* write FAT sectors */
  129.     writefat();
  130.     close(fd);
  131.     exit(0);
  132. }
  133.  
  134. /*
  135.  * Write a blank directory 'template' to the cluster starting at 'dot'.
  136.  */
  137.  
  138. void
  139. putcluster(dot, dot_dot)
  140. int dot, dot_dot;
  141. {
  142.     int buflen, start;
  143.     static struct directory dirs[32];
  144.     struct directory *mk_entry();
  145.     void exit(), perror(), move();
  146.     long time(), now;
  147.  
  148.     start = (dot - 2)*clus_size + dir_start + dir_len;
  149.     move(start);
  150.  
  151.     buflen = clus_size * MSECSIZ;
  152.                     /* make the '.' and '..' entries */    
  153.     time(&now);
  154.     dirs[0] = *mk_entry(".          ", 0x10, dot, 0L, now);
  155.     dirs[1] = *mk_entry("..         ", 0x10, dot_dot, 0L, now);
  156.  
  157.     if (write(fd, (char *) &dirs[0], buflen) != buflen) {
  158.         perror("putcluster: write");
  159.         exit(1);
  160.     }
  161.     return;
  162. }
  163.  
  164. /*
  165.  * Returns next free cluster or -1 if none are available.
  166.  */
  167.  
  168. int
  169. nextfat(last)
  170. int last;
  171. {
  172.     register int i;
  173.  
  174.     for (i=last+1; i<num_clus+2; i++) {
  175.         if (!getfat(i))
  176.             return(i);
  177.     }
  178.     return(-1);
  179. }
  180.