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 / mrd.c < prev    next >
C/C++ Source or Header  |  1991-07-20  |  3KB  |  138 lines

  1. /*
  2.  * Delete 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 ismatch, entry, start;
  30.     char *filename, *newfile, text[4], tname[9], *getname();
  31.     char *strncpy(), *pathname, *getpath(), *unixname();
  32.     void exit(), zapit(), writefat(), writedir(), free();
  33.     struct directory *dir, *search();
  34.  
  35.     if (init(2)) {
  36.         fprintf(stderr, "mrd: Cannot initialize diskette\n");
  37.         exit(1);
  38.     }
  39.                     /* only 1 directory ! */
  40.     if (argc != 2) {
  41.         fprintf(stderr, "Usage: mrd mdsosdirectory\n");
  42.         exit(1);
  43.     }
  44.  
  45.     filename = getname(argv[1]);
  46.     pathname = getpath(argv[1]);
  47.     if (subdir(pathname))
  48.         exit(1);
  49.  
  50.     ismatch = 0;
  51.     for (entry=0; entry<dir_entries; entry++) {
  52.         dir = search(entry);
  53.                     /* if empty */
  54.         if (dir->name[0] == 0x0)
  55.             break;
  56.                     /* if erased */
  57.         if (dir->name[0] == 0xe5)
  58.             continue;
  59.                     /* if not dir */
  60.         if (!(dir->attr & 0x10))
  61.             continue;
  62.  
  63.         strncpy(tname, (char *) dir->name, 8);
  64.         strncpy(text, (char *) dir->ext, 3);
  65.         tname[8] = '\0';
  66.         text[3] = '\0';
  67.  
  68.         newfile = unixname(tname, text);
  69.         if (!strcmp(newfile, filename)) {
  70.             start = dir->start[1]*0x100 + dir->start[0];
  71.             if (!isempty(start)) {
  72.                 fprintf(stderr, "mrd: Directory \"%s\" is not empty\n", filename);
  73.                 exit(1);
  74.             }
  75.             if (!start) {
  76.                 fprintf(stderr, "mrd: Can't remove root directory\n");
  77.                 exit(1);
  78.             }
  79.             zapit(start);
  80.             dir->name[0] = 0xe5;
  81.             writedir(entry, dir);
  82.             ismatch = 1;
  83.         }
  84.         free(newfile);
  85.     }
  86.     if (!ismatch) {
  87.         fprintf(stderr, "mrd: Directory \"%s\" not found\n", filename);
  88.         exit(1);
  89.     }
  90.                     /* update the FAT sectors */
  91.     writefat();
  92.     close(fd);
  93.     exit(0);
  94. }
  95.  
  96. /*
  97.  * See if directory is empty.  Returns 1 if empty, 0 if not.  Can't use
  98.  * subdir() and search() as it would clobber the globals.
  99.  */
  100.  
  101. int
  102. isempty(fat)
  103. int fat;
  104. {
  105.     register int i;
  106.     int next, buflen, sector;
  107.     unsigned char tbuf[CLSTRBUF];
  108.     void perror(), exit(), move();
  109.  
  110.     while (1) {
  111.         sector = (fat-2)*clus_size + dir_start + dir_len;
  112.         move(sector);
  113.         buflen = clus_size * MSECSIZ;
  114.         if (read(fd, (char *) tbuf, buflen) != buflen) {
  115.             perror("isempty: read");
  116.             exit(1);
  117.         }
  118.                     /* check first character of name */
  119.         for (i=0; i<MSECSIZ; i+=MDIRSIZ) {
  120.             if (tbuf[i] == '.')
  121.                 continue;
  122.             if (tbuf[i] != 0x0 && tbuf[i] != 0xe5)
  123.                 return(0);
  124.         }
  125.                     /* get next cluster number */
  126.         next = getfat(fat);
  127.         if (next == -1) {
  128.             fprintf(stderr, "isempty: FAT problem\n");
  129.             exit(1);
  130.         }
  131.                     /* end of cluster chain */
  132.         if (next >= 0xff8)
  133.             break;
  134.         fat = next;
  135.     }
  136.     return(1);
  137. }
  138.