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 / mdel.c < prev    next >
C/C++ Source or Header  |  1990-05-10  |  3KB  |  116 lines

  1. /*
  2.  * Delete 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. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29.     int i, ismatch, entry, start, nogo, verbose, fargn;
  30.     char *filename, *newfile, text[4], tname[9], *getname(), *unixname();
  31.     char *strncpy(), *getpath(), *pathname, ans[10];
  32.     void exit(), zapit(), writefat(), writedir(), free();
  33.     struct directory *dir, *search();
  34.  
  35.     if (init(2)) {
  36.         fprintf(stderr, "mdel: Cannot initialize diskette\n");
  37.         exit(1);
  38.     }
  39.  
  40.     if (argc < 2) {
  41.         fprintf(stderr, "Usage: mdel [-v] msdosfile [msdosfiles...]\n");
  42.         exit(1);
  43.     }
  44.     if (!strcmp(argv[1], "-v")) {
  45.         verbose = 1;
  46.         fargn = 2;
  47.     }
  48.     else {
  49.         verbose = 0;
  50.         fargn = 1;
  51.     }
  52.     for (i=fargn; i<argc; i++) {
  53.         filename = getname(argv[i]);
  54.         pathname = getpath(argv[i]);
  55.         if (subdir(pathname)) {
  56.             free(filename);
  57.             free(pathname);
  58.             continue;
  59.         }
  60.         nogo = 0;
  61.         ismatch = 0;
  62.         for (entry=0; entry<dir_entries; entry++) {
  63.             dir = search(entry);
  64.                     /* if empty */
  65.             if (dir->name[0] == 0x0)
  66.                 break;
  67.                     /* if erased */
  68.             if (dir->name[0] == 0xe5)
  69.                 continue;
  70.                     /* if dir or volume lable */
  71.             if ((dir->attr & 0x10) || (dir->attr & 0x08))
  72.                 continue;
  73.  
  74.             strncpy(tname, (char *) dir->name, 8);
  75.             strncpy(text, (char *) dir->ext, 3);
  76.             tname[8] = '\0';
  77.             text[3] = '\0';
  78.  
  79.             newfile = unixname(tname, text);
  80.                     /* see it if matches the pattern */
  81.             if (match(newfile, filename)) {
  82.                 if (verbose)
  83.                     printf("Removing %s\n", newfile);
  84.                 ismatch = 1;
  85.                 if (dir->attr & 0x01) {
  86.                     while (!nogo) {
  87.                         printf("mdel: \"%s\" is read only, erase anyway (y/n) ? ", newfile);
  88.                         gets(ans);
  89.                         if (ans[0] == 'y' || ans[0] == 'Y')
  90.                             break;
  91.                         if (ans[0] == 'n' || ans[0] == 'N')
  92.                             nogo = 1;
  93.                     }
  94.                     if (nogo) {
  95.                         free(newfile);
  96.                         continue;
  97.                     }
  98.                 }
  99.                 start = dir->start[1]*0x100 + dir->start[0];
  100.                 zapit(start);
  101.                 dir->name[0] = 0xe5;
  102.                 writedir(entry, dir);
  103.             }
  104.             free(newfile);
  105.         }
  106.         if (!ismatch)
  107.             fprintf(stderr, "mdel: File \"%s\" not found\n", filename);
  108.         free(filename);
  109.         free(pathname);
  110.     }
  111.                     /* update the FAT sectors */
  112.     writefat();
  113.     close(fd);
  114.     exit(0);
  115. }
  116.